Translate.php 5.6 KB

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