I18n.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Internationalization (i18n) class. Provides language loading and translation
  4. * methods without dependencies on [gettext](http://php.net/gettext).
  5. *
  6. * Typically this class would never be used directly, but used via the __()
  7. * function, which loads the message and replaces parameters:
  8. *
  9. * // Display a translated message
  10. * echo __('Hello, world');
  11. *
  12. * // With parameter replacement
  13. * echo __('Hello, :user', array(':user' => $username));
  14. *
  15. * @package Kohana
  16. * @category Base
  17. * @author Kohana Team
  18. * @copyright (c) Kohana Team
  19. * @license https://koseven.ga/LICENSE.md
  20. */
  21. class Kohana_I18n {
  22. /**
  23. * @var string target language: en-us, es-es, zh-cn, etc
  24. */
  25. public static $lang = 'en-us';
  26. /**
  27. * @var string source language: en-us, es-es, zh-cn, etc
  28. */
  29. public static $source = 'en-us';
  30. /**
  31. * @var array cache of loaded languages
  32. */
  33. protected static $_cache = [];
  34. /**
  35. * Get and set the target language.
  36. *
  37. * // Get the current language
  38. * $lang = I18n::lang();
  39. *
  40. * // Change the current language to Spanish
  41. * I18n::lang('es-es');
  42. *
  43. * @param string $lang new language setting
  44. * @return string
  45. * @since 3.0.2
  46. */
  47. public static function lang($lang = NULL)
  48. {
  49. if ($lang)
  50. {
  51. // Normalize the language
  52. I18n::$lang = strtolower(str_replace([' ', '_'], '-', $lang));
  53. }
  54. return I18n::$lang;
  55. }
  56. /**
  57. * Returns translation of a string. If no translation exists, the original
  58. * string will be returned. No parameters are replaced.
  59. *
  60. * $hello = I18n::get('Hello friends, my name is :name');
  61. *
  62. * @param string $string text to translate
  63. * @param string $lang target language
  64. * @return string
  65. */
  66. public static function get($string, $lang = NULL)
  67. {
  68. if ( ! $lang)
  69. {
  70. // Use the global target language
  71. $lang = I18n::$lang;
  72. }
  73. // Load the translation table for this language
  74. $table = I18n::load($lang);
  75. // Return the translated string if it exists
  76. return isset($table[$string]) ? $table[$string] : $string;
  77. }
  78. /**
  79. * Returns the translation table for a given language.
  80. *
  81. * // Get all defined Spanish messages
  82. * $messages = I18n::load('es-es');
  83. *
  84. * @param string $lang language to load
  85. * @return array
  86. */
  87. public static function load($lang)
  88. {
  89. if (isset(I18n::$_cache[$lang]))
  90. {
  91. return I18n::$_cache[$lang];
  92. }
  93. // New translation table
  94. $table = [];
  95. // Split the language: language, region, locale, etc
  96. $parts = explode('-', $lang);
  97. do
  98. {
  99. // Create a path for this set of parts
  100. $path = implode(DIRECTORY_SEPARATOR, $parts);
  101. if ($files = Kohana::find_file('i18n', $path, NULL, TRUE))
  102. {
  103. $t = [];
  104. foreach ($files as $file)
  105. {
  106. // Merge the language strings into the sub table
  107. $t = array_merge($t, Kohana::load($file));
  108. }
  109. // Append the sub table, preventing less specific language
  110. // files from overloading more specific files
  111. $table += $t;
  112. }
  113. // Remove the last part
  114. array_pop($parts);
  115. }
  116. while ($parts);
  117. // Cache the translation table locally
  118. return I18n::$_cache[$lang] = $table;
  119. }
  120. }
  121. if ( ! function_exists('__'))
  122. {
  123. /**
  124. * Kohana translation/internationalization function. The PHP function
  125. * [strtr](http://php.net/strtr) is used for replacing parameters.
  126. *
  127. * __('Welcome back, :user', array(':user' => $username));
  128. *
  129. * [!!] The target language is defined by [I18n::$lang].
  130. *
  131. * @uses I18n::get
  132. * @param string $string text to translate
  133. * @param array $values values to replace in the translated text
  134. * @param string $lang source language
  135. * @return string
  136. */
  137. function __($string, array $values = NULL, $lang = 'en-us')
  138. {
  139. if ($lang !== I18n::$lang)
  140. {
  141. // The message and target languages are different
  142. // Get the translation for this message
  143. $string = I18n::get($string);
  144. }
  145. return empty($values) ? $string : strtr($string, $values);
  146. }
  147. }