Translate.php 2.9 KB

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