用JQuery进行本地化? [英] Localization with JQuery?

查看:92
本文介绍了用JQuery进行本地化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用JQuery处理本地化.我想用德语文本设置innerHTML,但是如果将浏览器配置为使用英语,那么我想设置英语文本.

I have no idea how to handle localization with JQuery. I want to set an innerHTML with German text, but if the browser is configured to use English, then I want to set English text.

在PHP中,我将gettext用于此类内容,但是如何在JavaScript/jQuery中做到这一点?

In PHP I use gettext for such stuff, but how to do it in JavaScript/jQuery?

推荐答案

没有简单的解决方案.我可能要做的是为每种语言创建几个带有语言文本的脚本,并在PHP中包含适当的脚本.因此,如果有人使用您网站的英文版本,那么您将仅包含英文文件,如果有人使用德语版本,则您将包括德语文件等.

There is no easy solution for that. What I would probably do is create several scripts with language texts for every language and include proper one in PHP. So if someone is using english version of your site you would include only english file, if someone is using german version you would include german language file etc.

示例:

// your script logic
myscript.js

// language texts
myscript.en.js
myscript.de.js
myscript.it.js
...

您可以这样定义所有语言文件:

You can define all language files like that:

LANG = {
    txt1: 'English text1',
    txt2: 'English text2'
    ...
};

请确保您仅在HTML中包含其中之一,并确保首先包含语言文件.

Make sure you are including only one of those in your HTML and make sure to include language file first i.e.

<script type="text/javascript" src="myscript.de.js"></script>
<script type="text/javascript" src="myscript.js"></script>

然后,您可以在主脚本中使用这些本地化的文本,例如:

Then you can use those localized texts in your main script for example:

$(document).ready(function () {
    alert(LANG.txt1);
});

最好的是,您的逻辑(在此示例中为myscript.js)不必担心本地化,并且如果要添加新的语言文件,则不必更改它.

What's best about it is that your logic (myscript.js in this example) doesn't have to worry about localization and you won't have to change it if you want to add a new language file.

这篇关于用JQuery进行本地化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆