语言切换器-链接更改URL [英] Language switcher - link changes URL
问题描述
我正在使用Transposh Wordpress插件使网站双语化.
I am using the Transposh Wordpress plugin to make a site bilingual.
该插件带有一个下拉语言选择器,但是我想在导航中放置一个链接,以在两种语言之间切换网站.
The plugin comes with a dropdown language selector, but I would like to instead place a link in the navigation that toggles the site between the two languages.
默认网站为英语,示例页面可能为xxx.com/page
The default site is in English, and an example page might be xxx.com/page
另一种语言是葡萄牙语,翻译后的页面位于xxx.com/pt/page
The other language is Portuguese, with the translated page at xxx.com/pt/page
所以我希望链接在这两个值之间切换:
So I would like the link to toggle between these two values:
<a href="example.com/pt/page">Português</a>
和
<a href="example.com/page">English</a>
jQuery最好做到这一点吗?
Would jQuery be best to do this?
提前谢谢!
推荐答案
使用此"example.com/pt/page"链接加载页面时,请将href和链接的文本更改为英语.然后,当您使用"example.com/page"加载页面时,请将href和文本更改为Português.
When you load your page with this "example.com/pt/page" link, change the href and text of the link to English. And when you load the page with "example.com/page", change the href and text to 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");
}
});
更新: 如果您想将此链接添加到站点的所有页面,则:
Update: If you want to add this link to all pages to your site, then:
1)为所有链接设置类.像这样:
1) set class for all the links. Like this:
<a class="lang" href="anything">anything</a>
2)现在像这样修改jQuery处理程序:
2) Now modify the jQuery handler like this :
$(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/"网址之下,而英语页面位于"example.com/"之下
Assuming, your Português pages comes under "example.com/pt/" urls and English pages comes under "example.com/"
这篇关于语言切换器-链接更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!