From 33cf39f3a2b103b6768147ec99b20f4822dc92a6 Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 26 Jul 2016 22:54:46 +0200 Subject: changed structure of resulting l10n array --- lib.php | 4 ++-- php-mo.php | 78 +++++++++++++++++++++++++++++--------------------------------- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/lib.php b/lib.php index f253c58..bcba8e8 100644 --- a/lib.php +++ b/lib.php @@ -216,8 +216,8 @@ class l10n foreach ($dictionary as $key => $value) { if ($key != '') { - if ($value['msgstr'][0] != '') { - $_t[trim($key)] = trim($value['msgstr'][0]); + if ($value[0][0] != '') { + $_t[trim($key)] = trim($value[0][0]); } else { $_t[trim($key)] = trim($key); } diff --git a/php-mo.php b/php-mo.php index eedc34d..6c1b901 100644 --- a/php-mo.php +++ b/php-mo.php @@ -12,44 +12,47 @@ * removed unedeed phpmo_convert and phpmo_write_mo_file * more info on https://bugs.mageia.org/show_bug.cgi?id=14899 * added option for passing translation directly as an array +* changed structure of resulting array * * it returns array of translations: * $dictionary = phpmo_parse_po_file('input.po'); * * $dictionary = array ( * '' => array ( -* 'msgid' => '', -* 'msgstr' => array ( -* 0 => 'Project-Id-Version: ...', +* 0 => array ( +* 0 => 'Project-Id-Version: ...', * ), * ), -* 'string for translation 1' => array ( -* 'msgid' => 'string for translation 1', -* 'msgstr' => array ( -* 0 => 'translated string 1', +* 'msgid string for translation 1' => array ( // msgid +* 0 => array ( +* 0 => 'translated string 1', // msgstr * ), * ), -* 'string for translation 2' => array ( -* 'msgid' => 'string for translation 2', -* 'msgstr' => array ( -* 0 => 'translated string 2', -* 1 => 'translated string 2 but different context', -* 2 => 'another translated string 2 with different context', +* 'msgid string for translation 2' => array ( // msgid +* 0 => array ( +* 0 => 'translated string 2', // msgstr * ), -* 'msgctxt' => array ( -* 1 => 'context explanation for msgstr[1]', -* 2 => 'context explanation for msgstr[2]', +* 'context explanation for msgstr[1]' => array ( // msgctxt +* 0 => 'translated string 2 but different context', // msgstr +* ), +* 'context explanation for msgstr[2]' => array ( // msgctxt +* 0 => 'another translated string 2 with different context', // msgstr +* ), +* ), +* 'msgid string for translation 3' => array ( // msgid +* 0 => array ( +* 'msgid_plural' => 'plural string for translation 3', // msgid_plural +* 0 => 'translated string 3 for nplurals 0', // msgstr[0] +* 1 => 'translated string 3 for nplurals 1', // msgstr[1] +* 2 => 'translated string 3 for nplurals 2', // msgstr[2] * ), * ), -* 'string for translation 3' => array ( -* 'msgid' => 'string for translation 3', -* 'msgid_plural' => 'plural string for translation 3', -* 'msgstr' => array ( -* 0 => array ( -* 0 => 'translated string 3 for nplurals 0', -* 1 => 'translated string 3 for nplurals 1', -* 2 => 'translated string 3 for nplurals 2', -* ), +* 'msgid string for translation 4' => array ( // msgid +* 'context explanation for msgstr[0]' => array ( // msgctxt +* 'msgid_plural' => 'plural string for translation 4', // msgid_plural +* 0 => 'translated string 4 for nplurals 0', // msgstr[0] +* 1 => 'translated string 4 for nplurals 1', // msgstr[1] +* 2 => 'translated string 4 for nplurals 2', // msgstr[2] * ), * ), * ); @@ -213,6 +216,7 @@ function phpmo_parse_po_file($in, $translation_in_file = true) { if ($state == 'msgstr') if (!$fuzzy) $hash[] = $temp; + // Cleanup data, merge multiline entries, reindex hash for ksort $temp = $hash; $hash = array (); @@ -224,24 +228,14 @@ function phpmo_parse_po_file($in, $translation_in_file = true) { return FALSE; } } - // if there's already same msgid we need to store it there - if (array_key_exists($entry['msgid'], $hash)) { - $index = count($hash[$entry['msgid']]['msgstr']); - $hash[$entry['msgid']]['msgstr'][$index] = $entry['msgstr'][0]; - // is different msgctxt present also? - if (array_key_exists('msgctxt', $entry)) { - $hash[$entry['msgid']]['msgctxt'][$index] = $entry['msgctxt'][0]; - } - } else { - // if msgid_plural exists we need to store msgstr to subarray for proper counting of l10n completion - if (array_key_exists('msgid_plural', $entry)) { - $msgstr_plural = $entry['msgstr']; - $entry['msgstr'] = array(); - $entry['msgstr'][] = $msgstr_plural; - } - $hash[$entry['msgid']] = $entry; + + // add msgctxt if present + $msgctxt = (array_key_exists('msgctxt', $entry) ? $entry['msgctxt'][0] : 0); + // and also msgid_plural + if (array_key_exists('msgid_plural', $entry)) { + $entry['msgstr']['msgid_plural'] = $entry['msgid_plural']; } + $hash[$entry['msgid']][$msgctxt] = $entry['msgstr']; } - return $hash; } -- cgit v1.2.1