像PHP中的gettext一样在JavaScript中进行翻译? [英] Translation in JavaScript like gettext in PHP?

查看:159
本文介绍了像PHP中的gettext一样在JavaScript中进行翻译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP代码中使用 gettext ,但我遇到了一个大问题。我的所有JavaScript文件都不受翻译的影响,有人可以告诉我一种简单的方法,可以将所选语言的翻译成JavaScript。

I am using gettext in my PHP code, but I have a big problem. All my JavaScript files are not affected by the translation, can somebody tell me an easy way to get the translations in the chosen language into JavaScript as well.

推荐答案

最简单的方法是让PHP文件将 gettext 的翻译写入JavaScript变量。

The easiest way is having a PHP file write the translations from gettext into JavaScript variables.

js_lang.php:

js_lang.php:

word_hello = "<?php echo gettext("hello"); ?>"
word_world = "<?php echo gettext("world"); ?>"
word_how_are_you = "<?php echo gettext("how_are_you"); ?>"

然后加入:

<script type="text/javascript" src="js_lang.php"></script>

我还推荐这种方法与翻译插件S.Mark提及(非常有趣) !)。

I would also recommend this method in conjunction with the translation plugins S.Mark mentions (which are very interesting!).

您也可以在当前页面的标题中定义字典,而不包含外部文件,但是这样,您必须在每个文件中查找并发送数据页面加载 - 非常不必要,因为字典往往很少改变。

You can define the dictionary in the current page's header, too, without including an external file, but that way, you would have to look up and send the data on every page load - quite unnecessary, as a dictionary tends to change very rarely.

这篇关于像PHP中的gettext一样在JavaScript中进行翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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