代码之家  ›  专栏  ›  技术社区  ›  ParisNakitaKejser

在JavaScript中的翻译,比如在PHP中的getText?

  •  27
  • ParisNakitaKejser  · 技术社区  · 16 年前

    我正在使用 gettext 在我的PHP代码中,但是我有一个大问题。我所有的javascript文件都不受翻译的影响,有人能告诉我一个简单的方法,将所选语言的翻译也转换成javascript。

    8 回复  |  直到 7 年前
        1
  •  18
  •   Pekka    16 年前

    最简单的方法是让一个PHP文件从 gettext 到javascript变量中。

    JSYangLang.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结合使用。.

    您也可以在当前页面的标题中定义字典,而不包括外部文件,但是这样,您就必须在每次页面加载时查找和发送数据,这是完全不必要的,因为字典的变化很少。

        2
  •  13
  •   vdboor    16 年前

    我通常以javascript结构导出翻译:

    var app = {}
    var app.translations = {
      en:  { hello: "Hello, World!"
           , bye:   "Goodbye!"
           }
    , nl:  { hello: "Hallo, Wereld!"
           , bye:   "Tot ziens!"
           }
    };
    

    页面文本的当前语言可通过以下方式定义: <html xml:lang="en" lang="nl">

    这可以在javascript中读取:

    var curentLanguage = document.documentElement.lang || "en";
    app.lang = app.translations[ currentLanguage ] || app.translations.en;
    

    然后你可以写这样的代码:

    alert( app.lang.hello );
    

    可选地,A i18n() gettext() 函数可以带来一些智能,如果键不存在,则返回默认文本)。例如:

    function gettext( key )
    {
      return app.lang[ key ] || app.translations.en[ key ] || "{translation key not found: " + key + "}";
    }
    
        3
  •  7
  •   YOU    16 年前

    尝试, jQuery i18n jQuery localisation

    jquery i18n的一个例子,当然您需要从PHP的语言文件生成基于JSON的字典。

    var my_dictionary = { 
        "some text"  : "a translation",
        "some more text"  : "another translation"
    }
    $.i18n.setDictionary(my_dictionary);
    
    
    $('div#example').text($.i18n._('some text'));
    
        4
  •  3
  •   rubo77    9 年前

    JSGettext ( archived link )是GNU GetText规范的最佳实现。 首先下载jsGetText包并将其包含在您的页面中 /js/gettext.js版本

    <?php
    $locale = "ja_JP.utf8";
    if(isSet($_GET["locale"]))$locale = $_GET["locale"];
    ?>
    <html>
    <head>
    <link rel="gettext" type="application/x-po" href="/locale/<?php echo $locale ?>/LC_MESSAGES/messages.po" />
    <script type="text/javascript" src="/js/Gettext.js"></script>
    <script type="text/javascript" src="/js/test.js"></script>
    </head>
    <body>
    Test!
    </body>
    </html>
    

    例如,javascript代码

    window.onload = function init(){
    var gt = new Gettext({ 'domain' : 'messages' });
    alert(gt.gettext('Hello world'));
    }
    

    以下链接可供参考。它在不将.js文件转换为.php的情况下工作正常。

    Click here

        5
  •  1
  •   user187291    16 年前

    如果你摆脱了在代码中使用字符串的坏习惯,你的生活就会容易得多。也就是说,而不是

     alert("Some message")
    

    使用

    alert($("#some_message_id").text())
    

    其中“some_message_id”是在服务器端生成的隐藏的DIV或SPAN。

        6
  •  1
  •   dsas    15 年前

    作为进一步的提示,有一个名为po2json的Perl脚本,它将从.po文件生成json。

        7
  •  1
  •   tnga    10 年前

    对于GNU GetText API的javascript实现,这些链接也很有用:
    http://tnga.github.io/lib.ijs
    http://tnga.github.io/lib.ijs/docs/iJS.Gettext.html

    //set the locale in which the messages will be translated
    iJS.i18n.setlocale("fr_FR.utf8") ;
    //add domain where to find messages data. can also be in .json or .mo
    iJS.i18n.bindtextdomain("domain_po", "./path_to_locale", "po") ;
    //Always do this after a `setlocale` or a `bindtextdomain` call.
    iJS.i18n.try_load_lang() ; //will load and parse messages data from the setting catalog.
    //now print your messages
    alert( iJS.i18n.gettext("messages to be translated") ) ;
    //or use the common way to print your messages
    alert( iJS._("another way to get translated messages") ) ;
    
        8
  •  0
  •   abumalick    7 年前

    这个库似乎是用JavaScript实现getText的最佳方法:

    http://messageformat.github.io/Jed/

    https://github.com/messageformat/Jed

    文档中的示例:

    <script src="jed.js"></script>
    <script>
    var i18n = new Jed({
      // Generally output by a .po file conversion
      locale_data : {
        "messages" : {
          "" : {
            "domain" : "messages",
            "lang"   : "en",
            "plural_forms" : "nplurals=2; plural=(n != 1);"
          },
          "some key" : [ "some value"]
        }
      },
      "domain" : "messages"
    });
    
    alert( i18n.gettext( "some key" ) ); // alerts "some value"
    </script>