From 86229ab1304d917d60170a4f5e9aaff0682eda2b Mon Sep 17 00:00:00 2001 From: Filip Komar Date: Sun, 18 May 2014 20:16:47 +0000 Subject: move php-mo.php for better compatibility with mognase + improved navigation in l10n report & co. --- _nav/html/index.php | 1 - _nav/langs/php-mo.php | 154 -------------------------------------------------- _nav/lib.php | 2 + _nav/php-mo.php | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++ langs.inc.php | 2 +- langs.php | 1 - langs/diff.php | 2 +- langs/lib.php | 2 +- langs/missing.php | 2 +- langs/report.php | 1 + langs/report_test.php | 1 + 11 files changed, 162 insertions(+), 160 deletions(-) delete mode 100644 _nav/langs/php-mo.php create mode 100644 _nav/php-mo.php diff --git a/_nav/html/index.php b/_nav/html/index.php index 0bde089bb..8c8ac690c 100644 --- a/_nav/html/index.php +++ b/_nav/html/index.php @@ -44,7 +44,6 @@ $wrap = isset($_GET['w']) ? true : false; require 'conf.php'; include '../lib.php'; -require_once('../langs/php-mo.php'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET'); diff --git a/_nav/langs/php-mo.php b/_nav/langs/php-mo.php deleted file mode 100644 index 470ce80e8..000000000 --- a/_nav/langs/php-mo.php +++ /dev/null @@ -1,154 +0,0 @@ - - * - * NB: - * - If no $output_file specified, output filename is same as $input_file (but .mo) - * - Returns true/false for success/failure - * - No warranty, but if it breaks, please let me know - * - * More info: - * https://github.com/josscrowcroft/php.mo - * - * Based on php-msgfmt by Matthias Bauer (Copyright © 2007), a command-line PHP tool - * for converting .po files to .mo. - * (http://wordpress-soc-2007.googlecode.com/svn/trunk/moeffju/php-msgfmt/msgfmt.php) - * - * License: GPL v3 http://www.opensource.org/licenses/gpl-3.0.html - */ - -function phpmo_clean_helper($x) { - if (is_array($x)) { - foreach ($x as $k => $v) { - $x[$k] = phpmo_clean_helper($v); - } - } else { - if ($x[0] == '"') - $x = substr($x, 1, -1); - $x = str_replace("\"\n\"", '', $x); - $x = str_replace('$', '\\$', $x); - } - return $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; - } - - // results array - $hash = array (); - // temporary array - $temp = array (); - // state - $state = null; - $fuzzy = false; - - // iterate over lines - while(($line = fgets($fh, 65536)) !== false) { - $line = trim($line); - if ($line === '') { - // save stored entry on empty line - // block moved to fix "fuzzy flag first line" bug which didn't saved previous proper string at all - if (sizeof($temp) && array_key_exists('msgid', $temp) && array_key_exists('msgstr', $temp)) { - if (!$fuzzy) - $hash[] = $temp; - $temp = array (); - $state = null; - $fuzzy = false; - } - continue; - } - $array_of_splited_string = preg_split('/\s/', $line, 2); - $key = $array_of_splited_string[0]; - $data = (isset($array_of_splited_string[1]) ? $array_of_splited_string[1] : ''); - - switch ($key) { - case '#,' : // flag... - $fuzzy = in_array('fuzzy', preg_split('/,\s*/', $data)); - case '#' : // translator-comments - case '#.' : // extracted-comments - case '#:' : // reference... - case '#|' : // msgid previous-untranslated-string - break; - case '#~' : // commented-unused-string - $temp = array (); - $state = null; - $fuzzy = false; - break; - case 'msgctxt' : - // context - case 'msgid' : - // untranslated-string - case 'msgid_plural' : - // untranslated-string-plural - $state = $key; - $temp[$state] = $data; - break; - case 'msgstr' : - // translated-string - $state = 'msgstr'; - $temp[$state][] = $data; - break; - default : - if (strpos($key, 'msgstr[') !== FALSE) { - // translated-string-case-n - $state = 'msgstr'; - $temp[$state][] = $data; - } else { - // continued lines - switch ($state) { - case 'msgctxt' : - case 'msgid' : - case 'msgid_plural' : - $temp[$state] .= "\n" . $line; - break; - case 'msgstr' : - $temp[$state][sizeof($temp[$state]) - 1] .= "\n" . $line; - break; - default : - // parse error - fclose($fh); - return FALSE; - } - } - break; - } - } - fclose($fh); - - // add final entry - if ($state == 'msgstr') - $hash[] = $temp; - - // Cleanup data, merge multiline entries, reindex hash for ksort - $temp = $hash; - $hash = array (); - foreach ($temp as $entry) { - foreach ($entry as & $v) { - $v = phpmo_clean_helper($v); - if ($v === FALSE) { - // parse error - return FALSE; - } - } - $hash[$entry['msgid']] = $entry; - } - - return $hash; -} - -?> \ No newline at end of file diff --git a/_nav/lib.php b/_nav/lib.php index 55151034c..0cf33b47a 100644 --- a/_nav/lib.php +++ b/_nav/lib.php @@ -18,6 +18,8 @@ */ // definition +require_once('php-mo.php'); + class NCache { function __construct() { } diff --git a/_nav/php-mo.php b/_nav/php-mo.php new file mode 100644 index 000000000..470ce80e8 --- /dev/null +++ b/_nav/php-mo.php @@ -0,0 +1,154 @@ + + * + * NB: + * - If no $output_file specified, output filename is same as $input_file (but .mo) + * - Returns true/false for success/failure + * - No warranty, but if it breaks, please let me know + * + * More info: + * https://github.com/josscrowcroft/php.mo + * + * Based on php-msgfmt by Matthias Bauer (Copyright © 2007), a command-line PHP tool + * for converting .po files to .mo. + * (http://wordpress-soc-2007.googlecode.com/svn/trunk/moeffju/php-msgfmt/msgfmt.php) + * + * License: GPL v3 http://www.opensource.org/licenses/gpl-3.0.html + */ + +function phpmo_clean_helper($x) { + if (is_array($x)) { + foreach ($x as $k => $v) { + $x[$k] = phpmo_clean_helper($v); + } + } else { + if ($x[0] == '"') + $x = substr($x, 1, -1); + $x = str_replace("\"\n\"", '', $x); + $x = str_replace('$', '\\$', $x); + } + return $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; + } + + // results array + $hash = array (); + // temporary array + $temp = array (); + // state + $state = null; + $fuzzy = false; + + // iterate over lines + while(($line = fgets($fh, 65536)) !== false) { + $line = trim($line); + if ($line === '') { + // save stored entry on empty line + // block moved to fix "fuzzy flag first line" bug which didn't saved previous proper string at all + if (sizeof($temp) && array_key_exists('msgid', $temp) && array_key_exists('msgstr', $temp)) { + if (!$fuzzy) + $hash[] = $temp; + $temp = array (); + $state = null; + $fuzzy = false; + } + continue; + } + $array_of_splited_string = preg_split('/\s/', $line, 2); + $key = $array_of_splited_string[0]; + $data = (isset($array_of_splited_string[1]) ? $array_of_splited_string[1] : ''); + + switch ($key) { + case '#,' : // flag... + $fuzzy = in_array('fuzzy', preg_split('/,\s*/', $data)); + case '#' : // translator-comments + case '#.' : // extracted-comments + case '#:' : // reference... + case '#|' : // msgid previous-untranslated-string + break; + case '#~' : // commented-unused-string + $temp = array (); + $state = null; + $fuzzy = false; + break; + case 'msgctxt' : + // context + case 'msgid' : + // untranslated-string + case 'msgid_plural' : + // untranslated-string-plural + $state = $key; + $temp[$state] = $data; + break; + case 'msgstr' : + // translated-string + $state = 'msgstr'; + $temp[$state][] = $data; + break; + default : + if (strpos($key, 'msgstr[') !== FALSE) { + // translated-string-case-n + $state = 'msgstr'; + $temp[$state][] = $data; + } else { + // continued lines + switch ($state) { + case 'msgctxt' : + case 'msgid' : + case 'msgid_plural' : + $temp[$state] .= "\n" . $line; + break; + case 'msgstr' : + $temp[$state][sizeof($temp[$state]) - 1] .= "\n" . $line; + break; + default : + // parse error + fclose($fh); + return FALSE; + } + } + break; + } + } + fclose($fh); + + // add final entry + if ($state == 'msgstr') + $hash[] = $temp; + + // Cleanup data, merge multiline entries, reindex hash for ksort + $temp = $hash; + $hash = array (); + foreach ($temp as $entry) { + foreach ($entry as & $v) { + $v = phpmo_clean_helper($v); + if ($v === FALSE) { + // parse error + return FALSE; + } + } + $hash[$entry['msgid']] = $entry; + } + + return $hash; +} + +?> \ No newline at end of file diff --git a/langs.inc.php b/langs.inc.php index 2e2fa77d9..6607f7edc 100644 --- a/langs.inc.php +++ b/langs.inc.php @@ -52,7 +52,7 @@ $domains_lang = array( 'mageia.ro' => 'ro', ); -require_once('_nav/langs/php-mo.php'); +require_once ('_nav/lib.php'); /** * Redirect to a localized path, depending on incoming TLD. diff --git a/langs.php b/langs.php index ddf3bf1a3..e616782d8 100644 --- a/langs.php +++ b/langs.php @@ -77,7 +77,6 @@ $langsForm = << H; -include ('_nav/lib.php'); $hsnav = _mgnav_style() . _mgnav_html(true, $locale, $langsForm, $_SERVER['HTTP_HOST']); $hsfoot = ''; diff --git a/langs/diff.php b/langs/diff.php index 70ff102c6..251cd87f0 100644 --- a/langs/diff.php +++ b/langs/diff.php @@ -3,7 +3,7 @@ * Report the diff of 's' file * against matching file in language 'l', if it exists. */ - +define('HLANG', true); include 'lib.php'; $source_file = isset($_GET['s']) ? strip_tags(trim($_GET['s'])) : null; diff --git a/langs/lib.php b/langs/lib.php index 054a4743e..b238cdd4c 100644 --- a/langs/lib.php +++ b/langs/lib.php @@ -13,7 +13,7 @@ if (isset($_SERVER['APP_MODE']) && $_SERVER['APP_MODE'] !== 'prod') { ini_set('log_errors', FALSE); } -include '../langs.inc.php'; +include '../langs.php'; /** * Diff two .lang files, to get: diff --git a/langs/missing.php b/langs/missing.php index 952232704..db267864e 100644 --- a/langs/missing.php +++ b/langs/missing.php @@ -1,7 +1,7 @@