我在我的PHP代碼中使用gettext
,但是我遇到了一個很大的問題。我的所有JavaScript文件都不受翻譯的影響,有人可以告訴我一種簡單的方法,可以將所選語言的翻譯轉換爲JavaScript。翻譯在JavaScript中像PHP中的gettext?
回答
最簡單的方法是有一個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提到。
您也可以在當前頁面的標題中定義字典,而不包含外部文件,但這樣,您將不得不在每個頁面加載時查找併發送數據 - 這是非常不必要的,因爲字典傾向於很少改變。
嘗試,jQuery i18n或jQuery 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'));
我有nevere tryt的是,其在combinasion白衣JSON正在添加,警報和更多。 – ParisNakitaKejser 2010-03-08 08:52:56
或者您可以嘗試http://i18next.com附帶gettext到json轉換器。並且功能全面。 – jamuhl 2012-09-14 07:35:18
我一般出口的翻譯在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 + "}";
}
如果您擺脫了在您的代碼中使用字符串文字的壞習慣,您可以讓您的生活更輕鬆。也就是說,不是
alert("Some message")
使用,
alert($("#some_message_id").text())
其中「#some_message_id」是在服務器端生成一個隱藏的股利或跨度。
作爲進一步的提示,有一個名爲po2json的perl腳本,它將從.po文件生成json。
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>
例如
window.onload = function init(){
var gt = new Gettext({ 'domain' : 'messages' });
alert(gt.gettext('Hello world'));
}
JavaScript代碼參考在下面找到鏈接。它工作正常,沒有將.js文件轉換爲.php。
鏈接唯一的答案不好,因爲鏈接可能會死。 – 2013-09-26 06:02:29
用鏈接更新了答案。 – 2013-09-26 06:11:40
http://www.jsdelivr.com/projects/jsgettext和 https://sourceforge.net/projects/jsgettext.berlios/ – miralong 2016-02-11 17:21:52
對於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")) ;
- 1. PHP gettext不翻譯
- 2. Gettext不翻譯
- 3. getText crowdfounded翻譯
- 4. gettext未翻譯
- 5. Silex Twig翻譯Gettext
- 6. 翻譯php應用程序與gettext
- 7. 用PHP和gettext翻譯XSL文件
- 8. PHP gettext翻譯不起作用
- 9. Gettext不同的翻譯
- 10. 在php項目中使用gettext - 只有一種語言翻譯
- 11. 的Django如何mark_safe在gettext的翻譯
- 12. gettext沒有翻譯顯示
- 13. 沒有使用gettext翻譯
- 14. 如何保留gettext()翻譯?
- 15. ZF2 - 覆蓋gettext翻譯
- 16. 如何標記在po gettext文件中翻譯的空翻譯(msgstr)?
- 17. 翻譯javascript函數在PHP
- 18. 從PHP heredoc語法中提取gettext翻譯?
- 19. 在PHP中的翻譯
- 20. Django模型翻譯:在數據庫中存儲翻譯或使用gettext?
- 21. PHP gettext在從shell運行時以錯誤的語言翻譯
- 22. q在PHP中翻譯
- 23. 如何在angular-gettext中輸出alert()的翻譯內容?
- 24. 通過AJAX返回的Gettext未翻譯
- 25. Gettext的或數據庫翻譯
- 26. gettext翻譯的多功能功能
- 27. Windows 7上的Gettext不會翻譯
- 28. Gettext的翻譯字符串鏈接
- 29. 翻譯圖像中的Android
- 30. PHP的gettext:不是所有的字符串變成翻譯
你可以maby試着去找一個eg。並粘貼在http://pastebin.org上? :) – ParisNakitaKejser 2010-03-08 08:52:30
@neonman的例子究竟是什麼? – 2010-03-08 08:54:52
我想我現在得到idé:)坦克的幫助,我會用你的責任;) – ParisNakitaKejser 2010-03-08 09:15:17