diff options
| author | 2014-12-03 16:45:51 +0100 | |
|---|---|---|
| committer | 2014-12-03 16:45:51 +0100 | |
| commit | aa232d8fd52cf548c0b2e7d96e026e684eadda53 (patch) | |
| tree | 3d9983c3d3260c8a62326cb756544b8b8464f165 /lib/Minz/Translate.php | |
| parent | 82745a509a1284a9fe6a7f8d5d153eda953d9e11 (diff) | |
Update the i18n system.
Follow recommendations from https://github.com/FreshRSS/FreshRSS/issues/334
Diffstat (limited to 'lib/Minz/Translate.php')
| -rw-r--r-- | lib/Minz/Translate.php | 114 |
1 files changed, 80 insertions, 34 deletions
diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php index 8c2f90041..084bd7e07 100644 --- a/lib/Minz/Translate.php +++ b/lib/Minz/Translate.php @@ -5,71 +5,117 @@ */ /** - * La classe Translate se charge de la traduction - * Utilise les fichiers du répertoire /app/i18n/ + * This class is used for the internationalization. + * It uses files in `./app/i18n/` */ class Minz_Translate { /** - * $language est la langue à afficher + * $lang_name is the name of the current language to use. */ - private static $language; - + private static $lang_name; + + /** + * $lang_path is the pathname of i18n files (e.g. ./app/i18n/en/). + */ + private static $lang_path; + /** - * $translates est le tableau de correspondance - * $key => $traduction + * $translates is a cache for i18n translation. */ private static $translates = array(); - + /** - * Inclus le fichier de langue qui va bien - * l'enregistre dans $translates + * Load $lang_name and $lang_path based on configuration and selected language. */ public static function init() { $l = Minz_Configuration::language(); - self::$language = Minz_Session::param('language', $l); - - $l_path = APP_PATH . '/i18n/' . self::$language . '.php'; - - if (file_exists($l_path)) { - self::$translates = include($l_path); - } + self::$lang_name = Minz_Session::param('language', $l); + self::$lang_path = APP_PATH . '/i18n/' . self::$lang_name . '/'; } - + /** - * Alias de init + * Alias for init(). */ public static function reset() { self::init(); } - + /** - * Traduit une clé en sa valeur du tableau $translates - * @param $key la clé à traduire - * @return la valeur correspondante à la clé - * > si non présente dans le tableau, on retourne la clé elle-même + * Translate a key into its corresponding value based on selected language. + * @param $key the key to translate. + * @param additional parameters for variable keys. + * @return the value corresponding to the key. + * If no value is found, return the key itself. */ public static function t($key) { - $translate = $key; - - if (isset(self::$translates[$key])) { - $translate = self::$translates[$key]; + $group = explode('.', $key); + + if (count($group) < 2) { + // Minz_Log::debug($key . ' is not in a valid format'); + $top_level = 'gen'; + } else { + $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'); + return $key; + } + + self::$translates[$top_level] = include($filename); + } + + // Go through the i18n keys to get the correct translation value. + $translates = self::$translates[$top_level]; + $size_group = count($group); + $level_processed = 0; + $translation_value = $key; + foreach ($group as $i18n_level) { + $level_processed++; + if (!isset($translates[$i18n_level])) { + Minz_Log::debug($key . ' is not a valid key'); + return $key; + } + + if ($level_processed < $size_group) { + $translates = $translates[$i18n_level]; + } else { + $translation_value = $translates[$i18n_level]; + } + } + + if (is_array($translation_value)) { + if (isset($translation_value['_'])) { + $translation_value = $translation_value['_']; + } else { + Minz_Log::debug($key . ' is not a valid key'); + return $key; + } + } + + // Get the facultative arguments to replace i18n variables. $args = func_get_args(); unset($args[0]); - - return vsprintf($translate, $args); + + return vsprintf($translation_value, $args); } - + /** - * Retourne la langue utilisée actuellement - * @return la langue + * Return the current language. */ public static function language() { - return self::$language; + return self::$lang_name; } } + +/** + * Alias for Minz_Translate::t() + */ function _t($key) { $args = func_get_args(); unset($args[0]); |
