Array.php
00001 <?php
00010 class Dsao_Translate_Adapter_Array extends Zend_Translate_Adapter_Array
00011 {
00015 protected $_localeList = null;
00016
00024 protected function _lazyLoadTranslationData($idMessage, $locale)
00025 {
00026
00027 if (!$this->isAvailable($locale))
00028 {
00029
00030 $locale = $this->getLocale();
00031 }
00032
00033
00034
00035
00036
00037
00038 $arrayMessage = explode('_', $idMessage, 3);
00039
00040
00041
00042
00043
00044 if (!isset($this->_translate[$locale][$arrayMessage[0]]))
00045 {
00046
00047 $fileModule = BASE_DIR.'lang/'.$locale.'/'.$arrayMessage[0].'.php';
00048
00049
00050
00051
00052 if (!$this->_loadTranslationData($fileModule, $locale))
00053 {
00054 return false;
00055 }
00056
00057 else if (isset($this->_translate[$locale][$idMessage]))
00058 {
00059 return true;
00060 }
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 $fileControllerPhp = BASE_DIR.'lang/'.$locale.'/php/'.$arrayMessage[0].
00072 '/'.$arrayMessage[1].'.php';
00073 $fileControllerTemplate = BASE_DIR.'lang/'.$locale.'/template/'.
00074 $arrayMessage[0].'/'.$arrayMessage[1].'.php';
00075
00076
00077
00078 $this->_loadTranslationData($fileControllerPhp, $locale);
00079 $this->_loadTranslationData($fileControllerTemplate, $locale);
00080
00081
00082
00083 return isset($this->_translate[$locale][$idMessage]);
00084 }
00085
00098 protected function _loadTranslationData($data, $locale, array $options = array())
00099 {
00100
00101 if (!is_array($data))
00102 {
00103
00104 if (file_exists($data))
00105 {
00106
00107
00108 ob_start();
00109
00110
00111
00112
00113
00114
00115
00116
00117 return $this->addTranslation((array) include_once($data), $locale);
00118 ob_end_clean();
00119 }
00120
00121 else
00122 {
00123 return array();
00124 }
00125 }
00126
00127 return parent::_loadTranslationData($data, $locale, $options);
00128 }
00129
00137 public function _($idMessage, $locale = null)
00138 {
00139
00140 if (null == $locale)
00141 {
00142 $locale = $this->_options['locale'];
00143 }
00144
00145
00146 if (!isset($this->_translate[$locale][$idMessage]))
00147 {
00148
00149 $this->_lazyLoadTranslationData($idMessage, $locale);
00150 }
00151
00152 return parent::_($idMessage, $locale);
00153 }
00154
00161 public function addLayoutTranslationData($layout)
00162 {
00163
00164 $fileLayoutTranslation = BASE_DIR.'lang/'.
00165 $this->_options['locale'].'/layout/'.$layout.'.php';
00166
00167
00168 return $this->_loadTranslationData
00169 ($fileLayoutTranslation, $this->_options['locale']);
00170 }
00171
00181 public function getList()
00182 {
00183
00184 if (null === $this->_localeList)
00185 {
00186
00187 $this->_localeList = array();
00188
00189
00190 $directory = new DirectoryIterator(BASE_DIR.'lang/');
00191
00192
00193 foreach ($directory as $file)
00194 {
00195
00196
00197 if (!$file->isDot()
00198 && $file->isDir()
00199 && Zend_Locale::isLocale($file->getFilename()))
00200 {
00201 $this->_localeList[] = $file->getFilename();
00202 }
00203 }
00204 }
00205
00206 return $this->_localeList;
00207 }
00208
00215 public function isAvailable($locale)
00216 {
00217
00218
00219 return (parent::isAvailable($locale) ? true :
00220 in_array($locale, $this->getList()));
00221 }
00222
00232 public function setLocale($locale)
00233 {
00234
00235 if (!$this->isAvailable($locale))
00236 {
00237 $locale = array_keys(Zend_Locale::getDefault());
00238 $locale = $locale[0];
00239 }
00240
00241 parent::setLocale($locale);
00242 }
00243 }