Translate.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_list is the list of available languages.
  13. */
  14. private static $lang_list = array();
  15. /**
  16. * $lang_name is the name of the current language to use.
  17. */
  18. private static $lang_name;
  19. /**
  20. * $lang_path is the pathname of i18n files (e.g. ./app/i18n/en/).
  21. */
  22. private static $lang_path;
  23. /**
  24. * $translates is a cache for i18n translation.
  25. */
  26. private static $translates = array();
  27. /**
  28. * Init the translation object.
  29. * @param $lang_list the list of available languages.
  30. * @param $lang_name the lang to show.
  31. */
  32. public static function init($lang_list, $lang_name) {
  33. self::$lang_list = $lang_list;
  34. self::$lang_name = $lang_name;
  35. self::$lang_path = APP_PATH . '/i18n/' . self::$lang_name . '/';
  36. }
  37. /**
  38. * Reset the translation object with a new language.
  39. * @param $lang_name the new language to use
  40. */
  41. public static function reset($lang_name) {
  42. self::init(self::$lang_list, $lang_name);
  43. }
  44. /**
  45. * Return the list of available languages.
  46. * @return an array.
  47. */
  48. public static function availableLanguages() {
  49. return self::$lang_list;
  50. }
  51. /**
  52. * Translate a key into its corresponding value based on selected language.
  53. * @param $key the key to translate.
  54. * @param additional parameters for variable keys.
  55. * @return the value corresponding to the key.
  56. * If no value is found, return the key itself.
  57. */
  58. public static function t($key) {
  59. $group = explode('.', $key);
  60. if (count($group) < 2) {
  61. Minz_Log::debug($key . ' is not in a valid format');
  62. $top_level = 'gen';
  63. } else {
  64. $top_level = array_shift($group);
  65. }
  66. $filename = self::$lang_path . $top_level . '.php';
  67. // Try to load the i18n file if it's not done yet.
  68. if (!isset(self::$translates[$top_level])) {
  69. if (!file_exists($filename)) {
  70. Minz_Log::debug($top_level . ' is not a valid top level key');
  71. return $key;
  72. }
  73. self::$translates[$top_level] = include($filename);
  74. }
  75. // Go through the i18n keys to get the correct translation value.
  76. $translates = self::$translates[$top_level];
  77. $size_group = count($group);
  78. $level_processed = 0;
  79. $translation_value = $key;
  80. foreach ($group as $i18n_level) {
  81. $level_processed++;
  82. if (!isset($translates[$i18n_level])) {
  83. Minz_Log::debug($key . ' is not a valid key');
  84. return $key;
  85. }
  86. if ($level_processed < $size_group) {
  87. $translates = $translates[$i18n_level];
  88. } else {
  89. $translation_value = $translates[$i18n_level];
  90. }
  91. }
  92. if (is_array($translation_value)) {
  93. if (isset($translation_value['_'])) {
  94. $translation_value = $translation_value['_'];
  95. } else {
  96. Minz_Log::debug($key . ' is not a valid key');
  97. return $key;
  98. }
  99. }
  100. // Get the facultative arguments to replace i18n variables.
  101. $args = func_get_args();
  102. unset($args[0]);
  103. return vsprintf($translation_value, $args);
  104. }
  105. /**
  106. * Return the current language.
  107. */
  108. public static function language() {
  109. return self::$lang_name;
  110. }
  111. }
  112. /**
  113. * Alias for Minz_Translate::t()
  114. */
  115. function _t($key) {
  116. $args = func_get_args();
  117. unset($args[0]);
  118. array_unshift($args, $key);
  119. return call_user_func_array('Minz_Translate::t', $args);
  120. }