PHP 코드에서 gettext
을 사용하고 있지만 큰 문제가 있습니다. 모든 자바 스크립트 파일은 번역의 영향을받지 않으므로 선택한 언어의 번역을 자바 스크립트로 가져 오는 쉬운 방법을 누군가에게 말해 줄 수 있습니다.PHP에서 gettext와 같은 자바 스크립트 번역?
답변
가장 쉬운 방법은 PHP 파일 쓰기를 데에서 언어 파일에서 JSON 기반의 사전을 생성해야하고, 물론의 jQuery 국제화의 예 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"); ?>"
다음은 다음과 같습니다 (! 매우 흥미있는) 나는 또한 번역과 함께이 방법을 추천 플러그인 S.Mark 언급
<script type="text/javascript" src="js_lang.php"></script>
을 .
외부 파일을 포함하지 않고 현재 페이지의 머리글에서 사전을 정의 할 수도 있지만 그럴 경우 모든 페이지로드시 데이터를 검색하여 보내야합니다. 매우 드물게 변합니다.
시도 jQuery i18n 또는 jQuery localisation
당신이 PHP를
var my_dictionary = {
"some text" : "a translation",
"some more text" : "another translation"
}
$.i18n.setDictionary(my_dictionary);
$('div#example').text($.i18n._('some text'));
내가 경고를하는 것이 tryt nevere 있고, combinasion 오순절 JSON에서의오고 그리고 더. – ParisNakitaKejser
또는 http://i18next.com에 json 변환기에 gettext가 추가로 제공됩니다. 전체 기능입니다. – jamuhl
나는 일반적으로 자바 스크립트 구조에 번역을 수출 :
페이지 텍스트의 현재 언어를 사용하여 정의 할 수 있습니다var app = {}
var app.translations = {
en: { hello: "Hello, World!"
, bye: "Goodbye!"
}
, nl: { hello: "Hallo, Wereld!"
, bye: "Tot ziens!"
}
};
: <html xml:lang="en" lang="nl">
이 자바 스크립트에서 읽을 수 있습니다 :
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")
소용이다
"#some_message_id"가 서버 측에서 생성 숨겨진 DIV 또는 스팬alert($("#some_message_id").text())
.
추가 힌트로 .po 파일에서 json을 생성하는 po2json이라는 perl 스크립트가 있습니다.
JSGettext (archived link)은 GNU gettext 사양을 가장 잘 구현 한 것입니다. JSGETTEXT 패키지를 먼저 다운로드하여 내 페이지 /js/Gettext에 포함하십시오.JS 참고로 아래 링크를 찾을 예를
window.onload = function init(){
var gt = new Gettext({ 'domain' : 'messages' });
alert(gt.gettext('Hello world'));
}
을위한 자바 스크립트 코드
<?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>
. .js 파일을 .php로 변환하지 않고도 정상적으로 작동합니다.
링크 만 대답하면 좋지 않습니다. –
링크가있는 답변이 업데이트되었습니다. –
http://www.jsdelivr.com/projects/jsgettext 및 https://sourceforge.net/projects/jsgettext.berlios/ – miralong
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")) ;
예를 들어보십시오. http://pastebin.org에 붙여 넣으시겠습니까? :) – ParisNakitaKejser
@neonman 예를 들면 정확히 무엇입니까? –
나는 지금 idé를 얻은 것 같아 :) 탱크는 도움을 요청할 것이다.) – ParisNakitaKejser