PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/i18n_v2/inc/class.I18NtranslatorGettext.inc.php

http://flaimo-php.googlecode.com/
PHP | 170 lines | 80 code | 15 blank | 75 comment | 11 complexity | b409dbcbe381654f8f40793b4f2e0e38 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 Gettext 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 I18NtranslatorGettext extends I18NtranslatorBase implements I18NtranslatorInterface {
  21. /**
  22. * @var string
  23. */
  24. const MESSAGES_DIR = 'LC_MESSAGES';
  25. /**
  26. * @var array
  27. */
  28. protected $translation_table;
  29. /**
  30. * @var boolean
  31. */
  32. protected $gettext_loaded = FALSE;
  33. /**
  34. * @param string $namespaces
  35. * @param object $locale I18Nlocale
  36. * @uses I18NtranslatorBase::__construct()
  37. * @uses I18NtranslatorGettext::setGettext()
  38. * @return void
  39. */
  40. public function __construct($namespaces = '', I18Nlocale &$locale = NULL) {
  41. parent::__construct($namespaces, $locale);
  42. $this->setGettext();
  43. } // end constructor
  44. /**
  45. * retreive the available translator locales
  46. * @uses I18Nbase::getI18Nsetting()
  47. * @uses I18NtranslatorBase::getSessionLocales()
  48. * @uses I18NtranslatorGettext::setRealLocale()
  49. * @uses I18NtranslatorBase::$locales
  50. * @return boolean
  51. */
  52. protected function setLocales() {
  53. $this->locales = array();
  54. $root = parent::getI18Nsetting('locales_path') . '/';
  55. $handle = @opendir($root);
  56. while ($lang_dir = trim(@readdir($handle))) {
  57. if (!is_dir($root . $lang_dir) ||
  58. parent::isValidLocaleCode($lang_dir) === FALSE ||
  59. !is_dir($root . $lang_dir . '/' . self::MESSAGES_DIR)) {
  60. continue;
  61. } // end if
  62. $this->locales[strtolower($lang_dir)] =& $this->setRealLocale(strtolower($lang_dir));
  63. $_SESSION['i18n']['translator_locales'][$lang_dir] = $this->locales[$lang_dir]->getI18Nlocale();
  64. } // end while
  65. @closedir($handle);
  66. unset($root, $handle);
  67. return (count($this->locales) > 0) ? TRUE : FALSE;
  68. } // end function
  69. /**
  70. * retreive the real locale for an alias
  71. * @param string $locale_code
  72. * @uses I18Nbase::getI18Nsetting()
  73. * @uses I18Nbase::getI18NfactoryLocale()
  74. * @uses I18Nbase::isValidLocaleCode()
  75. * @return object
  76. */
  77. protected function setRealLocale($locale_code = '') {
  78. $path = parent::getI18Nsetting('locales_path') . '/' . $locale_code . '/redirect';
  79. if (parent::getI18Nsetting('use_alias_locales') === FALSE ||
  80. ($redirect_file = @file($path)) == FALSE) {
  81. return parent::getI18NfactoryLocale($locale_code);
  82. } // end if
  83. if (parent::isValidLocaleCode($redirect_file[0]) === TRUE) {
  84. return parent::getI18NfactoryLocale($redirect_file[0]);
  85. } // end if
  86. return parent::getI18NfactoryLocale($locale_code);
  87. } // end function
  88. /**
  89. * loads the gettext data
  90. * @uses I18NtranslatorGettext::$gettext_loaded
  91. * @uses I18NtranslatorBase::getTranslatorLocale()
  92. * @uses I18Nbase::getI18Nsetting()
  93. * @return boolean
  94. */
  95. protected function setGettext() {
  96. if ($this->gettext_loaded === TRUE) {
  97. return (boolean) TRUE;
  98. } // end if
  99. if (!extension_loaded('gettext')) {
  100. die ('Gettext extention not installed!');
  101. } // end if
  102. putenv('LANG=' . $this->getTranslatorLocale()->getI18Nlocale());
  103. //setlocale(LC_ALL, ''); // Enable this if you have problems with gettext. Maybe it helps
  104. $td_set = (boolean) FALSE;
  105. foreach ($this->namespaces as $namespace) {
  106. //echo '-' , $namespace , '-' , $i++ , '<br>';
  107. bindtextdomain($namespace, parent::getI18Nsetting('locales_path') . '/');
  108. if ($td_set === TRUE) {
  109. continue;
  110. } // end if
  111. textdomain($namespace);
  112. $td_set = (boolean) TRUE;
  113. } // end foreach
  114. $this->gettext_loaded = (boolean) TRUE;
  115. return (boolean) TRUE;
  116. } // end function
  117. /**
  118. * main translator method for translating strings
  119. * @param string $translationstring string to be translated
  120. * @param string $domain alias for namespace (sometimes needed for gettext)
  121. * @param array $arguments whe using translation strings with placeholders, $arguments is an array with the values for the placeholders
  122. * @return string translated tring or error message
  123. */
  124. public function translate($translationstring = '', $domain = '', $arguments = FALSE) {
  125. $output = gettext($translationstring);
  126. if ($output === $translationstring) {
  127. $output = dgettext($domain, $translationstring);
  128. } // end if
  129. if ($output === $translationstring) {
  130. if (parent::getI18Nsetting('show_errormessages')) {
  131. trigger_error('TRANSLATION ERROR: String "' . $translationstring . '" not found in translation table', E_USER_WARNING);
  132. } // end if
  133. return $string;
  134. } // end if
  135. return $output;
  136. } // end function
  137. /**
  138. * changes the locale of an translator object and resetzs the translation data
  139. * @param object $locale I18Nlocale object
  140. * @return boolean
  141. * @deprecated not possible with gettext
  142. */
  143. public function changeLocale(I18Nlocale &$locale = FALSE) {
  144. return (boolean) FALSE;
  145. } // end function
  146. /**
  147. * returns an array with $array[translationstring] = translation
  148. * @return array $array[translationstring] = translation
  149. * @deprecated not possible with gettext
  150. */
  151. public function getTranslationTable() {
  152. return array();
  153. } // end function
  154. } // end TranslatorGettext
  155. ?>