PageRenderTime 151ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/i18n_v2/inc/class.I18NtranslatorXML.inc.php

http://flaimo-php.googlecode.com/
PHP | 234 lines | 118 code | 23 blank | 93 comment | 26 complexity | a0aa9e0c92f898a108c2163558cfc831 MD5 | raw file
  1. <?php
  2. /**
  3. * required interface for all translatorXXX classes
  4. */
  5. require_once 'interface.I18NtranslatorInterface.inc.php';
  6. /**
  7. * load base class which takes care of all the other includes via it's autoload function
  8. */
  9. require_once 'class.I18Nbase.inc.php';
  10. /**
  11. * translator class with xml-files as a backend
  12. * @author Michael Wimmer <flaimo@gmail.com>
  13. * @category flaimo-php
  14. * @example ../www_root/i18n_example_script.php i18n example script
  15. * @license GNU General Public License v3
  16. * @link http://code.google.com/p/flaimo-php/
  17. * @package i18n
  18. * @version 2.3.1
  19. */
  20. class I18NtranslatorXML extends I18NtranslatorBase implements I18NtranslatorInterface {
  21. /**
  22. * @var boolean
  23. */
  24. const USE_FILECACHE = FALSE;
  25. /**#@+
  26. * @var array
  27. */
  28. protected $translation_table;
  29. protected $xmlfiles;
  30. /**#@-*/
  31. /**
  32. * @param string $namespaces
  33. * @param object $locale I18Nlocale
  34. * @uses I18NtranslatorBase::__construct()
  35. * @return void
  36. */
  37. public function __construct($namespaces = '', I18Nlocale &$locale = NULL) {
  38. parent::__construct($namespaces, $locale);
  39. } // end constructor
  40. /**
  41. * returns path to a translationfile
  42. * @param string $namespaces
  43. * @uses I18Nbase::getI18Nsetting()
  44. * @uses I18NtranslatorBase::getTranslatorLocale()
  45. * @return boolean
  46. */
  47. protected function getNamespaceFilepath($namespace = '') {
  48. return (string) parent::getI18Nsetting('locales_path') . '/' . parent::getTranslatorLocale()->getI18Nlocale() . '/' . $namespace . '.xml';
  49. } // end function
  50. /**
  51. * retreive the available translator locales
  52. * @uses I18Nbase::getI18Nsetting()
  53. * @uses I18NtranslatorBase::getSessionLocales()
  54. * @uses I18NtranslatorXML::setRealLocale()
  55. * @uses I18NtranslatorBase::$locales
  56. * @return boolean
  57. */
  58. protected function setLocales() {
  59. if (parent::getSessionLocales() == TRUE) {
  60. return (boolean) TRUE;
  61. } // end if
  62. $this->locales = array();
  63. $root = parent::getI18Nsetting('locales_path') . '/';
  64. $handle = @opendir($root);
  65. while ($lang_dir = trim(@readdir($handle))) {
  66. if (!is_dir($root . $lang_dir) ||
  67. parent::isValidLocaleCode($lang_dir) === FALSE) {
  68. continue;
  69. } // end if
  70. $lang_dir = strtolower($lang_dir);
  71. $this->locales[$lang_dir] =& $this->setRealLocale($lang_dir);
  72. $_SESSION['i18n']['translator_locales'][$lang_dir] = $this->locales[$lang_dir]->getI18Nlocale();
  73. } // end while
  74. @closedir($handle);
  75. unset($root, $handle);
  76. return (count($this->locales) > 0) ? TRUE : FALSE;
  77. } // end function
  78. /**
  79. * retreive the real locale for an alias
  80. * @param string $locale_code
  81. * @uses I18Nbase::getI18Nsetting()
  82. * @uses I18Nbase::getI18NfactoryLocale()
  83. * @uses I18Nbase::isValidLocaleCode()
  84. * @uses I18Nbase::getI18NfactoryLocale()
  85. * @return object
  86. */
  87. protected function setRealLocale($locale_code = '') {
  88. $path = parent::getI18Nsetting('locales_path') . '/' . $locale_code . '/redirect';
  89. if (parent::getI18Nsetting('use_alias_locales') === FALSE ||
  90. ($redirect_file = @file($path)) == FALSE) {
  91. return parent::getI18NfactoryLocale($locale_code);
  92. } // end if
  93. if (parent::isValidLocaleCode($redirect_file[0]) === TRUE) {
  94. return parent::getI18NfactoryLocale($redirect_file[0]);
  95. } // end if
  96. return parent::getI18NfactoryLocale($locale_code);
  97. } // end function
  98. /**
  99. * fetch the translation file
  100. * @param string $namespace
  101. * @uses I18NtranslatorXML::getNamespaceFilepath()
  102. * @return mixed
  103. */
  104. protected function fetchTranslationFile($namespace = '') {
  105. if (($file = simplexml_load_file($this->getNamespaceFilepath($namespace))) == FALSE) {
  106. return (boolean) FALSE;
  107. } // end if
  108. return $file;
  109. } // end function
  110. /**
  111. * transform filecontent into translation array
  112. * @param string $namespace
  113. * @uses I18NtranslatorXML::fetchTranslationFile()
  114. * @uses I18NtranslatorBase::getTranslatorLocale()
  115. * @uses I18NtranslatorXML::$translation_table
  116. * @return void
  117. */
  118. protected function fetchTranslations($namespace = '') {
  119. $file = $this->fetchTranslationFile($namespace);
  120. if ($file == FALSE) {
  121. return (boolean) FALSE;
  122. } // end if
  123. $this->xmlfiles[$namespace] = $file;
  124. foreach ($this->xmlfiles[$namespace]->locale as $locale) {
  125. if ($locale['id'] != parent::getTranslatorLocale()->getI18Nlocale()) {
  126. continue;
  127. } // end if
  128. foreach ($locale->namespace as $namespace_tmp) {
  129. if ($namespace_tmp['id'] != $namespace) {
  130. continue;
  131. } // end if
  132. foreach ($namespace_tmp->translations as $translations) {
  133. foreach ($translations->translation as $translation) {
  134. if (mb_strlen($translation['string']) == 0) {
  135. // cant do mb_strlen on $translation --> apache crash
  136. continue;
  137. } // end if
  138. parent::checkForDuplicates(trim($translation['string']), $namespace);
  139. $this->translation_table[trim($translation['string'])] = trim($translation);
  140. } // end foreach
  141. } // end foreach
  142. } // end foreach
  143. } // end foreach
  144. } // end function
  145. /**
  146. * fetch all translation files and transform them
  147. * @uses I18Nbase::getFileCache()
  148. * @uses I18NtranslatorXML::$use_filecache
  149. * @uses I18NtranslatorBase::$namespaces
  150. * @uses I18Nbase::getTranslatorLocale()
  151. * @uses I18NtranslatorXML::$translation_table
  152. * @uses I18NtranslatorXML::fetchTranslations()
  153. * @return boolean
  154. */
  155. protected function fetchAllTranslations() {
  156. if (self::USE_FILECACHE == TRUE) {
  157. $cache =& parent::getFileCache();
  158. $cache_filename = implode('_', $this->namespaces) . parent::getTranslatorLocale()->getI18Nlocale();
  159. if ($cache->isCached($cache_filename) === TRUE) {
  160. $this->translation_table = unserialize($cache->returnCache($cache_filename));
  161. unset($cache);
  162. return (boolean) TRUE;
  163. } // end if
  164. } // end if
  165. foreach ($this->namespaces as $namespace) {
  166. $this->fetchTranslations($namespace);
  167. } // end foreach
  168. $this->translation_table = array_filter(array_map('trim', $this->translation_table), 'strlen');
  169. ksort($this->translation_table);
  170. //$this->translation_table = array_unique($this->translation_table);
  171. if (self::USE_FILECACHE == TRUE) {
  172. $cache->writeCache($cache_filename, serialize($this->translation_table));
  173. unset($cache);
  174. } // end if
  175. return (boolean) TRUE;
  176. } // end function
  177. /**
  178. * returns an array with $array[translationstring] = translation
  179. * @uses I18NtranslatorXML::fetchAllTranslations()
  180. * @return array $array[translationstring] = translation
  181. */
  182. public function getTranslationTable() {
  183. if (!isset($this->translation_table)) {
  184. $this->fetchAllTranslations();
  185. } // end if
  186. return $this->translation_table;
  187. } // end function
  188. /**
  189. * main translator method for translating strings
  190. * @param string $translationstring string to be translated
  191. * @param string $domain alias for namespace (sometimes needed for gettext)
  192. * @param array $arguments whe using translation strings with placeholders, $arguments is an array with the values for the placeholders
  193. * @return string translated tring or error message
  194. * @uses I18NtranslatorXML::getTranslationTable()
  195. * @uses I18NtranslatorXML::$translation_table
  196. */
  197. public function translate($translationstring = '', $domain = '', $arguments = FALSE) {
  198. $string = trim($translationstring);
  199. if (!array_key_exists($string, $this->getTranslationTable())) {
  200. if (parent::getI18Nsetting('show_errormessages')) {
  201. trigger_error('TRANSLATION ERROR: String "' . $string . '" not found in translation table', E_USER_WARNING);
  202. } // end if
  203. return $string;
  204. } // end if
  205. if (!is_array($arguments)) {
  206. return $this->translation_table[$string];
  207. } // end if
  208. return vsprintf($this->translation_table[$string], $arguments);
  209. } // end function
  210. } // end TranslatorText
  211. ?>