簡體   English   中英

JavaScript 中的翻譯就像 PHP 中的 gettext 一樣?

[英]Translation in JavaScript like gettext in PHP?

我在 PHP 代碼中使用了gettext ,但我遇到了一個大問題。 我所有的 JavaScript 文件都不受翻譯的影響,有人能告訴我一種將所選語言的翻譯也轉換成 JavaScript 的簡單方法嗎?

最簡單的方法是讓 PHP 文件將gettext的翻譯寫入 JavaScript 變量。

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 提到的翻譯插件(非常有趣!)。

您也可以在當前頁面的標題中定義字典,而無需包含外部文件,但那樣的話,您將不得不在每次頁面加載時查找並發送數據——這是非常不必要的,因為字典往往很少更改。

我通常以 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 currentLanguage = document.documentElement.lang || "en";
app.lang = app.translations[ currentLanguage ] || app.translations.en;

然后你可以寫這樣的代碼:

alert( app.lang.hello );

或者,一個i18n()gettext()函數可以帶來一些智能,如果鍵不存在則返回默認文本)。 例如:

function gettext( key )
{
  return app.lang[ key ] || app.translations.en[ key ] || "{translation key not found: " + key + "}";
}

試試, jQuery i18njQuery 本地化

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'));

JSGettext存檔鏈接)是 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 即可正常工作。

點擊這里

如果您改掉在代碼中使用字符串字面量的壞習慣,您的生活就會變得更加輕松。 也就是說,而不是

 alert("Some message")

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

其中“#some_message_id”是在服務器端生成的隱藏 div 或 span。

對於 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") ) ;

作為進一步的提示,有一個名為 po2json 的 perl 腳本,它將從 .po 文件生成 json。

這個庫似乎是 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM