PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/kohana/i18n.php

https://bitbucket.org/emolina/asesores
PHP | 139 lines | 48 code | 17 blank | 74 comment | 5 complexity | 397ad5a31a6ee168ac68d142186b362b 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. * [!!] The __() function is declared in `SYSPATH/base.php`.
  16. *
  17. * @package Kohana
  18. * @category Base
  19. * @author Kohana Team
  20. * @copyright (c) 2008-2010 Kohana Team
  21. * @license http://kohanaframework.org/license
  22. */
  23. class Kohana_I18n {
  24. /**
  25. * @var string target language: en-us, es-es, zh-cn, etc
  26. */
  27. public static $lang = 'en-us';
  28. /**
  29. * @var string source language: en-us, es-es, zh-cn, etc
  30. */
  31. public static $source = 'en-us';
  32. /**
  33. * @var array cache of loaded languages
  34. */
  35. protected static $_cache = array();
  36. /**
  37. * Get and set the target language.
  38. *
  39. * // Get the current language
  40. * $lang = I18n::lang();
  41. *
  42. * // Change the current language to Spanish
  43. * I18n::lang('es-es');
  44. *
  45. * @param string new language setting
  46. * @return string
  47. * @since 3.0.2
  48. */
  49. public static function lang($lang = NULL)
  50. {
  51. if ($lang)
  52. {
  53. // Normalize the language
  54. I18n::$lang = strtolower(str_replace(array(' ', '_'), '-', $lang));
  55. }
  56. return I18n::$lang;
  57. }
  58. /**
  59. * Returns translation of a string. If no translation exists, the original
  60. * string will be returned. No parameters are replaced.
  61. *
  62. * $hello = I18n::get('Hello friends, my name is :name');
  63. *
  64. * @param string text to translate
  65. * @param string target language
  66. * @return string
  67. */
  68. public static function get($string, $lang = NULL)
  69. {
  70. if ( ! $lang)
  71. {
  72. // Use the global target language
  73. $lang = I18n::$lang;
  74. }
  75. // Load the translation table for this language
  76. $table = I18n::load($lang);
  77. // Return the translated string if it exists
  78. return isset($table[$string]) ? $table[$string] : $string;
  79. }
  80. /**
  81. * Returns the translation table for a given language.
  82. *
  83. * // Get all defined Spanish messages
  84. * $messages = I18n::load('es-es');
  85. *
  86. * @param string language to load
  87. * @return array
  88. */
  89. public static function load($lang)
  90. {
  91. if (isset(I18n::$_cache[$lang]))
  92. {
  93. return I18n::$_cache[$lang];
  94. }
  95. // New translation table
  96. $table = array();
  97. // Split the language: language, region, locale, etc
  98. $parts = explode('-', $lang);
  99. do
  100. {
  101. // Create a path for this set of parts
  102. $path = implode(DIRECTORY_SEPARATOR, $parts);
  103. if ($files = Kohana::find_file('i18n', $path, NULL, TRUE))
  104. {
  105. $t = array();
  106. foreach ($files as $file)
  107. {
  108. // Merge the language strings into the sub table
  109. $t = array_merge($t, Kohana::load($file));
  110. }
  111. // Append the sub table, preventing less specific language
  112. // files from overloading more specific files
  113. $table += $t;
  114. }
  115. // Remove the last part
  116. array_pop($parts);
  117. }
  118. while ($parts);
  119. // Cache the translation table locally
  120. return I18n::$_cache[$lang] = $table;
  121. }
  122. } // End I18n