如何将gettext字典中的术语提供给JavaScript? [英] How do I feed terms from a gettext dictionary into JavaScript?

查看:78
本文介绍了如何将gettext字典中的术语提供给JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用支持多种语言的php和javascript(jquery)开发一个网站。我正在尝试获得以下内容:

I'm trying to develop a website with php and javascript (jquery) that supports multiple languages. I'm trying to obtain something that is:


  • 高效:我需要尽可能减少此操作的后端处理。

  • 可扩展:它必须简单实用,为翻译添加新字符串

问题是:我的javascript代码动态生成DOM中的各种元素,需要打印语言文件中的一些字符串。

The problem is: my javascript code is generating dynamically various elements in the DOM and needs to print some strings that are in the language files.

我的实际解决方案是,使用 gettext ,在文档中打印javascript必须使用的字符串:

My actual solution is, using gettext in php, to print in the document the strings that have to be used by javascript:

<?php
... some kind of gettext initialization ...
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo _("My page Title"); ?></title>
    ...
</head>
<body>

    ...

    <script type="text/javascript">
        var LANG = {
            "string1" : "<?php echo _("string1"); ?>",
            "string2" : "<?php echo _("string2"); ?>",
            "string3" : "<?php echo _("string3"); ?>",
            ...
        };
    </script>
</body>
</html>

然后我正在访问 LANG 在我的js来源中,但我不认为这是最好的解决方案...

Then I'm accessing to the LANG in my js sources, but I don't think that this is the best solution...

我的问题是:


  • 使用相同的数据库php将翻译应用于javascript文件的正确方法是什么?

  • 效率如何 gettext 库?在生成的页面上实现某种形式的缓存机制可能很有用吗?

  • What is the correct approach to apply a translations to a javascript file, using the same "database" of php?
  • How efficient is gettext library? It could be useful to implement some form of caching mechanism on the generated pages?

推荐答案

gettext 被许多行业标准应用程序使用。它没有任何根本性的错误,除了我认为它处理得不好(因为它往往依赖于系统上安装的语言环境和这样的垃圾)。

gettext is used by many industry-standard apps out there. There's nothing fundamentally wrong with it except that I think it doesn't handle very well (as it tends to depend on locales installed on the system and crap like that).

更复杂但也更灵活的替代方案可以是 Zend_Translate 可以连接到不同的字典后端 - .po / mo文件,如gettext(这很好,因为你有一整套用于所有平台的编辑工具),还有数据库。

A more complex, but also more flexible alternative could be Zend_Translate that can be connected to different dictionary backends - .po/mo files like gettext (which are great because you have an entire universe of editing tools for all platforms), but also databases.

你展示的方法(将字典送入主文档中的JavaScript)很好,因为据说你应该已经在那时使用PHP进行动态处理了JavaScript文件可以保持静态,这很棒。

The approach you show (feeding the dictionary into JavaScript in the main document) is fine, as you supposedly are supposedly already using PHP to do dynamic processing at that point and the JavaScript file can remain static, which is great.

如果你有一个庞大的字典,那么HTML文件可能会有点沉重,因为你要转移所有字典每个请求的数据。在这种情况下,您应该考虑外部JavaScript资源。您有两种选择:

It might be a bit heavy on the HTML file if you have a huge dictionary, though, as you're transferring all that data on every request. In that case, you should consider an external JavaScript resource after all. You have two options:


  • 提供PHP生成的JavaScript文件 - 包含所有字典定义的文件。使用某种形式的 If-modified-因为缓存所以你只有在实际发生了某些变化时才重新渲染字典,或者发送缓存生命周期的缓存标题。

  • Serve a PHP-generated JavaScript file - one that contains all the dictionary definitions. Use some form of If-modified-since caching so you have to re-render the dictionary only when something has actually changed, or send caching headers with looong lifetimes.

或:每当字典更新时,运行PHP脚本(自动或手动),生成包含所有字典定义的静态JavaScript文件。这可能是两个世界中最好的。

or: whenever the dictionary gets updated, run a PHP script (automatically or manually) that generates a static JavaScript file containing all the dictionary definitions. That might be the best of both worlds.

这篇关于如何将gettext字典中的术语提供给JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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