views:

474

answers:

1

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.

A: 

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.

Fabien Ménager
Sorry Fabien, I doubt you understood what I'm looking for.As the title says, it's about parsing with Poedit the strings to be translated. We need this in order to automatically extract the strings to be translated from our JS files. Poedit does this for the languages it supports easily. It works perfectly with PHP files, but it doesn't support JS. Even if it did, the format that we use would not be supported out of the box. What we really need is some sort of a Regex based source code parser for Poedit.PS: I don't really understand what your solution solves.
Marius Burz
Okay, I thought you used PHP to parse the javascript files and translates the tokens using php-gettext.I think this page could help you what you want : http://www.tine20.org/wiki/index.php/Developers/Concepts/Translation_Howto
Fabien Ménager