问题描述
我使用jquery-i18n-properties插件向我的网站提供i18n。我已经改变了我的HTML,并执行以下操作来加载所需的.properties:
jQuery.i18n.properties ({
name:'Messages',
path:'bundle /',
mode:'both',
语言:lang,
callback:function() {
$(#msg_welcome)。text(jQuery.i18n.prop('msg_welcome'));
...
}
});
一切正常。我现在想要做的是允许用户按下按钮来改变语言。有没有办法做到这一点,只使用JavaScript或这个插件,而无需重新加载页面?谢谢。
是的,这很简单。实际上,在jQuery.i18n.properties网站上有这样的例子()。
function changeLang(lang){
jQuery.i18n.properties({
name:'Messages',
path:'bundle /',
mode:'both',
language:lang,
callback:function(){
$(#msg_welcome)。text(jQuery.i18n.prop('msg_welcome'));
...
}
});
}
//代码中的其他位置
//改为英文
changeLang('en_EN');
//改为其他
changeLang('pt_PT');
I'm providing i18n to my website using jquery-i18n-properties plugin. I have already changed my HTML and do the following in order to load the .properties that is required:
jQuery.i18n.properties({
name: 'Messages',
path: 'bundle/',
mode: 'both',
language: lang,
callback: function() {
$("#msg_welcome").text(jQuery.i18n.prop('msg_welcome'));
...
}
});
Everything works fine. What I want to do now is allow the user to change the language pressing a button. Is there a way to do it using only javascript or this plugin without reloading the page? Thanks.
Yeah it's easy. In fact, there's example that does just that on jQuery.i18n.properties site (http://codingwithcoffee.com/files/trunk/index.html).
Trick is, to simply reinitialize plugin with new language. Abstract your current code into another function, that accepts lang as parameter. Initialize with some default language, and once user changes the current language, pass it to that function.
function changeLang(lang) {
jQuery.i18n.properties({
name: 'Messages',
path: 'bundle/',
mode: 'both',
language: lang,
callback: function() {
$("#msg_welcome").text(jQuery.i18n.prop('msg_welcome'));
...
}
});
}
// somewhere else in your code
// change to english
changeLang('en_EN');
// change to other
changeLang('pt_PT');
这篇关于如何使用jquery-i18n-properties和JavaScript动态改变语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!