PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/i18n_v2/inc/class.I18NtranslatorSQLite3.inc.php

http://flaimo-php.googlecode.com/
PHP | 196 lines | 103 code | 18 blank | 75 comment | 18 complexity | b4917a4c4256cb608b23c882d2bd9034 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. * singleton function for getting one db connection
  12. * @return object
  13. */
  14. function setSQLiteConn($db_name) {
  15. static $conn;
  16. if (!isset($conn)) {
  17. $conn = new PDO('sqlite:' . $db_name);
  18. } // end if
  19. return $conn;
  20. } // end function
  21. function getPrepStat($index, $conn, $sql = '') {
  22. static $ps;
  23. if (!isset($ps[$index])) {
  24. $ps[$index] = $conn->prepare($sql);
  25. } // end if
  26. return $ps[$index];
  27. } // end function
  28. /**
  29. * translator class with SQLite3 as a backend
  30. * @author Michael Wimmer <flaimo@gmail.com>
  31. * @category flaimo-php
  32. * @example ../www_root/i18n_example_script.php i18n example script
  33. * @license GNU General Public License v3
  34. * @link http://code.google.com/p/flaimo-php/
  35. * @package i18n
  36. * @version 2.3.1
  37. */
  38. class I18NtranslatorSQLite3 extends I18NtranslatorBase implements I18NtranslatorInterface {
  39. /**#@+
  40. * @var string database settings
  41. */
  42. protected $database = 'translations.sqlite3';
  43. protected $table = 'flp_translator';
  44. /**#@-*/
  45. /**
  46. * @var array
  47. */
  48. protected $translation_table;
  49. protected $prep_stat = array();
  50. /**
  51. * @var boolean
  52. */
  53. const USE_FILECACHE = FALSE; // filecache normally is slower than using sqlite directly
  54. /**
  55. * @param string $namespaces
  56. * @param object $locale I18Nlocale
  57. * @uses I18NtranslatorBase::__construct()
  58. * @return void
  59. */
  60. public function __construct($namespaces = '', I18Nlocale &$locale = NULL) {
  61. parent::__construct($namespaces, $locale);
  62. } // end constructor
  63. /**
  64. * fetches the db connection rfom the singleton function
  65. * @uses setSQLiteConn()
  66. * @uses I18Nbase::getI18Nsetting()
  67. * @return resource
  68. */
  69. protected function getConn() {
  70. return setSQLiteConn(parent::getI18Nsetting('locales_path') . '/' . $this->database);
  71. } // end function
  72. /**
  73. * retreive the available translator locales
  74. * @uses I18NtranslatorBase::$locales
  75. * @uses I18NtranslatorSQLite3::getConn()
  76. * @uses I18Nbase::isValidLocaleCode()
  77. * @uses I18Nbase::getI18NfactoryLocale()
  78. * @return boolean
  79. */
  80. protected function setLocales() {
  81. $this->locales = array();
  82. $ps = getPrepStat('locales', $this->getConn(), 'SELECT * FROM ' . $this->table . ' LIMIT 0,1');
  83. $ps->execute();
  84. $result = $ps->fetchAll();
  85. $keys = array_keys($result[0]);
  86. foreach ($keys as $name) {
  87. if (parent::isValidLocaleCode($name) == TRUE) {
  88. $name = str_replace('_', '-', $name);
  89. $this->locales[$name] = parent::getI18NfactoryLocale($name);
  90. $_SESSION['i18n']['translator_locales'][$name] = $name;
  91. } // end if
  92. } // end foreach
  93. return (count($this->locales) > 0) ? TRUE : FALSE;
  94. } // end function
  95. /**
  96. * fetch all translation files and transform them
  97. * @uses I18NtranslatorSQLite3::$use_filecache
  98. * @uses I18Nbase::getFileCache()
  99. * @uses I18NtranslatorBase::$namespaces
  100. * @uses I18Nbase::getTranslatorLocale()
  101. * @uses I18NtranslatorSQLite3::$translation_table
  102. * @return boolean
  103. */
  104. protected function fetchAllTranslations() {
  105. if (self::USE_FILECACHE == TRUE) {
  106. $cache =& parent::getFileCache();
  107. $cache_filename = implode('_', $this->namespaces) . parent::getTranslatorLocale()->getI18Nlocale();
  108. if ($cache->isCached($cache_filename) == TRUE) {
  109. $this->translation_table = unserialize($cache->returnCache($cache_filename));
  110. unset($cache);
  111. return (boolean) TRUE;
  112. } // end if
  113. } // end if
  114. $sql = 'SELECT string, ' . str_replace('-', '_', parent::getTranslatorLocale()->getI18Nlocale());
  115. $sql .= ' FROM ' . $this->table;
  116. $sql .= ' WHERE namespace IN ("' . implode('","', $this->namespaces) . '")';
  117. $sql .= ' ORDER BY string ASC';
  118. $result = $this->getConn()->query($sql);
  119. foreach ($result as $row) {
  120. if ((mb_strlen($row[0]) > 0) && (mb_strlen($row[1]) > 0)) {
  121. parent::checkForDuplicates($row[0], implode(', ', $this->namespaces));
  122. $this->translation_table[$row[0]] = $row[1];
  123. } // end if
  124. } // end foreach
  125. if (self::USE_FILECACHE == TRUE) {
  126. $cache->writeCache($cache_filename, serialize($this->translation_table));
  127. unset($cache);
  128. } // end if
  129. return (boolean) TRUE;
  130. } // end function
  131. /**
  132. * returns an array with $array[translationstring] = translation
  133. * @uses I18NtranslatorSQLite::fetchAllTranslations()
  134. * @return array $array[translationstring] = translation
  135. */
  136. public function getTranslationTable() {
  137. if (!isset($this->translation_table)) {
  138. $this->fetchAllTranslations();
  139. } // end if
  140. return $this->translation_table;
  141. } // end function
  142. /**
  143. * main translator method for translating strings
  144. * @param string $translationstring string to be translated
  145. * @param string $domain alias for namespace (sometimes needed for gettext)
  146. * @param array $arguments whe using translation strings with placeholders, $arguments is an array with the values for the placeholders
  147. * @return string translated tring or error message
  148. * @uses I18NtranslatorSQLite3::getTranslationTable()
  149. * @uses I18NtranslatorSQLite3::$translation_table
  150. * @uses I18NtranslatorSQLite3::getConn()
  151. * @uses I18NtranslatorBase::getTranslatorLocale()
  152. */
  153. public function translate($translationstring = '', $domain = '', $arguments = FALSE) {
  154. $string = trim($translationstring);
  155. if (array_key_exists($string, $this->getTranslationTable())) {
  156. if (!is_array($arguments)) {
  157. return $this->translation_table[$string];
  158. } // end if
  159. return vsprintf($this->translation_table[$string], $arguments);
  160. } // end if
  161. $sql = 'SELECT ' . $this->getTranslatorLocale()->getI18Nlocale();
  162. $sql .= ' FROM ' . $this->table;
  163. $sql .= ' WHERE string = "' . mysql_real_escape_string($string) . '"';
  164. $ps = $this->getConn()->prepare($sql); // uses a view
  165. $ex = $ps->execute();
  166. $result = $ps->fetch();
  167. if ($ps->rowCount() > 0 &&
  168. (($field &= $result[0]) != NULL)) {
  169. $this->translation_table[$string] = $field;
  170. return (string) $this->translation_table[$string];
  171. } // end if
  172. if (parent::getI18Nsetting('show_errormessages')) {
  173. trigger_error('TRANSLATION ERROR: String "' . $string . '" not found in translation table', E_USER_WARNING);
  174. } // end if
  175. return $string;
  176. } // end function
  177. } // end I18NtranslatorSQLite3
  178. ?>