/classes/cyclone/I18n.php

https://github.com/cyclonephp/cyclone · PHP · 128 lines · 39 code · 17 blank · 72 comment · 5 complexity · 3765ccfe84a5bc6a5074c9d609088732 MD5 · raw file

  1. <?php
  2. namespace cyclone;
  3. /**
  4. * Internationalization (i18n) class. Provides language loading and translation
  5. * methods without dependancies on [gettext](http://php.net/gettext).
  6. *
  7. * Typically this class would never be used directly, but used via the __()
  8. * function, which loads the message and replaces parameters:
  9. *
  10. * // Display a translated message
  11. * echo __('Hello, world');
  12. *
  13. * // With parameter replacement
  14. * echo __('Hello, :user', array(':user' => $username));
  15. *
  16. * [!!] The __() function is declared in `SYSPATH/base.php`.
  17. *
  18. * @package cyclone
  19. * @category Base
  20. * @author Kohana Team
  21. * @copyright (c) 2008-2009 Kohana Team
  22. * @license http://kohanaphp.com/license
  23. */
  24. class I18n
  25. {
  26. /**
  27. * @var string target language: en-us, es-es, zh-cn, etc
  28. */
  29. public static $lang = 'en-us';
  30. // Cache of loaded languages
  31. protected static $_cache = array();
  32. /**
  33. * Get and set the target language.
  34. *
  35. * // Get the current language
  36. * $lang = I18n::lang();
  37. *
  38. * // Change the current language to Spanish
  39. * I18n::lang('es-es');
  40. *
  41. * @param string new language setting
  42. * @return string
  43. * @since 3.0.2
  44. */
  45. public static function lang($lang = NULL)
  46. {
  47. if ($lang) {
  48. // Normalize the language
  49. I18n::$lang = strtolower(str_replace(array(' ', '_'), '-', $lang));
  50. }
  51. return I18n::$lang;
  52. }
  53. /**
  54. * Returns translation of a string. If no translation exists, the original
  55. * string will be returned. Example: @code
  56. *
  57. * $hello = I18n::get('Hello :name !', array(
  58. * ':name' => 'World'
  59. * )); @endcode
  60. *
  61. * <p>Hint: in most cases it is more convenient to use the __() alias function
  62. * of this method, which is defined in the <code>init.php</code> of the library.</p>
  63. *
  64. * @param string $string text to translate
  65. * @param array $values key-value pairs to be replaced in the translated text
  66. * @param string $lang can be used to specify the target language. If it is
  67. * null then the method will fall back to <code>I18n::$lang</code>
  68. * @return string the transtaled text with the parameters replaced
  69. */
  70. public static function get($string, $values = array(), $lang = NULL) {
  71. if (NULL === $lang) {
  72. $lang = static::$lang;
  73. }
  74. if (!isset(I18n::$_cache[$lang])) {
  75. // Load the translation table
  76. I18n::load($lang);
  77. }
  78. $string = isset(I18n::$_cache[$lang][$string]) ? I18n::$_cache[$lang][$string] : $string;
  79. $rval = strtr($string, $values);
  80. // Return the translated string if it exists
  81. return $rval;
  82. }
  83. /**
  84. * Returns the translation table for a given language.
  85. *
  86. * // Get all defined Spanish messages
  87. * $messages = I18n::load('es-es');
  88. *
  89. * @param string language to load
  90. * @return array
  91. */
  92. public static function load($lang)
  93. {
  94. if (isset(I18n::$_cache[$lang])) {
  95. return I18n::$_cache[$lang];
  96. }
  97. // New translation table
  98. $table = array();
  99. // Split the language: language, region, locale, etc
  100. $parts = explode('-', $lang);
  101. do {
  102. // Create a path for this set of parts
  103. $path = implode(\DIRECTORY_SEPARATOR, $parts);
  104. $table += FileSystem::get_default()->list_files("i18n/$path.php", TRUE);
  105. // Remove the last part
  106. array_pop($parts);
  107. } while ($parts);
  108. // Cache the translation table locally
  109. return I18n::$_cache[$lang] = $table;
  110. }
  111. } // End I18n