From ac88f67dbe4eb625ebcf0c2d4b26f33123772ae4 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2016 20:18:04 +0200 Subject: expand _po_diff funcion to include 3-way comparison + sync it to changed structure in l10n array --- langs/lib.php | 115 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 52 insertions(+), 63 deletions(-) (limited to 'langs/lib.php') diff --git a/langs/lib.php b/langs/lib.php index ddbfff733..dc4dbd8a4 100644 --- a/langs/lib.php +++ b/langs/lib.php @@ -80,10 +80,11 @@ function _lang_diff($a, $b) * @param string $resource file name ('about/license') * @param array $source_l array with source file strings (to avoid duplicated parsing) * @param string $path directly passed path for nonlocal files + * @param array $compared array with strings for comparing * * @return array */ -function _po_diff($locale, $resource, $source_l = NULL, $path = NULL) +function _po_diff($locale, $resource, $source_l = NULL, $path = NULL, $compared = NULL) { if (is_null($path)) { if (NULL == $source_l) { @@ -94,6 +95,9 @@ function _po_diff($locale, $resource, $source_l = NULL, $path = NULL) if (NULL == $source_l) { $source_path_filename = sprintf('%s/%s.pot', $path, $resource); $source_l = phpmo_parse_po_file($source_path_filename); + if (FALSE == $source_l) { + $source_l = array(array()); + } } if ('en' == $locale) { $target_l = $source_l; @@ -101,84 +105,68 @@ function _po_diff($locale, $resource, $source_l = NULL, $path = NULL) $locale = locale_hyphen_underscore($locale, true); $target_path_filename = sprintf('%s/%s.po', $path, $locale); $target_l = phpmo_parse_po_file($target_path_filename); + if (FALSE == $target_l) { + $target_l = array(array()); + } } } + unset($source_l['']); // filter out header + unset($target_l['']); // filter out header - $pot_strings = array(); - $untrans = array(); - $fuzzy_or_missing = array(); + // process $source_l, $target_l and $compared $num_of_original_strings = 0; - - if (FALSE != $source_l) { - foreach ($source_l as $escaped_string => $subarray) { - if (!empty($escaped_string)) { // filter out header - $msgctxt = array(); - foreach ($subarray[0] as $key => $unused) { - if (isset($subarray["msgctxt"][$key])) { - $msgctxt[$key] = $subarray["msgctxt"][$key]; - } else { - $msgctxt[$key] = TRUE; - } - $num_of_original_strings++; - } - $pot_strings[$escaped_string] = $msgctxt; + $fuzzy_or_missing = array(); + $untrans = array(); + $differences = array(); + foreach ($source_l as $msgid => $subarray) { + foreach ($subarray as $context_or_num => $msgstr_subarray) { + $num_of_original_strings++; // Note: count plural msgid strings also as one + if (array_key_exists('msgid_plural', $msgstr_subarray)) { + $msgid_plural = $msgstr_subarray['msgid_plural']; + } else { + $msgid_plural = NULL; } - } - } - $fuzzy_or_missing = $pot_strings; - $untrans = $pot_strings; - - if (FALSE != $target_l) { - foreach ($target_l as $escaped_string => $subarray) { - if (!empty($escaped_string)) { // filter out header - foreach ($subarray[0] as $key => $msgstr) { - // remove present string - unset($fuzzy_or_missing[$escaped_string][$key]); - if (!empty($msgstr)) { - // remove translated string - if(is_array($msgstr)) { // are there any plural strings present? - $erase_plural_key = TRUE; - foreach ($msgstr as $nplural) { - if (empty($nplural)) { - $erase_plural_key = FALSE; - } - } - // if all plural keys are translated, remove msgstr for that msgid - if ($erase_plural_key) { - unset($untrans[$escaped_string][$key]); - } - } else { - unset($untrans[$escaped_string][$key]); + // process $target_l translation + if (!isset($target_l[$msgid])) { + // adding fuzzy or missing string or plurals + $fuzzy_or_missing[$msgid] = $subarray; + $msgstr_target_l = NULL; + } else { + $msgstr_target_l = $target_l[$msgid][$context_or_num]; + // are there any plurals untranslated? + $untranslated_plural_target_l = FALSE; + if (!is_null($msgid_plural)) { + // filter out msgid_plural + unset($msgstr_target_l['msgid_plural']); + foreach ($msgstr_target_l as $nplural) { + if (empty($nplural)) { + $untranslated_plural_target_l = TRUE; } } } + // untranslated string + if ($untranslated_plural_target_l || empty($msgstr_target_l[0])) { + $untrans[$msgid] = $subarray; // adding untranslated string or plurals + } } - } - } - $arrays = array($fuzzy_or_missing, $untrans); - foreach ($arrays as &$current_array) { - $strings = array(); - $cnt = 0; - foreach ($current_array as $escaped_string => $msgctxt) { - if(!empty($msgctxt)) { - $keys = array_keys($msgctxt); - if(is_numeric($keys[0])) { - $index = $cnt; + // process $compared translation + if (!is_null($compared)) { + unset($compared['']); // filter out header + // if there is a translation for this string in $compared + if (isset($compared[$msgid][$context_or_num])) { + $msgstr_compared = $compared[$msgid][$context_or_num]; } else { - $index = $keys[0]; + $msgstr_compared[0] = ''; + } + if ($msgstr_target_l != $msgstr_compared) { + $differences[$msgid]['target_l'][$context_or_num] = $msgstr_target_l; + $differences[$msgid]['compared'][$context_or_num] = $msgstr_compared; } - $strings[$index] = $escaped_string; - $cnt++; } } - $current_array = $strings; } - unset($current_array); - - list($fuzzy_or_missing, $untrans) = $arrays; - $untrans = array_diff($untrans, $fuzzy_or_missing); return array( 'a' => $num_of_original_strings, // # of original strings @@ -186,6 +174,7 @@ function _po_diff($locale, $resource, $source_l = NULL, $path = NULL) 'source_strings' => $source_l, // array of original strings 'fuzzy_or_missing' => $fuzzy_or_missing, // array of fuzzy or missing strings 'notrans' => $untrans, // array of untranslated strings + 'differences' => $differences, // array of different strings from two translations 'extra' => array(), 'dup_str' => array(), ); -- cgit v1.2.1