Translate.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. self::registerPath(APP_PATH . '/i18n');
  59. foreach (self::$path_list as $path) {
  60. $scan = scandir($path);
  61. if (is_array($scan)) {
  62. $path_langs = array_values(array_diff(
  63. $scan,
  64. array('..', '.')
  65. ));
  66. if (is_array($path_langs)) {
  67. $list_langs = array_merge($list_langs, $path_langs);
  68. }
  69. }
  70. }
  71. return array_unique($list_langs);
  72. }
  73. /**
  74. * Return the language to use in the application.
  75. * It returns the connected language if it exists then returns the first match from the
  76. * preferred languages then returns the default language
  77. * @param $user the connected user language (nullable)
  78. * @param $preferred an array of the preferred languages
  79. * @param $default the preferred language to use
  80. * @return a string containing the language to use
  81. */
  82. public static function getLanguage($user, $preferred, $default) {
  83. if (null !== $user) {
  84. return $user;
  85. }
  86. $languages = Minz_Translate::availableLanguages();
  87. foreach ($preferred as $language) {
  88. $language = strtolower($language);
  89. if (in_array($language, $languages, true)) {
  90. return $language;
  91. }
  92. }
  93. return $default ? $default : 'en';
  94. }
  95. /**
  96. * Register a new path.
  97. * @param $path a path containing i18n directories (e.g. ./en/, ./fr/).
  98. */
  99. public static function registerPath($path) {
  100. if (!in_array($path, self::$path_list) && is_dir($path)) {
  101. self::$path_list[] = $path;
  102. self::loadLang($path);
  103. }
  104. }
  105. /**
  106. * Load translations of the current language from the given path.
  107. * @param $path the path containing i18n directories.
  108. */
  109. private static function loadLang($path) {
  110. $lang_path = $path . '/' . self::$lang_name;
  111. if (!file_exists($lang_path) || self::$lang_name == '') {
  112. // The lang path does not exist, nothing more to do.
  113. return;
  114. }
  115. $list_i18n_files = array_values(array_diff(
  116. scandir($lang_path),
  117. array('..', '.')
  118. ));
  119. // Each file basename correspond to a top-level i18n key. For each of
  120. // these keys we store the file pathname and mark translations must be
  121. // reloaded (by setting $translates[$i18n_key] to null).
  122. foreach ($list_i18n_files as $i18n_filename) {
  123. $i18n_key = basename($i18n_filename, '.php');
  124. if (!isset(self::$lang_files[$i18n_key])) {
  125. self::$lang_files[$i18n_key] = array();
  126. }
  127. self::$lang_files[$i18n_key][] = $lang_path . '/' . $i18n_filename;
  128. self::$translates[$i18n_key] = null;
  129. }
  130. }
  131. /**
  132. * Load the files associated to $key into $translates.
  133. * @param $key the top level i18n key we want to load.
  134. */
  135. private static function loadKey($key) {
  136. // The top level key is not in $lang_files, it means it does not exist!
  137. if (!isset(self::$lang_files[$key])) {
  138. Minz_Log::debug($key . ' is not a valid top level key');
  139. return false;
  140. }
  141. self::$translates[$key] = array();
  142. foreach (self::$lang_files[$key] as $lang_pathname) {
  143. $i18n_array = include($lang_pathname);
  144. if (!is_array($i18n_array)) {
  145. Minz_Log::warning('`' . $lang_pathname . '` does not contain a PHP array');
  146. continue;
  147. }
  148. // We must avoid to erase previous data so we just override them if
  149. // needed.
  150. self::$translates[$key] = array_replace_recursive(
  151. self::$translates[$key], $i18n_array
  152. );
  153. }
  154. return true;
  155. }
  156. /**
  157. * Translate a key into its corresponding value based on selected language.
  158. * @param $key the key to translate.
  159. * @param additional parameters for variable keys.
  160. * @return the value corresponding to the key.
  161. * If no value is found, return the key itself.
  162. */
  163. public static function t($key) {
  164. $group = explode('.', $key);
  165. if (count($group) < 2) {
  166. Minz_Log::debug($key . ' is not in a valid format');
  167. $top_level = 'gen';
  168. } else {
  169. $top_level = array_shift($group);
  170. }
  171. // If $translates[$top_level] is null it means we have to load the
  172. // corresponding files.
  173. if (!isset(self::$translates[$top_level]) ||
  174. is_null(self::$translates[$top_level])) {
  175. $res = self::loadKey($top_level);
  176. if (!$res) {
  177. return $key;
  178. }
  179. }
  180. // Go through the i18n keys to get the correct translation value.
  181. $translates = self::$translates[$top_level];
  182. $size_group = count($group);
  183. $level_processed = 0;
  184. $translation_value = $key;
  185. foreach ($group as $i18n_level) {
  186. $level_processed++;
  187. if (!isset($translates[$i18n_level])) {
  188. Minz_Log::debug($key . ' is not a valid key');
  189. return $key;
  190. }
  191. if ($level_processed < $size_group) {
  192. $translates = $translates[$i18n_level];
  193. } else {
  194. $translation_value = $translates[$i18n_level];
  195. }
  196. }
  197. if (is_array($translation_value)) {
  198. if (isset($translation_value['_'])) {
  199. $translation_value = $translation_value['_'];
  200. } else {
  201. Minz_Log::debug($key . ' is not a valid key');
  202. return $key;
  203. }
  204. }
  205. // Get the facultative arguments to replace i18n variables.
  206. $args = func_get_args();
  207. unset($args[0]);
  208. return vsprintf($translation_value, $args);
  209. }
  210. /**
  211. * Return the current language.
  212. */
  213. public static function language() {
  214. return self::$lang_name;
  215. }
  216. }
  217. /**
  218. * Alias for Minz_Translate::t()
  219. */
  220. function _t($key) {
  221. $args = func_get_args();
  222. unset($args[0]);
  223. array_unshift($args, $key);
  224. return call_user_func_array('Minz_Translate::t', $args);
  225. }