0

I'm in need to start from the msgstr to retrieve the msgid. The reason is that I've got several translations, all EN to SOME OTHER LANG, but now in the current installation I need to go back from SOME OTHER LANG to EN. Note that I'm also working in WordPress, maybe this is not important though. There are a couple of similar questions here but not exactly what I need.

So is there a way to accomplish this?

Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118
  • 1. Since there's no requirement for `msgstr`s to be unique, there may be more than one `msgid` that matches a `msgstr`. 2. You could use any library that can parse PO files (or write your own) to get an array of entries and search through them. – deceze Jul 05 '18 at 07:24

1 Answers1

1

WordPress ships with a PO reader and writer as part of the pomo package. Below is a pretty simple script that swaps the msgid and msgstr fields around and writes out a new file.

As already pointed out in the comments, there are several things that make this potentially problematic:

  1. Your target strings must all be unique (and not empty)
  2. If you have message context, this will stay in the original language.
  3. Your original language must have only two plural forms.

Onward -

<?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() );
Tim
  • 8,036
  • 2
  • 36
  • 52