Translation gettext.po文件比较/处理文件之间的字符串
我想对一个包含多个不同应用程序文件的项目进行一些翻译。然而,为了使所有文件的内容一致,翻译工具可以加载一组.po文件,例如交叉检查文件中相同或类似的参考msgid字符串,以确保翻译效果,这将非常有用。如果引用相同,还可能允许一次性翻译多个文件/字符串Translation gettext.po文件比较/处理文件之间的字符串,translation,gettext,po,Translation,Gettext,Po,我想对一个包含多个不同应用程序文件的项目进行一些翻译。然而,为了使所有文件的内容一致,翻译工具可以加载一组.po文件,例如交叉检查文件中相同或类似的参考msgid字符串,以确保翻译效果,这将非常有用。如果引用相同,还可能允许一次性翻译多个文件/字符串 像这样的东西存在吗?我不得不为CiviCRM项目做同样的事情。我收到的一个建议是使用OpenRefine进行检查,它大概有一些工具可以找到类似的字符串,但我想用一些简单的东西来自动化这个过程,所以我编写了一个简短的脚本 公平的警告,这不是最有效的,
像这样的东西存在吗?我不得不为CiviCRM项目做同样的事情。我收到的一个建议是使用OpenRefine进行检查,它大概有一些工具可以找到类似的字符串,但我想用一些简单的东西来自动化这个过程,所以我编写了一个简短的脚本 公平的警告,这不是最有效的,而且在大项目上运行可能需要一段时间,我们在CiviCRM中有大约16000条字符串 供参考: 由于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文件上运行它,然后逐个循环遍历所有字符串
我对按字母顺序对字符串进行排序犹豫不决,但我发现不止几个例子中字符串前面有不正确的空格、拼写错误等
另一个可能的改进是首先检查字符串的长度,并跳过长度非常不同的字符串