How to dynamically change language using jquery-i18n-properties and JavaScript?(如何使用 jquery-i18n-properties 和 JavaScript 动态更改语言?)
问题描述
我正在使用 jquery-i18n-properties 插件向我的网站提供 i18n.我已经更改了我的 HTML 并执行以下操作以加载所需的 .properties:
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'));
...
}
});
一切正常.我现在要做的是允许用户按下按钮更改语言.有没有办法只使用 javascript 或这个插件而不重新加载页面?谢谢.
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.
推荐答案
是的,很简单.事实上,在 jQuery.i18n.properties 网站 (http://codingwithcoffee.com/files/trunk/index.html).
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).
诀窍是用新语言简单地重新初始化插件.将您当前的代码抽象到另一个函数中,该函数接受 lang 作为参数.使用某种默认语言进行初始化,一旦用户更改了当前语言,就将其传递给该函数.
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 动态更改语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 jquery-i18n-properties 和 JavaScript 动态更改语言?
基础教程推荐
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
