From 387d067e53211d538f545ff002227a989c263fa2 Mon Sep 17 00:00:00 2001 From: filip Date: Mon, 25 Jul 2016 11:01:36 +0200 Subject: added option for passing translation directly as an array for parsing l10n files --- php-mo.php | 52 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/php-mo.php b/php-mo.php index 6f635c2..eedc34d 100644 --- a/php-mo.php +++ b/php-mo.php @@ -11,6 +11,7 @@ * don't add last translation if it's fuzzy * 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 * * it returns array of translations: * $dictionary = phpmo_parse_po_file('input.po'); @@ -80,12 +81,20 @@ function phpmo_clean_helper($x) { /* Parse gettext .po files. */ /* @link http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files */ -function phpmo_parse_po_file($in) { - // read .po file - $fh = @fopen($in, 'r'); - if ($fh === false) { - // Could not open file resource - return false; +function phpmo_parse_po_file($in, $translation_in_file = true) { + if ($translation_in_file === true) { + // read .po file + $fh = @fopen($in, 'r'); + if ($fh === false) { + // Could not open file resource + return false; + } + } else { + if (!is_array($in) || empty($in)) { + // given translation not valid + return false; + } + $translation_in_file = false; } // results array @@ -95,9 +104,25 @@ function phpmo_parse_po_file($in) { // state $state = null; $fuzzy = false; + $parse_condition = true; // iterate over lines - while(($line = fgets($fh, 65536)) !== false) { + while($parse_condition) { + // output and test line for translation in file + if ($translation_in_file) { + if (($line = fgets($fh, 65536)) === false) { + $parse_condition = false; + continue; + } + // output and test line for translation in array + } else { + $line = current($in); + $end_array_test = next($in); + if (false === $end_array_test) { + $parse_condition = false; + continue; + } + } $line = trim($line); if ($line === '') continue; @@ -107,7 +132,7 @@ function phpmo_parse_po_file($in) { $data = (isset($array_of_splited_string[1]) ? $array_of_splited_string[1] : ''); switch ($key) { case '#,' : // flag... - case '#' : // translator-comments + case '#' : // translator-comments case '#.' : // extracted-comments case '#:' : // reference... case '#|' : // msgid previous-untranslated-string @@ -171,20 +196,23 @@ function phpmo_parse_po_file($in) { break; default : // parse error - fclose($fh); + if ($translation_in_file) { + fclose($fh); + } return FALSE; } } break; } } - fclose($fh); + if ($translation_in_file) { + fclose($fh); + } // add final entry if ($state == 'msgstr') if (!$fuzzy) $hash[] = $temp; - // Cleanup data, merge multiline entries, reindex hash for ksort $temp = $hash; $hash = array (); @@ -207,7 +235,7 @@ function phpmo_parse_po_file($in) { } 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']; + $msgstr_plural = $entry['msgstr']; $entry['msgstr'] = array(); $entry['msgstr'][] = $msgstr_plural; } -- cgit v1.2.1