diff options
Diffstat (limited to 'lib/Minz/Translate.php')
| -rw-r--r-- | lib/Minz/Translate.php | 127 |
1 files changed, 108 insertions, 19 deletions
diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php index 1b4102ca9..baddcb424 100644 --- a/lib/Minz/Translate.php +++ b/lib/Minz/Translate.php @@ -10,9 +10,9 @@ */ class Minz_Translate { /** - * $lang_list is the list of available languages. + * $path_list is the list of registered base path to search translations. */ - private static $lang_list = array(); + private static $path_list = array(); /** * $lang_name is the name of the current language to use. @@ -20,9 +20,9 @@ class Minz_Translate { private static $lang_name; /** - * $lang_path is the pathname of i18n files (e.g. ./app/i18n/en/). + * $lang_files is a list of registered i18n files. */ - private static $lang_path; + private static $lang_files = array(); /** * $translates is a cache for i18n translation. @@ -31,13 +31,16 @@ class Minz_Translate { /** * Init the translation object. - * @param $lang_list the list of available languages. * @param $lang_name the lang to show. */ - public static function init($lang_list, $lang_name) { - self::$lang_list = $lang_list; + public static function init($lang_name = null) { self::$lang_name = $lang_name; - self::$lang_path = APP_PATH . '/i18n/' . self::$lang_name . '/'; + self::$lang_files = array(); + self::$translates = array(); + self::registerPath(APP_PATH . '/i18n'); + foreach (self::$path_list as $path) { + self::loadLang($path); + } } /** @@ -45,15 +48,103 @@ class Minz_Translate { * @param $lang_name the new language to use */ public static function reset($lang_name) { - self::init(self::$lang_list, $lang_name); + self::$lang_name = $lang_name; + self::$lang_files = array(); + self::$translates = array(); + foreach (self::$path_list as $path) { + self::loadLang($path); + } } /** * Return the list of available languages. - * @return an array. + * @return an array containing langs found in different registered paths. */ public static function availableLanguages() { - return self::$lang_list; + $list_langs = array(); + + foreach (self::$path_list as $path) { + $path_langs = array_values(array_diff( + scandir($path), + array('..', '.') + )); + + $list_langs = array_merge($list_langs, $path_langs); + } + + return array_unique($list_langs); + } + + /** + * Register a new path. + * @param $path a path containing i18n directories (e.g. ./en/, ./fr/). + */ + public static function registerPath($path) { + if (in_array($path, self::$path_list)) { + return; + } + + self::$path_list[] = $path; + self::loadLang($path); + } + + /** + * Load translations of the current language from the given path. + * @param $path the path containing i18n directories. + */ + private static function loadLang($path) { + $lang_path = $path . '/' . self::$lang_name; + if (!file_exists($lang_path) || is_null(self::$lang_name)) { + // The lang path does not exist, nothing more to do. + return; + } + + $list_i18n_files = array_values(array_diff( + scandir($lang_path), + array('..', '.') + )); + + // Each file basename correspond to a top-level i18n key. For each of + // these keys we store the file pathname and mark translations must be + // reloaded (by setting $translates[$i18n_key] to null). + foreach ($list_i18n_files as $i18n_filename) { + $i18n_key = basename($i18n_filename, '.php'); + if (!isset(self::$lang_files[$i18n_key])) { + self::$lang_files[$i18n_key] = array(); + } + self::$lang_files[$i18n_key][] = $lang_path . '/' . $i18n_filename; + self::$translates[$i18n_key] = null; + } + } + + /** + * Load the files associated to $key into $translates. + * @param $key the top level i18n key we want to load. + */ + private static function loadKey($key) { + // The top level key is not in $lang_files, it means it does not exist! + if (!isset(self::$lang_files[$key])) { + Minz_Log::debug($key . ' is not a valid top level key'); + return false; + } + + self::$translates[$key] = array(); + + foreach (self::$lang_files[$key] as $lang_pathname) { + $i18n_array = include($lang_pathname); + if (!is_array($i18n_array)) { + Minz_Log::warning('`' . $lang_pathname . '` does not contain a PHP array'); + continue; + } + + // We must avoid to erase previous data so we just override them if + // needed. + self::$translates[$key] = array_replace_recursive( + self::$translates[$key], $i18n_array + ); + } + + return true; } /** @@ -73,16 +164,14 @@ class Minz_Translate { $top_level = array_shift($group); } - $filename = self::$lang_path . $top_level . '.php'; - - // Try to load the i18n file if it's not done yet. - if (!isset(self::$translates[$top_level])) { - if (!file_exists($filename)) { - Minz_Log::debug($top_level . ' is not a valid top level key'); + // If $translates[$top_level] is null it means we have to load the + // corresponding files. + if (!isset(self::$translates[$top_level]) || + is_null(self::$translates[$top_level])) { + $res = self::loadKey($top_level); + if (!$res) { return $key; } - - self::$translates[$top_level] = include($filename); } // Go through the i18n keys to get the correct translation value. |
