1
votes

In our JS files we use the following format for Gettext translation:

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. The generated JS looks like this:

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.
Is there a way to parse the strings to be translated out of '!t[...]' using Poedit?

What would solve the problem is some sort of a Poedit parser that is regex based. 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). Then we use the following format in our JS files:

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.

2

2 Answers

0
votes

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.

0
votes

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