From 42fa789ce0ea2df5240c17e0ea35e2a1f1a6742c Mon Sep 17 00:00:00 2001 From: Filip Komar Date: Fri, 7 Mar 2014 22:01:48 +0000 Subject: first steps as a preparation of l10n report (only diff.php for now - 3 exceptions) to gettext system --- langs/lib.php | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 4 deletions(-) (limited to 'langs/lib.php') diff --git a/langs/lib.php b/langs/lib.php index f7422167f..05740418d 100644 --- a/langs/lib.php +++ b/langs/lib.php @@ -21,13 +21,12 @@ include '../langs.inc.php'; * - missing strings (in $a, not in $b) * - extra strings (in $b, not in $a) * - untranslated strings (same in $a and $b, or empty in $b) + * - duplicate strings in $b * * @param string $a file name * @param string $b file name * * @return array - * - * @todo some strings may be left untranslated on purpose */ function _lang_diff($a, $b) { @@ -70,7 +69,60 @@ function _lang_diff($a, $b) ); } -function _lang_diff_stats($a, $b) +/** + * Diff pot and po files, to get: + * - source (pot) strings count + * - missing strings in target + * - untranslated strings in target + * - empty array for extra and duplicate strings for backward compatibility + * + * @param string $locale locale name ('sl') + * @param string $resource file name ('about/license') + * + * @return array +*/ +function _po_diff($locale, $resource) +{ + $source_l = read_translation_file('en', $resource); + $target_l = read_translation_file($locale, $resource); + + $pot_strings = array(); + $untrans = array(); + $missing = array(); + + foreach ($source_l as $escaped_string => $subarray) { + if ($escaped_string == $subarray["msgid"]) { // filter out header + $pot_strings[$escaped_string] = $subarray["msgid"]; + } + } + + foreach ($target_l as $escaped_string => $subarray) { + if ($escaped_string == $subarray["msgid"]) { // filter out header + $po_strings[$escaped_string] = $subarray["msgstr"][0]; + } + } + + foreach ($pot_strings as $escaped_string => $translated_string) { + if (isset($po_strings[$escaped_string])) { + if (empty($po_strings[$escaped_string])) { + $untrans[] = $escaped_string; + } + } else { + $missing[] = $escaped_string; + } + } + + return array( + 'a' => count($pot_strings), // # of original strings +// 'b' => count($po_strings), // # of target strings + 'missing' => $missing, // probably could be empty array() of missing strings + 'notrans' => $untrans, // array of untranslated strings + 'extra' => array(), // could be empty array() of unused (old) strings + 'dup_str' => array(), // could be empty array() of duplicate strings + ); +} + +/*function _lang_diff_stats($a, $b) { $diff = _lang_diff($a, $b); @@ -81,7 +133,7 @@ function _lang_diff_stats($a, $b) $diff['correct'] = $diff['b'] - $diff['notrans'] - $diff['missing']; return $diff; -} +} /**/ if ( ! function_exists('glob_recursive')) { @@ -111,12 +163,51 @@ if ( ! function_exists('glob_recursive')) } } +/** + * Create 'sl/about/license.sl.lang' + * from 'en/about/license.en.lang' + * + * @param string $s file name with path + * @param string $l locale name + * + * @return string +*/ function _lang_file_switch($s, $l) { $s = str_replace('en.lang', $l . '.lang', $s); return str_replace('en/', $l . '/', $s); } +/** + * Create 'sl/about/license.po' + * from 'en/about/license.pot' + * + * @param string $s file name with path + * @param string $l locale name + * + * @return string +*/ +function _po_file_switch($s, $l) +{ + $s = str_replace('.pot', '.po', $s); + return str_replace('en/', $l . '/', $s); +} + +/** + * Create 'about/license' + * from 'en/about/license.pot' or 'en/about/license.en.lang' + * + * @param string $source_file file name with path + * @param string $extension file extension to remove + * + * @return string +*/ +function _extract_resource($source_file, $extension = '.pot') +{ + $resource = str_replace($extension, '', $source_file); + return str_replace('en/', '', $resource); +} + function get_lang_references() { return glob_recursive('en/*', GLOB_MARK); -- cgit v1.2.1