我需要从msgstr开始检索msgid。原因是我有几种翻译,全部都是EN到SOME OTHER LANG,但是现在在当前安装中,我需要从SOME OTHER LANG转到EN。请注意,我也在WordPress中工作,但这也许并不重要。这里有几个类似的问题,但并不是我真正需要的。
那么有没有办法做到这一点?
答案 0 :(得分:1)
WordPress作为pomo
软件包的一部分附带了PO读写器。下面是一个非常简单的脚本,该脚本在周围交换msgid
和msgstr
字段并写出新文件。
正如评论中已经指出的那样,有几件事使此问题变得潜在:
向前-
<?php
require_once 'path/to/wp-includes/pomo/po.php';
$source_file = 'path/to/languages/old-file.po';
$target_file = 'path/to/languages/new-file.po';
// parse original message catalogue from source file
$source = new PO;
$source->import_from_file($source_file);
// prep target messages with a different language
$target = new PO;
$target->set_headers( $source->headers );
$target->set_header('Language', 'en_US');
$target->set_header('Language-Team', 'English from SOME OTHER LANG');
$target->set_header('Plural-Forms', 'nplurals=2; plural=n!=1;');
/* @var Translation_Entry $entry */
foreach( $source->entries as $entry ){
$reversed = clone $entry;
// swap msgid and msgstr (singular)
$reversed->singular = $entry->translations[0];
$reversed->translations[0] = $entry->singular;
// swap msgid_plural and msgstr[2] (plural)
if( $entry->is_plural ){
$reversed->plural = $entry->translations[1];
$reversed->translations[1] = $entry->plural;
}
// append target file with modified entry
$target->add_entry( $reversed );
}
// write final file back to disk
file_put_contents( $target_file, $target->export() );