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.

Marius Burz
  • 4,555
  • 2
  • 18
  • 28

2 Answers2

0

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.

Václav Slavík
  • 6,445
  • 2
  • 28
  • 25
0

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
  • 140,109
  • 3
  • 41
  • 60
  • 1
    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 May 28 '09 at 20:29
  • 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 May 28 '09 at 21:35