aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/Translate.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-12-12 18:58:34 +0100
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-12-12 18:58:34 +0100
commiteabcf90f5266b746503fef5c438971d7d6a1689e (patch)
tree7f2d6d38d297a0f4c4b4e7d52e0d59ce7f7f025b /lib/Minz/Translate.php
parent99763412c2f49e4e52b5a0343bb465141be76188 (diff)
parent6078888de631ea105410079888838503fd7acb3d (diff)
Merge branch 'dev' into beta
Conflicts: app/i18n/en.php app/i18n/fr.php
Diffstat (limited to 'lib/Minz/Translate.php')
-rw-r--r--lib/Minz/Translate.php114
1 files changed, 80 insertions, 34 deletions
diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php
index 8c2f90041..e7efb8665 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]);