多语言支持
我正在开发一个使用 jQuery 的 HTML5 应用程序。我想使其成为多语言:检测用户语言并将所有文字更改为用户语言。
我认为一种方法是为每种支持的语言使用一个 HTML 文件,但这很浪费空间。
另一种方法是使用 jQuery 将所有文字更改为用户的语言。但我不知道该怎么做。
你怎么认为?有更好的方法吗?
更新:
我忘了说我在 JavaScript 中也有一些文字。
I'm developing a HTML5 application that uses jQuery. I want to make it multi language: detecting user language and changing all literals to user's language.
I think that one approach is to use one HTML file for each language supported, but it is a waste of space.
Another approach could be use jQuery to change all literals to user's language. But I'm not sure how to do this.
What do you think? Is there a better approach?
UPDATE:
I've forget it to say that I have some literals inside JavaScript too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能对用户的语言进行某种服务器端查询,然后自动加载适当的文本吗?或许 CMS 也适合这里。
对于所有 Javascript 代码,我将使用字符串文字作为变量。因此您可以加载适合用户语言的不同语言文件。
文件 english.js:
文件 german.js:
在你的 Javascript 中:
或者我错过了这里的重点? ;)
Coulnd't you do some sort of server-side query to the user's language and then load the appropriate text automatically? Maybe even a CMS is appropriate here.
For all the Javascript code, I would use String literals as a variable. So you can load a different language file appropriate to the user language.
File english.js:
File german.js:
And in your Javascript:
Or am I missing the point here? ;)
在我的 HTML5 演示中,“HTML5 词云”,
http://timc.idv.tw/wordcloud/
(源代码可以在 https://github.com/timdream/wordcloud 找到)
我单独写的适用于不同语言的 HTML,并包含一组 Javascript 文件。对于脚本中的文字字符串,我将它们收集到一个对象(名为
T
)中,并将其放入每个 HTML 文件的块中。
这使我可以灵活地为每种语言定制页面;正如你所看到的,我在英文版中列出了 CNN 作为示例,但在中文版中列出了其他来源。
In the HTML5 Demo of my, the "HTML5 Word Clouds",
http://timc.idv.tw/wordcloud/
(source code can be found at https://github.com/timdream/wordcloud)
I wrote separate HTML for different languages, and includes a single set of Javascript files. For literal strings with in the script, I collect them into an object (named
T
) and put it into<script>
block of each HTML files.This give me the flexibility to customize pages for each language; as you can see, I listed CNN as example in English version, but list other sources in the Chinese version.
如果您绝对必须在客户端执行此操作,那么使用 json 或 xml 文件来存储您的翻译怎么样?这避免了创建同一页面的副本的麻烦。例如,在你的 json.你会有“welcome_eng”:“welcome”和“welcome_fr”:“bienvenue”等。
然后使用 javascript 加载适当的变量,如下所示获取变量:
blablabla=["welcome_"+language]
或者,如果您想要更少的工作,您的欢迎文本的 div 将具有 id“welcome”,然后您的 javascript 获取该 id 并添加适当的内容。
If you absolutely have to do it at client-side, how about using a json or xml file to store your translations? This avoids the trouble of creating copies of the same page. For example, in your json. you'd have "welcome_eng": "welcome" and "welcome_fr": "bienvenue", etc.
Then you load the appropriate one using javascript, as in, get the variables like this:
blablabla=["welcome_"+language]
Or, if you want even less work, your welcome text's div will have the id "welcome", then your javascript gets the id and add the appropriate content.
这主要取决于页面的动态或静态程度:如果页面包含大量文本,则为每种语言复制页面会更容易。在这种情况下,仔细将 HTML 与 CSS 和脚本隔离非常重要。所有 CSS 和脚本应存储在单独的页面中,以避免在更新样式或脚本时必须更新所有翻译。
OTOH,如果它主要是动态的,那么在创建页面时用翻译替换文本片段是有意义的。但我不会在客户端(jQuery)进行文本替换。这是服务器端的工作。
编辑:如果您有 javascript 文字,那么您当然应该将它们与 HTML 并排保存在 HTML 文件中或单独的 .js 文件中。但服务器仍需以正确的语言传送内容。
It mostly depends on how dynamic or static your pages are: If they contain much text, than it will be easier to duplicate the page for each language. In this case it is very important to carefully isolate HTML from CSS and scripts. All CSS and scripts should be stored in separate pages, in order to avoid having to update all translations whnever you update a style or a script.
OTOH, if it is mostly dynamic, than it makes sense to replace text snippets by their translation when creating the page. But I wouldn't do the text replacement client-side (jQuery). It's a server-side job.
Edit: If you have javascript literals, you should then of course keep them side by side with the HTML, either in the HTML file or in a separate .js file. But it remains up to the server to deliver the contents in the correct language.