PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-1.0.0/library/Zend/Translate/Adapter.php

https://github.com/jorgenils/zend-framework
PHP | 306 lines | 118 code | 40 blank | 148 comment | 27 complexity | f92196572034f9ea9950f5b8704408c2 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Translate
  17. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Date.php 2498 2006-12-23 22:13:38Z thomas $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Locale */
  22. require_once 'Zend/Locale.php';
  23. /** Zend_Translate_Exception */
  24. require_once 'Zend/Translate/Exception.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Translate
  28. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Translate_Adapter {
  32. /**
  33. * Current locale/language
  34. *
  35. * @var string|null
  36. */
  37. protected $_locale;
  38. /**
  39. * Table of all supported languages
  40. *
  41. * @var array
  42. */
  43. protected $_languages = array();
  44. /**
  45. * Array with all options, each adapter can have own additional options
  46. *
  47. * @var array
  48. */
  49. protected $_options = array('clear' => false);
  50. /**
  51. * Translation table
  52. *
  53. * @var array
  54. */
  55. protected $_translate = array();
  56. /**
  57. * Generates the adapter
  58. *
  59. * @param string|array $data Translation data for this adapter
  60. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with Locale identifiers
  61. * see Zend_Locale for more information
  62. * @param string|array $options Options for the adaptor
  63. * @throws Zend_Translate_Exception
  64. */
  65. public function __construct($data, $locale = null, array $options = array())
  66. {
  67. if ($locale === null) {
  68. $locale = new Zend_Locale();
  69. }
  70. $this->addTranslation($data, $locale, $options);
  71. $this->setLocale($locale);
  72. }
  73. /**
  74. * Sets new adapter options
  75. *
  76. * @param array $options Adapter options
  77. * @throws Zend_Translate_Exception
  78. */
  79. public function setOptions(array $options = array())
  80. {
  81. foreach ($options as $key => $option) {
  82. $this->_options[strtolower($key)] = $option;
  83. }
  84. }
  85. /**
  86. * Returns the adapters name and it's options
  87. *
  88. * @param string|null $optionKey String returns this option
  89. * null returns all options
  90. * @return integer|string|array
  91. */
  92. public function getOptions($optionKey = null)
  93. {
  94. if ($optionKey === null) {
  95. return $this->_options;
  96. }
  97. if (array_key_exists(strtolower($optionKey), $this->_options)) {
  98. return $this->_options[strtolower($optionKey)];
  99. }
  100. return null;
  101. }
  102. /**
  103. * Gets locale
  104. *
  105. * @return Zend_Locale|null
  106. */
  107. public function getLocale()
  108. {
  109. return $this->_locale;
  110. }
  111. /**
  112. * Sets locale
  113. *
  114. * @param string|Zend_Locale $locale Locale to set
  115. * @throws Zend_Translate_Exception
  116. */
  117. public function setLocale($locale)
  118. {
  119. if ($locale instanceof Zend_Locale) {
  120. $locale = $locale->toString();
  121. } else if (!$locale = Zend_Locale::isLocale($locale)) {
  122. throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist");
  123. }
  124. if (!in_array($locale, $this->_languages)) {
  125. throw new Zend_Translate_Exception("Language ({$locale}) has to be added before it can be used.");
  126. }
  127. $this->_locale = $locale;
  128. }
  129. /**
  130. * Returns the avaiable languages from this adapter
  131. *
  132. * @return array
  133. */
  134. public function getList()
  135. {
  136. return $this->_languages;
  137. }
  138. /**
  139. * Is the wished language avaiable ?
  140. *
  141. * @param string|Zend_Locale $locale Language to search for, identical with locale identifier,
  142. * see Zend_Locale for more information
  143. * @return boolean
  144. */
  145. public function isAvailable($locale)
  146. {
  147. if ($locale instanceof Zend_Locale) {
  148. $locale = $locale->toString();
  149. }
  150. return in_array($locale, $this->_languages);
  151. }
  152. /**
  153. * Load translation data
  154. *
  155. * @param mixed $data
  156. * @param string|Zend_Locale $locale
  157. * @param array $options
  158. */
  159. abstract protected function _loadTranslationData($data, $locale, array $options = array());
  160. /**
  161. * Add translation data
  162. *
  163. * It may be a new language or additional data for existing language
  164. * If $clear parameter is true, then translation data for specified
  165. * language is replaced and added otherwise
  166. *
  167. * @param array|string $data Translation data
  168. * @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier,
  169. * see Zend_Locale for more information
  170. * @param array $options OPTIONAL Option for this Adapter
  171. * @throws Zend_Translate_Exception
  172. */
  173. public function addTranslation($data, $locale, array $options = array())
  174. {
  175. if (!$locale = Zend_Locale::isLocale($locale)) {
  176. throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist");
  177. }
  178. if (!in_array($locale, $this->_languages)) {
  179. $this->_languages[$locale] = $locale;
  180. }
  181. $this->_loadTranslationData($data, $locale, $options);
  182. }
  183. /**
  184. * Translates the given string
  185. * returns the translation
  186. *
  187. * @param string $messageId Translation string
  188. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to use, identical with locale identifier,
  189. * see Zend_Locale for more information
  190. * @return string
  191. */
  192. public function translate($messageId, $locale = null)
  193. {
  194. if ($locale === null) {
  195. $locale = $this->_locale;
  196. } else {
  197. if (!$locale = Zend_Locale::isLocale($locale)) {
  198. // language does not exist, return original string
  199. return $messageId;
  200. }
  201. }
  202. if ((array_key_exists($locale, $this->_translate)) and
  203. (array_key_exists($messageId, $this->_translate[$locale]))) {
  204. // return original translation
  205. return $this->_translate[$locale][$messageId];
  206. } else if (strlen($locale) != 2) {
  207. // faster than creating a new locale and separate the leading part
  208. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  209. if ((array_key_exists($locale, $this->_translate)) and
  210. (array_key_exists($messageId, $this->_translate[$locale]))) {
  211. // return regionless translation (en_US -> en)
  212. return $this->_translate[$locale][$messageId];
  213. }
  214. }
  215. // no translation found, return original
  216. return $messageId;
  217. }
  218. /**
  219. * Checks if a string is translated within the source or not
  220. * returns boolean
  221. *
  222. * @param string $messageId Translation string
  223. * @param boolean $original OPTIONAL Allow translation only for original language
  224. * when true, a translation for 'en_US' would give false when it can
  225. * be translated with 'en' only
  226. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to use, identical with locale identifier,
  227. * see Zend_Locale for more information
  228. * @return boolean
  229. */
  230. public function isTranslated($messageId, $original = false, $locale = null)
  231. {
  232. if (($original !== false) and ($original !== true)) {
  233. $locale = $original;
  234. $original = false;
  235. }
  236. if ($locale === null) {
  237. $locale = $this->_locale;
  238. } else {
  239. if (!$locale = Zend_Locale::isLocale($locale)) {
  240. // language does not exist, return original string
  241. return false;
  242. }
  243. }
  244. if ((array_key_exists($locale, $this->_translate)) and
  245. (array_key_exists($messageId, $this->_translate[$locale]))) {
  246. // return original translation
  247. return true;
  248. } else if ((strlen($locale) != 2) and ($original === false)) {
  249. // faster than creating a new locale and separate the leading part
  250. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  251. if ((array_key_exists($locale, $this->_translate)) and
  252. (array_key_exists($messageId, $this->_translate[$locale]))) {
  253. // return regionless translation (en_US -> en)
  254. return true;
  255. }
  256. }
  257. // no translation found, return original
  258. return false;
  259. }
  260. /**
  261. * Returns the adapter name
  262. *
  263. * @return string
  264. */
  265. abstract public function toString();
  266. }