多语言 AJAX 应用程序
我正在使用大量 javascript 和 AJAX 构建一个 Web 应用程序。这个应用程序必须是多语言的。
在服务器端,我有一个用 PHP 编码的小模板引擎,用于处理翻译文件。但在客户端,我有时会显示文本,并且需要按照相同的规则对其进行翻译。我不想每次需要显示消息时都发出http请求,那么最好的方法是什么?
换句话说,如何构建一个服务端和客户端统一的翻译系统?
I'm building a web application using a lot of javascript and AJAX. This app has to be multilingual.
On the server side, I have a little template-engine coded in PHP which handles translation files. But on the client side, I sometimes display text and I need to translate it following the same rules. I do not want to make an http request every time I need to display a message, so what is the best way to do it?
In other words, how to build an unified translation system working both server and client side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置一个服务器端脚本,该脚本将根据用户首选项提供包含翻译后的变量的动态 JavaScript。例如:
此脚本可以包含带有翻译的全局变量:
该脚本将由客户端浏览器缓存并在您的脚本中使用:
只需确保设置正确的 HTTP 响应缓存标头,以便客户端只需获取这些值一次,可能是在选择语言时。显然,服务器端脚本还可以使用 Accept-Language HTTP 请求标头来确定客户端首选项,而不是查询字符串参数。
You could setup a server side script which will serve dynamic javascript containing the translated variables according to the user preferences. For example:
This script could contain a global variable with the translations:
The script will be cached by client browsers and used in your scripts:
Just make sure you setup proper HTTP response cache headers so that the clients need to fetch those values only once, probably when choosing a language. Obviously the server side script could also use the
Accept-Language
HTTP request header to determine the client preferences instead of a query string parameter.