简体   繁体   English

用Poedit翻译JS的方法

[英]A way to translate JS with Poedit

I´m translating strings in PHP with Gettext and Poedit software. 我正在用Gettext和Poedit软件在PHP中翻译字符串。 I would like to use translated strings also in JavaScript, but without putting the code inline into HTML-documents, but in external files. 我也想在JavaScript中使用转换后的字符串,但不要将代码内联到HTML文档中,而要放在外部文件中。 Inline in HTML-documents it would be no problem because the inline JS-code is also rendered. 内联在HTML文档中,这没问题,因为内联JS代码也已呈现。 An example: 一个例子:

var hello = <?=_("hello");?>

The translator should use the same translation-tables as the php-code does (because of duplicate strings). 译者应使用与php代码相同的翻译表(因为字符串重复)。

So my idea was to "compile" the JavaScript files with PHP. 所以我的想法是用PHP“编译” JavaScript文件。 For example in an external JS-file is this code: 例如,在外部JS文件中的代码是:

if (window.confirm("_translate('are_you_sure')")) {
location.href = this.href;
}

A php script reads this code above and finds all strings inside _translate() . 一个php脚本在上面读取此代码,并在_translate()找到所有字符串。 The code will replace _translate('are_you_sure') with the translated string. 该代码将用翻译后的字符串替换_translate('are_you_sure') It works but is never translating, because the Poedit software will not find the string in _translate() even if I add _translate into the keys of the source. 它可以工作,但是永远不会翻译,因为即使我将_translate添加到源代码的键中,Poedit软件也不会在_translate()找到字符串。 So there is no translated strings to be translated. 因此,没有要翻译的翻译字符串。

So my question is how to tell Poedit that he should read strings with _translate() inside a javascript-file? 所以我的问题是如何告诉Poedit他应该在javascript文件中使用_translate()读取字符串?

you could include a php script, which do the translations in the javascript file 您可以包含一个php脚本,该脚本在javascript文件中进行翻译

$js = $_GET['js'];

// security check
if (preg_match('~[^a-z0-9_-]~', $js)) {
    die("invalid js");
}

$content = file_get_contents('path_to_js/' . $js . '.js');
preg_match_all('~_translate\(\'([^\']+)\'\)~', $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
    $content = str_replace($match[0], _($match[1]), $content);
}
echo $content;

so you have to include instead of yourscript.js the php file script.php?js=yourscript 所以你必须包括代替yourscript.js PHP文件script.php?js=yourscript

So my question is how to tell Poedit that he should read strings with _translate() inside a javascript-file? 所以我的问题是如何告诉Poedit他应该在javascript文件中使用_translate()读取字符串?

Add the _translate keyword (which is a non-default one for JS) to the keywords list and add the path to your .js file. _translate关键字(对于JS是非默认关键字)添加到关键字列表中,并将路径添加到您的.js文件。 That's it. 而已。 Poedit/xgettext supports JavaScript for quite some time now. Poedit / xgettext现在已经支持JavaScript相当一段时间了。

You misdiagnosed your problem, though, and Poedit's JS support isn't it. 但是,您误诊了您的问题,并非是Poedit的JS支持。 The real problem is that you're expecting Poedit/xgettext to parse a string literal that happens to contain some code. 真正的问题是您期望Poedit / xgettext解析一个恰好包含一些代码的字符串文字 "_translate('are_you_sure')" is not JavaScript code (that xgettext could find the _translate function in) in your example above — it's a string! 在上面的示例中, "_translate('are_you_sure')"不是JavaScript代码(xgettext可以在其​​中找到_translate函数),而是一个字符串! And you're asking the confirm() function to display it including the "_translate" bit, it makes no sense. 而且您要的是Confirm confirm()函数显示包含“ _translate”位的它,这没有任何意义。

You wouldn't have that problem with code like this: 这样的代码不会有这个问题:

msg = _translate('are_you_sure');
if (window.confirm(msg)) {
  location.href = this.href;
}

(Of course, you have to ensure client-side JS gettext deployment, including access to MO files, then, which is more work then translating strings on the PHP side.) (当然,您必须确保客户端 JS gettext部署,包括访问MO文件,然后再在PHP端翻译字符串,这是更多工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM