Translate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * $path_list is the list of registered base path to search translations.
  13. */
  14. private static $path_list = array();
  15. /**
  16. * $lang_name is the name of the current language to use.
  17. */
  18. private static $lang_name;
  19. /**
  20. * $lang_files is a list of registered i18n files.
  21. */
  22. private static $lang_files = array();
  23. /**
  24. * $translates is a cache for i18n translation.
  25. */
  26. private static $translates = array();
  27. /**
  28. * Init the translation object.
  29. * @param $lang_name the lang to show.
  30. */
  31. public static function init($lang_name = null) {
  32. self::$lang_name = $lang_name;
  33. self::$lang_files = array();
  34. self::$translates = array();
  35. self::registerPath(APP_PATH . '/i18n');
  36. foreach (self::$path_list as $path) {
  37. self::loadLang($path);
  38. }
  39. }
  40. /**
  41. * Reset the translation object with a new language.
  42. * @param $lang_name the new language to use
  43. */
  44. public static function reset($lang_name) {
  45. self::$lang_name = $lang_name;
  46. self::$lang_files = array();
  47. self::$translates = array();
  48. foreach (self::$path_list as $path) {
  49. self::loadLang($path);
  50. }
  51. }
  52. /**
  53. * Return the list of available languages.
  54. * @return an array containing langs found in different registered paths.
  55. */
  56. public static function availableLanguages() {
  57. $list_langs = array();
  58. foreach (self::$path_list as $path) {
  59. $path_langs = array_values(array_diff(
  60. scandir($path),
  61. array('..', '.')
  62. ));
  63. $list_langs = array_merge($list_langs, $path_langs);
  64. }
  65. return array_unique($list_langs);
  66. }
  67. /**
  68. * Register a new path.
  69. * @param $path a path containing i18n directories (e.g. ./en/, ./fr/).
  70. */
  71. public static function registerPath($path) {
  72. if (in_array($path, self::$path_list)) {
  73. return;
  74. }
  75. self::$path_list[] = $path;
  76. self::loadLang($path);
  77. }
  78. /**
  79. * Load translations of the current language from the given path.
  80. * @param $path the path containing i18n directories.
  81. */
  82. private static function loadLang($path) {
  83. $lang_path = $path . '/' . self::$lang_name;
  84. if (!file_exists($lang_path) || is_null(self::$lang_name)) {
  85. // The lang path does not exist, nothing more to do.
  86. return;
  87. }
  88. $list_i18n_files = array_values(array_diff(
  89. scandir($lang_path),
  90. array('..', '.')
  91. ));
  92. // Each file basename correspond to a top-level i18n key. For each of
  93. // these keys we store the file pathname and mark translations must be
  94. // reloaded (by setting $translates[$i18n_key] to null).
  95. foreach ($list_i18n_files as $i18n_filename) {
  96. $i18n_key = basename($i18n_filename, '.php');
  97. if (!isset(self::$lang_files[$i18n_key])) {
  98. self::$lang_files[$i18n_key] = array();
  99. }
  100. self::$lang_files[$i18n_key][] = $lang_path . '/' . $i18n_filename;
  101. self::$translates[$i18n_key] = null;
  102. }
  103. }
  104. /**
  105. * Load the files associated to $key into $translates.
  106. * @param $key the top level i18n key we want to load.
  107. */
  108. private static function loadKey($key) {
  109. // The top level key is not in $lang_files, it means it does not exist!
  110. if (!isset(self::$lang_files[$key])) {
  111. Minz_Log::debug($key . ' is not a valid top level key');
  112. return false;
  113. }
  114. self::$translates[$key] = array();
  115. foreach (self::$lang_files[$key] as $lang_pathname) {
  116. $i18n_array = include($lang_pathname);
  117. if (!is_array($i18n_array)) {
  118. Minz_Log::warning('`' . $lang_pathname . '` does not contain a PHP array');
  119. continue;
  120. }
  121. // We must avoid to erase previous data so we just override them if
  122. // needed.
  123. self::$translates[$key] = array_replace_recursive(
  124. self::$translates[$key], $i18n_array
  125. );
  126. }
  127. return true;
  128. }
  129. /**
  130. * Translate a key into its corresponding value based on selected language.
  131. * @param $key the key to translate.
  132. * @param additional parameters for variable keys.
  133. * @return the value corresponding to the key.
  134. * If no value is found, return the key itself.
  135. */
  136. public static function t($key) {
  137. $group = explode('.', $key);
  138. if (count($group) < 2) {
  139. Minz_Log::debug($key . ' is not in a valid format');
  140. $top_level = 'gen';
  141. } else {
  142. $top_level = array_shift($group);
  143. }
  144. // If $translates[$top_level] is null it means we have to load the
  145. // corresponding files.
  146. if (!isset(self::$translates[$top_level]) ||
  147. is_null(self::$translates[$top_level])) {
  148. $res = self::loadKey($top_level);
  149. if (!$res) {
  150. return $key;
  151. }
  152. }
  153. // Go through the i18n keys to get the correct translation value.
  154. $translates = self::$translates[$top_level];
  155. $size_group = count($group);
  156. $level_processed = 0;
  157. $translation_value = $key;
  158. foreach ($group as $i18n_level) {
  159. $level_processed++;
  160. if (!isset($translates[$i18n_level])) {
  161. Minz_Log::debug($key . ' is not a valid key');
  162. return $key;
  163. }
  164. if ($level_processed < $size_group) {
  165. $translates = $translates[$i18n_level];
  166. } else {
  167. $translation_value = $translates[$i18n_level];
  168. }
  169. }
  170. if (is_array($translation_value)) {
  171. if (isset($translation_value['_'])) {
  172. $translation_value = $translation_value['_'];
  173. } else {
  174. Minz_Log::debug($key . ' is not a valid key');
  175. return $key;
  176. }
  177. }
  178. // Get the facultative arguments to replace i18n variables.
  179. $args = func_get_args();
  180. unset($args[0]);
  181. return vsprintf($translation_value, $args);
  182. }
  183. /**
  184. * Return the current language.
  185. */
  186. public static function language() {
  187. return self::$lang_name;
  188. }
  189. }
  190. /**
  191. * Alias for Minz_Translate::t()
  192. */
  193. function _t($key) {
  194. $args = func_get_args();
  195. unset($args[0]);
  196. array_unshift($args, $key);
  197. return call_user_func_array('Minz_Translate::t', $args);
  198. }