PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Adapto/Language.php

http://github.com/egeniq/adapto
PHP | 281 lines | 129 code | 51 blank | 101 comment | 37 complexity | 5c057f5c25eda972abc3cf85ed8cd097 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Adapto Toolkit.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package adapto
  9. *
  10. */
  11. /**
  12. * Class that handles userinterface internationalization.
  13. *
  14. * This class is used to retrieve the proper translations for any string
  15. * displayed in the userinterface. It includes only those language files
  16. * that are actually used, and has several fallback systems to find
  17. * translations if they can be find in the correct module.
  18. *
  19. * @author Boy Baukema <boy@ibuildings.nl>
  20. * @package adapto
  21. *
  22. */
  23. class Adapto_Language
  24. {
  25. private $_zendTranslate = NULL;
  26. /**
  27. * Default Constructor
  28. * @access private
  29. */
  30. public function __construct()
  31. {
  32. Adapto_Util_Debugger::debug("New instance made of Adapto_Language");
  33. }
  34. public static function _($string, $module="adapto", $entity = NULL, $lng = NULL)
  35. {
  36. $instance = Zend_Registry::get("Adapto_Language");
  37. return $instance->text($string, $module, $entity, $lng);
  38. }
  39. /**
  40. * Text function, retrieves a translation for a certain string.
  41. *
  42. * @param mixed $string string or array of strings containing the name(s) of the string to return
  43. * when an array of strings is passed, the second will be the fallback if
  44. * the first one isn't found, and so forth
  45. * @param String $module module in which the language file should be looked for,
  46. * defaults to core module with fallback to Adapto
  47. * @param String $entity the entity to which the string belongs
  48. * @param String $lng ISO 639-1 language code, defaults to config variable
  49. * @param String $firstfallback the first module to check as part of the fallback
  50. * @param bool $entityfaulttext if true, then it doesn't returns false when it can't find a translation
  51. * @param bool $modulefallback Wether or not to use all the modules of the application in the fallback,
  52. * when looking for strings
  53. * @return String the string from the languagefile
  54. */
  55. public function text($string, $module="adapto", $entity = NULL, $lng = NULL)
  56. {
  57. // We don't translate nothing
  58. if ($string == '') {
  59. return '';
  60. }
  61. if ($lng == "") {
  62. $lng = $this->getLanguage();
  63. }
  64. $lng = strtolower($lng);
  65. // If only one string given, process it immediately
  66. if (!is_array($string))
  67. return $this->_getString($string, $module, $lng, $entity);
  68. // If multiple strings given, iterate through all strings and return the translation if found
  69. for ($i = 0, $_i = count($string); $i < $_i; $i++) {
  70. // Try to get the translation
  71. $translation = $this->_getString($string[$i], $module, $lng, $entity, ($i < ($_i - 1)));
  72. // Return the translation if found
  73. if ($translation != "")
  74. return $translation;
  75. }
  76. return "";
  77. }
  78. public function getSupportedLanguages()
  79. {
  80. return Adapto_Config::get('adapto', 'language.supported_languages', array('en'));
  81. }
  82. /**
  83. * Get the current language, either from url, or if that's not present, from what the user has set.
  84. * @static
  85. * @return String current language.
  86. */
  87. public function getLanguage()
  88. {
  89. $session = Zend_Registry::get('Session_Adapto');
  90. if (isset($session->language)
  91. && in_array($session->language, $this->getSupportedLanguages())) {
  92. $lng = $session->language;
  93. } else {
  94. $lng = $this->getUserLanguage();
  95. // Remember it
  96. $session->language = $lng;
  97. }
  98. return strtolower($lng);
  99. }
  100. /**
  101. * Get the selected language of the current user if he/she set one,
  102. * otherwise we try to get it from the browser settings and if even THAT
  103. * fails, we return the default language.
  104. *
  105. * @static
  106. * @return unknown
  107. */
  108. public function getUserLanguage()
  109. {
  110. $supported = $this->getSupportedLanguages();
  111. /** @todo this is what it did in ATK; must be rewritten to Zend_Auth etc.
  112. $sessionmanager = null;
  113. if (function_exists('atkGetSessionManager'))
  114. $sessionmanager = &atkGetSessionManager();
  115. if (!empty($sessionmanager)) {
  116. if (function_exists("getUser")) {
  117. $userinfo = getUser();
  118. $fieldname = Adapto_Config::get('adapto', 'auth_languagefield');
  119. if (isset($userinfo[$fieldname]) && in_array($userinfo[$fieldname], $supported))
  120. return $userinfo[$fieldname];
  121. }
  122. } */
  123. // Otherwise we check the headers
  124. if (Adapto_Config::get('adapto', 'language.use_browser_language', false)) {
  125. $headerlng = $this->getLanguageFromHeaders();
  126. if ($headerlng && in_array($headerlng, $supported))
  127. return $headerlng;
  128. }
  129. // We give up and just return the default language
  130. return Adapto_Config::get('adapto', 'language', 'en');
  131. }
  132. /**
  133. * Get the primary languagecode that the user has set in his/her browser
  134. *
  135. * @static
  136. * @return String The languagecode
  137. */
  138. public static function getLanguageFromHeaders()
  139. {
  140. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  141. $langs = split('[,;]', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  142. if ($langs[0] != "") {
  143. $elems = explode("-", $langs[0]); // lng might contain a subset after the dash.
  144. $autolng = $elems[0];
  145. }
  146. }
  147. return $autolng;
  148. }
  149. protected function _loadString($key, $module, $lng)
  150. {
  151. if ($this->_zendTranslate == NULL) {
  152. $strings = $this->_loadLanguage($module, $lng);
  153. $this->_zendTranslate = new Zend_Translate(
  154. array(
  155. 'adapter' => 'array',
  156. 'content' => $strings,
  157. 'locale' => $lng)
  158. );
  159. } else {
  160. if (!$this->_zendTranslate->isAvailable($lng)) {
  161. $strings = $this->_loadLanguage($module, $lng);
  162. $this->_zendTranslate->addTranslation(
  163. array(
  164. 'content' => $strings,
  165. 'locale' => $lng)
  166. );
  167. }
  168. }
  169. return $this->_zendTranslate->_($key);
  170. }
  171. protected function _loadLanguage($module, $lng)
  172. {
  173. $strings = array();
  174. if ($module == "adapto") {
  175. $path = APPLICATION_PATH . '/../library/Adapto/Language/';
  176. } else {
  177. $path = APPLICATION_PATH . '/modules/' . $module . '/';
  178. }
  179. $path .= $lng . '.php';
  180. if (!file_exists($path)) {
  181. throw new Adapto_Exception("Language '$lng' not available in module '$module'");
  182. } else {
  183. include($path);
  184. return $$lng;
  185. }
  186. }
  187. /**
  188. * This function takes care of the fallbacks when retrieving a string ids.
  189. * It is as following:
  190. * First we check for a string specific to both the module and the entity
  191. * (module_entity_key).
  192. * If that isn't found we check for an entity specific string (entity_key).
  193. * And if all that fails we look for a general string in the module.
  194. *
  195. * @access protected
  196. *
  197. * @param string $key the name of the string to return
  198. * @param string $module module in which the language file should be looked for,
  199. * defaults to core module with fallback to Adapto
  200. * @param string $lng ISO 639-1 language code, defaults to config variable
  201. * @param string $entity the entity to which the string belongs
  202. * @param bool $entityfaulttext wether or not to pass a default text back
  203. * @param string $firstfallback the first module to check as part of the fallback
  204. * @param bool $modulefallback Wether or not to use all the modules of the application in the fallback,
  205. * when looking for strings
  206. * @return string the name with which to call the string we want from the languagefile
  207. */
  208. protected function _getString($key, $module, $lng, $entity = "", $failSilently = false)
  209. {
  210. if ($entity != "") {
  211. $text = $this->_loadString($module . "_" . $entity . "_" . $key, $module, $lng);
  212. if ($text != "") {
  213. return $text;
  214. } else {
  215. $text = $this->_loadString($entity . "_" . $key, $module, $lng);
  216. if ($text != "") {
  217. return $text;
  218. }
  219. }
  220. }
  221. $text = $this->_loadString($key, $module, $lng);
  222. if ($text != "") {
  223. return $text;
  224. }
  225. if (!$failSilently) {
  226. throw new Adapto_Exception("Adapto_Language: translation for '$key' with module: '$module' and entity: '$entity' and language: '$lng' not found");
  227. }
  228. return "";
  229. }
  230. }