Translate.php 2.9 KB

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