この「example.com/pt/page」リンクを使用してページをロードするときは、リンクのhrefとテキストを英語に変更してください。また、「example.com/page」を使用してページを読み込むときに、hrefとテキストをPortuguêsに変更します。
<a id="lang" href="example.com/pt/page">Português</a>
$(document).ready(function() {
var winLocation = window.location;
var loc = winLocation + "";
if(loc.indexOf("example.com/pt/page") != -1) {
$("#lang").prop("href", "example.com/page");
$("#lang").text("English");
}
else {
$("#lang").prop("href", "example.com/pt/page");
$("#lang").text("Português");
}
});
更新:このリンクをサイトのすべてのページに追加する場合は、次のようにします。
1)すべてのリンクのクラスを設定します。このような:
<a class="lang" href="anything">anything</a>
2)jQueryハンドラーを次のように変更します。
$(document).ready(function() {
var winLocation = window.location;
var loc = winLocation + "";
if(loc.indexOf("/example.com/pt/") != -1) {
$(".lang").prop("href", loc.replace("/example.com/pt/", "/example.com/"));
$(".lang").text("English");
}
else {
$(".lang").prop("href", loc.replace("/example.com/", "/example.com/pt/"));
$(".lang").text("Português");
}
});
ポルトガル語のページは「example.com/pt/」のURLにあり、英語のページは「example.com/」にあると仮定します。