11

我用德语的所有文本启动我的 PHP 应用程序,然后使用 gettext 提取所有字符串并将它们翻译成英语。
所以,现在我有一个 .po 文件,其中包含德语的所有 msgid 和英语的msgstrs。我想切换它们,以便我的源代码包含英语作为msgids,主要原因有两个:

  1. 更多的翻译人员会懂英语,因此只适合为他们提供带有英语msgids的文件。我总是可以在发出文件之前和收到文件之后切换文件,但是不。
  2. 如果内容文本也是英文,它将帮助我编写英文的对象和函数名称和注释。我想这样做,所以该项目对其他开源合作者更开放(更可能懂英语而不是德语)。

我可以手动执行此操作,这是我预计为它编写自动化例程(因为我对shell 脚本非常糟糕)而不是手动执行的任务。但我也预计会像往常一样鄙视每一分钟的手工计算机劳动(感觉像是矛盾的说法,对吗?)。

以前有人做过吗?我认为这将是一个常见问题,但找不到任何东西。非常感谢。

示例问题:

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

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

我想我会缩小问题的范围。.po 文件中的开关当然没有问题,它很简单

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

对我来说,问题是在解析 .po 文件时搜索我的项目文件夹文件_('$msgid')并进行替换的例程_('msgstr')(这可能甚至不是最优雅的方式,毕竟 .po 文件包含包含所有文件路径的注释,其中msgid 出现)。


在与akirk的回答混为一谈之后,我又遇到了一些问题。

  1. 因为我有_('xxx')_("xxx")电话的混合,我必须小心(不)转义。
    • msgids 和 msgstrs 中的双引号 " 必须不转义,但是斜线不能去掉,因为可能是双引号在 PHP 中也被转义了
    • 单引号在替换为 PHP 时必须进行转义,但随后也必须在 .po 文件中进行更改。对我来说幸运的是,单引号仅出现在英文文本中。
  2. msgids 和 msgstrs 可以有多行,那么它们看起来像这样
    msgid = ""
    "line 1\n"
    "line 2\n"
    msgstr = ""
    "line 1\n"
    "line 2\n"
  3. 目前当然会跳过复数形式,但在我的情况下,这不是问题
  4. poedit 想要删除似乎已成功切换的过时字符串,我不知道为什么在(许多)情况下会发生这种情况。

今晚我将不得不停止工作。尽管如此,使用解析器而不是 RegExps 似乎并不过分。

4

3 回答 3

5

我建立在 akirk 的答案之上,并想保留我在这里提出的答案,以防有人遇到同样的问题。这不是递归的,但当然很容易改变。随时评论改进,我将观看和编辑这篇文章。

$po = file_get_contents("locale/en_GB/LC_MESSAGES/messages.po");

$translations = array(); // german => english
$rawmsgids = array(); // find later
$msgidhits = array(); // record success
$msgstrs = array(); // find later

preg_match_all('/msgid "(.+)"\nmsgstr "(.+)"/', $po, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    $german = str_replace('\"','"',$match[1]); // unescape double quotes (could misfire if you escaped double quotes in PHP _("<a href=\"bla\">bla</a>") but in my case that was one case versus many)
    $english = str_replace('\"','"',$match[2]);


    $en_sq_e = str_replace("'","\'",$english); // escape single quotes

    $translations['_(\''. $german . '\''] = '_(\'' . $en_sq_e . '\'';
    $rawmsgids['_(\''. $german . '\''] = $match[1]; // find raw msgid with searchstr as key

    $translations['_("'. $match[1] . '"'] = '_("' . $match[2] . '"';
    $rawmsgids['_("'. $match[1] . '"'] = $match[1];

    $translations['__(\''. $german . '\''] = '__(\'' . $en_sq_e . '\'';
    $rawmsgids['__(\''. $german . '\''] = $match[1];

    $translations['__("'. $match[1] . '"'] = '__("' . $match[2] . '"';
    $rawmsgids['__("'. $match[1] . '"'] = $match[1];

    $msgstrs[$match[1]] = $match[2]; // msgid => msgstr
}


foreach (glob("*.php") as $file) {
    $code = file_get_contents($file);

    $filehits = 0; // how many replacements per file

    foreach($translations AS $msgid => $msgstr) {
        $hits = 0;
        $code = str_replace($msgid,$msgstr,$code,$hits);
        $filehits += $hits;

        if($hits!=0) $msgidhits[$rawmsgids[$msgid]] = 1; // this serves to record if the msgid was found in at least one incarnation
        elseif(!isset($msgidhits[$rawmsgids[$msgid]])) $msgidhits[$rawmsgids[$msgid]] = 0;
    }
    // file_put_contents($file, $code); // be careful to test this first before doing the actual replace (and do use a version control system!) 
    echo "$file : $filehits <br>"; 
    echo $code;
}
/* debug */ 
$found = array_keys($msgidhits, 1, true);
foreach($found AS $mid) echo $mid . " => " . $msgstrs[$mid] . "\n\n";

echo "Not Found: <br>";
$notfound = array_keys($msgidhits, 0, true);
foreach($notfound AS $mid) echo $mid . " => " . $msgstrs[$mid] . "\n\n";

/*
following steps are still needed:
    * convert plurals (ngettext)
    * convert multi-line msgids and msgstrs (format mentioned in question)
    * resolve uniqueness conflict (msgids are unique, msgstrs are not), so you may have duplicate msgids (poedit finds these)
*/
于 2011-01-18T11:20:58.527 回答
2

请参阅http://code.activestate.com/recipes/475109-regular-expression-for-python-string-literals/了解基于 python 的良好正则表达式,用于查找字符串文字,并考虑转义。虽然它是 python,但这对于多行字符串和其他极端情况可能非常有用。

请参阅http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/poswap.html了解 .po 文件的现成的、开箱即用的基础语言交换器。

例如,以下命令行会将基于德语的西班牙语翻译转换为基于英语的西班牙语翻译。在开始转换之前,您只需确保您的新基础语言(英语)已 100% 翻译:

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

最后将英语 po 文件交换为德语 po 文件,使用 swappo: http ://manpages.ubuntu.com/manpages/hardy/man1/swappo.1.html

交换文件后,可能需要对生成的文件进行一些手动优化。例如,标题可能会损坏,并且可能会出现一些重复的文本。

于 2011-04-08T16:41:45.183 回答
1

因此,如果我对您的理解正确,您希望将所有德语 gettext 调用替换为英语调用。要替换目录中的内容,可以使用类似这样的方法。

$po = file_get_contents("translation.pot");
$translations = array(); // german => english
preg_match_all('/msgid "(.+)"\nmsgstr "(.+)"/', $po, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
    $translations['_("'. $match[1] . '")'] = '_("' . $match[2] . '")';
    $translations['_(\''. $match[1] . '\')'] = '_(\'' . $match[2] . '\')';
}
foreach (glob("*.php") as $file) {
    $code = file_get_contents($file);
    $code = str_replace(array_keys($translations), array_values($translations), $code);
    //file_put_contents($file, $code);
    echo $code; // be careful to test this first before doing the actual replace (and do use a version control system!)
}
于 2011-01-17T14:23:19.127 回答