简体   繁体   English

基于Poedit正则表达式的解析器?

[英]Poedit regex based parser?

In our JS files we use the following format for Gettext translation: 在我们的JS文件中,我们使用以下格式进行Gettext翻译:

var str1 = '!t[The text that should be translated]';
var str2 = '!t[Some more text]';

This JS files will be parsed using PHP and the parsed strings get translated via Zend Framework Zend_Translate. 将使用PHP解析此JS文件,并通过Zend Framework Zend_Translate转换解析后的字符串。 The generated JS looks like this: 生成的JS看起来像这样:

var str1 = 'The text that should be translated';
var str2 = 'Some more text';

For extracting the strings to be translated and for translating our PHP files we use Poedit, it works very well. 为了提取要翻译的字符串并翻译我们的PHP文件,我们使用Poedit,它可以很好地工作。
Is there a way to parse the strings to be translated out of '!t[...]' using Poedit? 有没有一种方法可以使用Poedit从'!t[...]'解析出要翻译的字符串?

What would solve the problem is some sort of a Poedit parser that is regex based. 解决该问题的方法是某种基于正则表达式的Poedit解析器。 Is there any such parser? 有这样的解析器吗?

As an alternative, we could define a source code parser based on xgettext with the language PHP as parameter(you have to do it because xgettext doesn't know about .js files and it treats them a C files). 作为替代方案,我们可以基于xgettext定义源代码解析器,并将语言PHP作为参数(您必须这样做,因为xgettext不了解.js文件,并且将它们视为C文件)。 Then we use the following format in our JS files: 然后,我们在JS文件中使用以下格式:

var str1 = '<?=_t("The text that should be translated")?>';
var str2 = '<?=_t("Some more text")?>';

Needless to say, it's really uncool to use code that looks like php all over the place just to be able to parse the strings with Poedit. 不用说,在各处使用看起来像php的代码只是真的很酷,以便能够使用Poedit解析字符串。

Poedit and xgettext do support JavaScript now (I honestly don't know if it was the case in 2009, but I think it wasn't), but they don't support string literals with custom markup in them. Poedit和xgettext现在确实支持JavaScript(老实说,我不知道2009年是否如此,但我认为事实并非如此),但是它们不支持带有自定义标记的字符串文字。 So you still can't extract from 所以你仍然无法从中提取

var str1 = '!t[The text that should be translated]';

but you can easily extract using a helper function: 但是您可以使用辅助函数轻松提取:

var str1 = t('The text that should be translated');

if you just add t as a keyword in Poedit. 如果您只是在Poedit中将t添加为关键字。

A regexp that matches your strings 与您的字符串匹配的正则表达式

 $translated = preg_replace('/[\'"]\!t\[(.+)\][\'"]/e', 'translate_function('\\2')', $str);

I don't know if the \\2 should be replaced by \\1 or \\3, you solution is the "e" modifier provided by the PCRE regex engine. 我不知道应将\\ 2替换为\\ 1还是\\ 3,您解决的是PCRE regex引擎提供的“ e”修饰符。

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

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