diff options
author | Maurice Svay <maurice@svay.com> | 2012-04-29 11:11:03 -0700 |
---|---|---|
committer | Maurice Svay <maurice@svay.com> | 2012-04-29 11:11:03 -0700 |
commit | 2626ea9b5ce18cf980eb869efa691e3d078a76d0 (patch) | |
tree | 8e5a23b4e1282d1fe67958fcdf63fdeb0947834b | |
parent | b5c2590daaa73999560343a7a8d3867dc7f2beb8 (diff) | |
parent | be4d9bf84103db5ed02d6ffd97e74f6c515f8515 (diff) | |
download | planet-2626ea9b5ce18cf980eb869efa691e3d078a76d0.tar planet-2626ea9b5ce18cf980eb869efa691e3d078a76d0.tar.gz planet-2626ea9b5ce18cf980eb869efa691e3d078a76d0.tar.bz2 planet-2626ea9b5ce18cf980eb869efa691e3d078a76d0.tar.xz planet-2626ea9b5ce18cf980eb869efa691e3d078a76d0.zip |
Merge pull request #38 from pascalchevrel/l10n
add support for localization notes in strings. This is a feature allowin...
-rwxr-xr-x | app/app.php | 2 | ||||
-rw-r--r-- | app/classes/Simplel10n.class.php | 2 | ||||
-rw-r--r-- | app/l10n/extract.php | 196 |
3 files changed, 198 insertions, 2 deletions
diff --git a/app/app.php b/app/app.php index d17c42e..f2795ab 100755 --- a/app/app.php +++ b/app/app.php @@ -39,7 +39,7 @@ if (is_file($savedConfig)){ $l10n = new Simplel10n($conf['locale']); // this is an helper function. We will usually use that function and not Simplel10n::getString() -function _g($str) { +function _g($str, $comment='') { return Simplel10n::getString($str); } diff --git a/app/classes/Simplel10n.class.php b/app/classes/Simplel10n.class.php index cc31921..36ac958 100644 --- a/app/classes/Simplel10n.class.php +++ b/app/classes/Simplel10n.class.php @@ -17,7 +17,7 @@ class Simplel10n { $this->l10nFolder = $path; } - static function getString($str) { + static function getString($str, $comment='') { if(array_key_exists($str, $GLOBALS['locale'])) { return $GLOBALS['locale'][$str]; } else { diff --git a/app/l10n/extract.php b/app/l10n/extract.php new file mode 100644 index 0000000..c63e564 --- /dev/null +++ b/app/l10n/extract.php @@ -0,0 +1,196 @@ +<?php + +/* + * This is a file parser to extract localizable strings from moonmoon + * + * It will scan the whole moonmoon repository for php files, extract + * localized strings and their localization notes and create .lang files. + * Existing translations will be automatically updated. + * A short report will be displayed afterwards. + * + * The easiest way to add a new locale is just to create an empty .lang file and then run the script + * + * The script scans for the files in the l10n/ folder to know which locales are supported + */ + +// released versions of moonmoon should immediately return for security +// return; + +$root = dirname(__FILE__) . '/../../'; +$GLOBALS['english'] = array(); + +include_once $root . '/app/classes/Simplel10n.class.php'; + +/* + * This is a file parser to extract localizable strings (in .php files) + * $GLOBALS['english'] is populated with localizable strings and their associated localization notes + * + */ + + +function extract_l10n_strings($file) { + $lines = file($file); + $patterns = array('/_g\((.*?)\)/', '/getString\((.*?)\)/', ); + + foreach ($lines as $line) { + + // Skip comments + if($line[0] == '#' || $line[0] == '/') continue; + + // parsing logic + foreach($patterns as $pattern) { + if(preg_match_all($pattern, $line, $matches, PREG_PATTERN_ORDER)) { + foreach($matches[1] as $val) { + + // Do not extract php variables calls or empty strings + if($val[0] == '$' || $val == '') continue; + + // Is there a localization comment ? + $l10n_note = explode("',", $val); + + // Also test strings in double quotes + if(count($l10n_note) == 1) { + $l10n_note = explode('",', $val); + } + + // Extract cleaned up strings + if(count($l10n_note) == 2) { + $l10n_str = substr(trim($l10n_note[0]), 1, -1); + $l10n_note = substr(trim($l10n_note[1]), 1, -1); + } else { + $l10n_str = substr(trim($val), 1, -1); # Remove quotes around the string + $l10n_note = ''; + } + + if(!array_key_exists($l10n_str, $GLOBALS['english'])) { + $GLOBALS['english'][$l10n_str] = array($l10n_str, $l10n_note); + } + } + } + } + } +} + +/* + * This is a function echoing $GLOBALS['english'] in .lang format + * Typical usage would be: + * <?php + * extract_l10n_strings('.'); + * show_l10n_strings() ; + */ + +function show_l10n_strings() { + + header('Content-Type:text/plain'); + + foreach($GLOBALS['english'] as $val) { + if($val[1]) { + echo '# ' . $val[1] . "\n"; + } + echo ";$val[0]\n"; + echo "$val[0]\n\n\n"; + } +} + +/* + * Recursively scan files in a folder + * returns an array of file paths + */ + +function find_all_files($dir) { + + $result = array(); + $root = scandir($dir); + + $ignore = array('.', '..', '.git', '.svn', '.hg', 'cache', '.gitignore', 'lib'); + + foreach($root as $value) { + + if(in_array($value, $ignore)) { + continue; + } + + if(is_file("$dir/$value")) { + $split = explode('.', $value); + if(isset($split[1]) && $split[1] == 'php'){ + $result[] = "$dir/$value"; + } + continue; + } + + foreach(find_all_files("$dir/$value") as $value) { + $result[]=$value; + } + } + + return $result; +} + +function update_lang_files($source, $dest) { + + $files = find_all_files($source); + + foreach($files as $file) { + extract_l10n_strings($file); + } + + + $files = scandir($dest); + $ignore = array('.', '..'); + + + // list locales + $locales = array(); + foreach($files as $file) { + + if(in_array($file, $ignore)) { + continue; + } + + $split = explode('.', $file); + + if($split[1] == 'lang') { + $locales[] = $split[0]; + } + } + + + foreach($locales as $locale) { + $status[$locale] = 0; + $lang_file_path = $dest . '/' . $locale; + + Simplel10n::load($lang_file_path); + + ob_start(); + foreach($GLOBALS['english'] as $key => $val) { + + if($val[1]) { + echo '# Translation note: ' . $val[1] . "\n"; + } + echo ";$val[0]\n"; + + $value = @Simplel10n::getString($key); + echo $value . "\n\n\n"; + if($value == $val[0]) { + $status[$locale]++; + } + } + + $content = ob_get_contents(); + ob_end_clean(); + file_put_contents($lang_file_path. '.lang', $content); + + unset($GLOBALS['locale']); + } + + + // Display a short status report + header('Content-Type:text/plain'); + echo "Number of English strings: " . count($GLOBALS['english']) . "\n"; + echo "Your installation has these languages installed: " . implode(', ', $locales) . "\n"; + foreach($locales as $val) { + echo $val . " has " . $status[$val] . " untranslated strings.\n"; + } +} + +update_lang_files($root, $root . 'app/l10n'); |