PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/i18n.php

https://bitbucket.org/herson091/kohana1
PHP | 166 lines | 59 code | 19 blank | 88 comment | 7 complexity | b1ce4d4ea8a223d075ba6b327d65a403 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  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) 2008-2012 Kohana Team
  19. * @license http://kohanaframework.org/license
  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 = array();
  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(array(' ', '_'), '-', $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 = array();
  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 = array();
  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. } // End I18n
  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. }