Search code examples
phpregexparsinggettextpoedit

Switch gettext translated language with original language


I started my PHP application with all text in German, then used gettext to extract all strings and translate them to English.
So, now I have a .po file with all msgids in German and msgstrs in English. I want to switch them, so that my source code contains the English as msgids for two main reasons:

  1. More translators will know English, so it is only appropriate to serve them up a file with msgids in English. I could always switch the file before I give it out and after I receive it, but naaah.
  2. It would help me to write English object & function names and comments if the content text was also English. I'd like to do that, so the project is more open to other Open Source collaborators (more likely to know English than German).

I could do this manually and this is the sort of task where I anticipate it will take me more time to write an automated routine for it (because I'm very bad with shell scripts) than do it by hand. But I also anticipate despising every minute of manual computer labour (feels like an oxymoron, right?) like I always do.

Has someone done this before? I figured this would be a common problem, but couldn't find anything. Many thanks ahead.

Sample Problem:

<title><?=_('Routinen')?></title>

#: /users/ruben/sites/v/routinen.php:43
msgid "Routinen"
msgstr "Routines"

I thought I'd narrow the problem down. The switch in the .po-file is no issue of course, it is as simple as

preg_replace('/msgid "(.+)"\nmsgstr "(.+)"/', '/msgid "$2"\nmsgstr "$1"/', $str);

The problem for me is the routine that searches my project folder files for _('$msgid') and substitutes _('msgstr') while parsing the .po-file (which is probably not even the most elegant way, after all the .po-file contains comments which contain all file paths where the msgid occurs).


After fooling around with akirk's answer a little, I ran into some more problems.

  1. Because I have a mixture of _('xxx') and _("xxx") calls, I have to be careful about (un)escaping.
    • Double quotes " in msgids and msgstrs have to be unescaped, but the slashes can't be stripped, because it may be that the double quote was also escaped in PHP
    • Single quotes have to be escaped when they're replaced into PHP, but then they also have to be changed in the .po-file. Luckily for me, single quotes only appear in English text.
  2. msgids and msgstrs can have multiple lines, then they look like this
    msgid = ""
    "line 1\n"
    "line 2\n"
    msgstr = ""
    "line 1\n"
    "line 2\n"
  3. plural forms are of course skipped at the moment, but in my case that's not an issue
  4. poedit wants to remove strings as obsolete that seem successfully switched and I have no idea why this happens in (many) cases.

I'll have to stop working on this for tonight. Still it seems using the parser instead of RegExps wouldn't be overkill.


Solution

  • See http://code.activestate.com/recipes/475109-regular-expression-for-python-string-literals/ for a good python-based regular expression for finding string literals, taking escapes into account. Although it's python, this might be quite good for multiline strings and other corner cases.

    See http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/poswap.html for a ready, out-of-the-box base language swapper for .po files.

    For instance, the following command line will convert german-based spanish translation to english-based spanish translation. You just have to ensure that your new base language (english) is 100% translated before starting conversion:

    poswap -i de-en.po -t de-es.po -o en-es.po
    

    And finally to swap english po file to german po file, use swappo: http://manpages.ubuntu.com/manpages/hardy/man1/swappo.1.html

    After swapping files, some manual polishing of resultant files might be required. For instance headers might be broken and some duplicate texts might occur.