【问题标题】:gettext .po file comparing/working with strings between filesgettext .po 文件比较/处理文件之间的字符串
【发布时间】:2014-11-23 16:54:48
【问题描述】:

我想对一个包含不同应用程序的多个文件的项目进行一些翻译。然而,为了轻松地使所有文件的内容保持一致,使用可以加载一堆 .po 文件的翻译工具会很有用,例如交叉检查文件是否有相同或相似的参考 msgid 字符串以确保翻译。如果引用相同,也许还允许一次性翻译多个文件/字符串。

这样的东西存在吗..?

【问题讨论】:

    标签: translation gettext po


    【解决方案1】:

    我必须为一个项目 (CiviCRM) 做同样的事情。我收到的一个建议是检查 OpenRefine,它大概有一些工具可以找到类似的字符串,但我想用一些简单的东西来自动化这个过程,所以我写了一个简短的脚本。

    公平的警告,这不是最有效的,而且在大型项目上运行可能需要一段时间(我们在 CiviCRM 中有大约 16000 个字符串)。

    供参考: https://github.com/civicrm/l10n/blob/master/bin/find-similar-strings.php

    而且由于 SO 不喜欢链接作为答案,请点击此处了解更多详情:

    #!/usr/bin/php
    <?php
    
    /**
    * Reads from STDIN and finds similar-looking strings.
    *
    * Usage:
    * cat *.pot | ../bin/find-similar-strings.php
    *
    * Context:
    * http://forum.civicrm.org/index.php/topic,34805.0.html
    */
    
    // Default match threshold is 90% match.
    $threshold = (! empty($argv[1]) ? $argv[1] : 90);
    
    // Read all input from stdin.
    $src = file_get_contents("php://stdin");
    
    // http://stackoverflow.com/a/1070937/2387700
    // Extract all "msgid" strings (they can be multi-line).
    preg_match_all('/msgid\s+\"([^\"]*)\"/', $src, $matches);
    $msgids = $matches[1];
    
    // Sort the strings alphabetically, to make them easier to compare.
    // sort($msgids);
    foreach ($msgids as $key1 => $msgid1) {
      foreach ($msgids as $key2 => $msgid2) {
        $percent = 0;
        if ($msgid1 && $msgid2 && $msgid1 != $msgid2) {
          if (similar_text($msgid1, $msgid2, $percent)) {
            if ($percent > $threshold) {
              $percent = (int) $percent;
              echo "$msgid1 [$percent %]\n";
              echo "$msgid2 \n\n";
            }
          }
        }
      }
    
      // To avoid going through the strings twice, we unset the string
      // si that the inner-loop goes faster.
      unset($msgids[$key1]);
    }
    

    这将加载 .pot 文件(源字符串,但我想您也可以在 .po 文件上运行它),并一一循环遍历所有字符串。

    我一直在犹豫是否按字母顺序对字符串进行排序,但我发现不少情况下,字符串前面的空格不正确、拼写错误等。

    另一个可能的改进是首先检查字符串的长度,然后跳过长度非常不同的字符串。

    【讨论】:

      猜你喜欢
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      相关资源
      最近更新 更多