2010-03-08 109 views

回答

17

最簡單的方法是有一個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提到。

您也可以在當前頁面的標題中定義字典,而不包含外部文件,但這樣,您將不得不在每個頁面加載時查找併發送數據 - 這是非常不必要的,因爲字典傾向於很少改變。

+0

你可以maby試着去找一個eg。並粘貼在http://pastebin.org上? :) – ParisNakitaKejser 2010-03-08 08:52:30

+0

@neonman的例子究竟是什麼? – 2010-03-08 08:54:52

+0

我想我現在得到idé:)坦克的幫助,我會用你的責任;) – ParisNakitaKejser 2010-03-08 09:15:17

7

嘗試,jQuery i18njQuery localisation

jQuery的國際化,當然你需要產生從語言文件基於JSON字典從PHP

var my_dictionary = { 
    "some text" : "a translation", 
    "some more text" : "another translation" 
} 
$.i18n.setDictionary(my_dictionary); 


$('div#example').text($.i18n._('some text')); 
+0

我有nevere tryt的是,其在combinasion白衣JSON正在添加,警報和更多。 – ParisNakitaKejser 2010-03-08 08:52:56

+0

或者您可以嘗試http://i18next.com附帶gettext到json轉換器。並且功能全面。 – jamuhl 2012-09-14 07:35:18

11

我一般出口的翻譯在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); 

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

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

如果您擺脫了在您的代碼中使用字符串文字的壞習慣,您可以讓您的生活更輕鬆。也就是說,不是

alert("Some message") 

使用,

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

其中「#some_message_id」是在服務器端生成一個隱藏的股利或跨度。

0

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

3

JSGettextarchived 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> 

例如

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

JavaScript代碼參考在下面找到鏈接。它工作正常,沒有將.js文件轉換爲.php。

Click here

+0

鏈接唯一的答案不好,因爲鏈接可能會死。 – 2013-09-26 06:02:29

+0

用鏈接更新了答案。 – 2013-09-26 06:11:40

+1

http://www.jsdelivr.com/projects/jsgettext和 https://sourceforge.net/projects/jsgettext.berlios/ – miralong 2016-02-11 17:21:52

1

對於JavaScript執行GNU的gettext API的這些鏈接可能也有用:
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")) ; 
+0

雖然此鏈接可能回答問題,但最好包含答案的基本部分並提供參考鏈接。如果外部鏈接頁面將來發生變化,僅鏈接答案將不會有任何用處 – Panther 2015-08-13 06:11:39

+0

Ok現在已修復 – tnga 2015-11-13 15:51:38