Translate.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * This class is used for the internationalization.
  8. * It uses files in `./app/i18n/`
  9. */
  10. class Minz_Translate {
  11. /**
  12. * $lang_name is the name of the current language to use.
  13. */
  14. private static $lang_name;
  15. /**
  16. * $lang_files is a list of registered i18n files.
  17. */
  18. private static $lang_files = array();
  19. /**
  20. * $translates is a cache for i18n translation.
  21. */
  22. private static $translates = array();
  23. /**
  24. * Init the translation object.
  25. * @param $lang_name the lang to show.
  26. */
  27. public static function init($lang_name) {
  28. self::$lang_name = $lang_name;
  29. self::$lang_files = array();
  30. self::$translates = array();
  31. self::registerPath(APP_PATH . '/i18n');
  32. }
  33. /**
  34. * Reset the translation object with a new language.
  35. * @param $lang_name the new language to use
  36. */
  37. public static function reset($lang_name) {
  38. self::init($lang_name);
  39. }
  40. /**
  41. * Return the list of available languages.
  42. * @return an array.
  43. * @todo fix this method.
  44. */
  45. public static function availableLanguages() {
  46. return array();
  47. }
  48. /**
  49. * Register a new path and load i18n files inside.
  50. *
  51. * @param $path a path containing i18n directories (e.g. ./en/, ./fr/).
  52. */
  53. public static function registerPath($path) {
  54. // We load first i18n files for the current language.
  55. $lang_path = $path . '/' . self::$lang_name;
  56. $list_i18n_files = array_values(array_diff(
  57. scandir($lang_path),
  58. array('..', '.')
  59. ));
  60. // Each file basename correspond to a top-level i18n key. For each of
  61. // these keys we store the file pathname and mark translations must be
  62. // reloaded (by setting $translates[$i18n_key] to null).
  63. foreach ($list_i18n_files as $i18n_filename) {
  64. $i18n_key = basename($i18n_filename, '.php');
  65. if (!isset(self::$lang_files[$i18n_key])) {
  66. self::$lang_files[$i18n_key] = array();
  67. }
  68. self::$lang_files[$i18n_key][] = $lang_path . '/' . $i18n_filename;
  69. self::$translates[$i18n_key] = null;
  70. }
  71. }
  72. /**
  73. * Load the files associated to $key into $translates.
  74. *
  75. * @param $key the top level i18n key we want to load.
  76. */
  77. private static function loadKey($key) {
  78. // The top level key is not in $lang_files, it means it does not exist!
  79. if (!isset(self::$lang_files[$key])) {
  80. Minz_Log::debug($key . ' is not a valid top level key');
  81. return false;
  82. }
  83. self::$translates[$key] = array();
  84. foreach (self::$lang_files[$key] as $lang_pathname) {
  85. $i18n_array = include($lang_pathname);
  86. if (!is_array($i18n_array)) {
  87. Minz_Log::warning('`' . $lang_pathname . '` does not contain a PHP array');
  88. continue;
  89. }
  90. // We must avoid to erase previous data so we just override them if
  91. // needed.
  92. self::$translates[$key] = array_replace_recursive(
  93. self::$translates[$key], $i18n_array
  94. );
  95. }
  96. return true;
  97. }
  98. /**
  99. * Translate a key into its corresponding value based on selected language.
  100. * @param $key the key to translate.
  101. * @param additional parameters for variable keys.
  102. * @return the value corresponding to the key.
  103. * If no value is found, return the key itself.
  104. */
  105. public static function t($key) {
  106. $group = explode('.', $key);
  107. if (count($group) < 2) {
  108. Minz_Log::debug($key . ' is not in a valid format');
  109. $top_level = 'gen';
  110. } else {
  111. $top_level = array_shift($group);
  112. }
  113. // If $translates[$top_level] is null it means we have to load the
  114. // corresponding files.
  115. if (is_null(self::$translates[$top_level])) {
  116. $res = self::loadKey($top_level);
  117. if (!$res) {
  118. return $key;
  119. }
  120. }
  121. // Go through the i18n keys to get the correct translation value.
  122. $translates = self::$translates[$top_level];
  123. $size_group = count($group);
  124. $level_processed = 0;
  125. $translation_value = $key;
  126. foreach ($group as $i18n_level) {
  127. $level_processed++;
  128. if (!isset($translates[$i18n_level])) {
  129. Minz_Log::debug($key . ' is not a valid key');
  130. return $key;
  131. }
  132. if ($level_processed < $size_group) {
  133. $translates = $translates[$i18n_level];
  134. } else {
  135. $translation_value = $translates[$i18n_level];
  136. }
  137. }
  138. if (is_array($translation_value)) {
  139. if (isset($translation_value['_'])) {
  140. $translation_value = $translation_value['_'];
  141. } else {
  142. Minz_Log::debug($key . ' is not a valid key');
  143. return $key;
  144. }
  145. }
  146. // Get the facultative arguments to replace i18n variables.
  147. $args = func_get_args();
  148. unset($args[0]);
  149. return vsprintf($translation_value, $args);
  150. }
  151. /**
  152. * Return the current language.
  153. */
  154. public static function language() {
  155. return self::$lang_name;
  156. }
  157. }
  158. /**
  159. * Alias for Minz_Translate::t()
  160. */
  161. function _t($key) {
  162. $args = func_get_args();
  163. unset($args[0]);
  164. array_unshift($args, $key);
  165. return call_user_func_array('Minz_Translate::t', $args);
  166. }