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

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

https://github.com/jorgenils/zend-framework
PHP | 261 lines | 95 code | 35 blank | 131 comment | 20 complexity | 9927f2797dbd94eccbbc215cc947c83d 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)) {
  203. if (array_key_exists($messageId, $this->_translate[$locale])) {
  204. // return original translation
  205. return $this->_translate[$locale][$messageId];
  206. }
  207. } else if (strlen($locale) != 2) {
  208. // faster than creating a new locale and separate the leading part
  209. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  210. if (array_key_exists($locale, $this->_translate)) {
  211. if (array_key_exists($messageId, $this->_translate[$locale])) {
  212. // return regionless translation (en_US -> en)
  213. return $this->_translate[$locale][$messageId];
  214. }
  215. }
  216. }
  217. // no translation found, return original
  218. return $messageId;
  219. }
  220. /**
  221. * Returns the adapter name
  222. *
  223. * @return string
  224. */
  225. abstract public function toString();
  226. }