68
68
* Array with all options, each adapter can have own additional options
69
* 'clear' => when true, clears already loaded data when adding new files
69
* 'clear' => when true, clears already loaded translations when adding new files
70
* 'content' => content to translate or file or directory with content
70
71
* 'disableNotices' => when true, omits notices from being displayed
71
72
* 'ignore' => a prefix for files and directories which are not being added
72
73
* 'locale' => the actual set locale to use
100
102
* Generates the adapter
102
* @param string|array $data OPTIONAL Translation data or filename for this adapter
103
* @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with Locale
104
* identifiers see Zend_Locale for more information
105
* @param array $options OPTIONAL Options for the adaptor
104
* @param array|Zend_Config $options Translation options for this adapter
106
105
* @throws Zend_Translate_Exception
109
public function __construct($data = null, $locale = null, array $options = array())
108
public function __construct($options = array())
110
if ($options instanceof Zend_Config) {
111
$options = $options->toArray();
112
} else if (func_num_args() > 1) {
113
$args = func_get_args();
115
$options['content'] = array_shift($args);
118
$options['locale'] = array_shift($args);
122
$opt = array_shift($args);
123
$options = array_merge($opt, $options);
125
} else if (!is_array($options)) {
126
$options = array('content' => $options);
111
129
if (isset(self::$_cache)) {
112
130
$id = 'Zend_Translate_' . $this->toString() . '_Options';
113
131
$result = self::$_cache->load($id);
119
if (($locale === "auto") or ($locale === null)) {
137
if (empty($options['locale']) || ($options['locale'] === "auto")) {
120
138
$this->_automatic = true;
122
140
$this->_automatic = false;
144
if (!empty($options['locale'])) {
145
$locale = $options['locale'];
146
unset($options['locale']);
125
149
$this->setOptions($options);
126
if ($data !== null) {
127
$this->addTranslation($data, $locale, $options);
150
$options['locale'] = $locale;
152
if (!empty($options['content'])) {
153
$this->addTranslation($options);
130
if ($this->getLocale() !== (string) $locale) {
131
$this->setLocale($locale);
156
if ($this->getLocale() !== (string) $options['locale']) {
157
$this->setLocale($options['locale']);
136
* Add translation data
138
* It may be a new language or additional data for existing language
139
* If $clear parameter is true, then translation data for specified
140
* language is replaced and added otherwise
142
* @param array|string $data Translation data
143
* @param string|Zend_Locale $locale (optional) Locale/Language to add data for, identical
144
* with locale identifier, see Zend_Locale for more information
145
* @param array $options (optional) Option for this Adapter
164
* This may be a new language or additional content for an existing language
165
* If the key 'clear' is true, then translations for the specified
166
* language will be replaced and added otherwise
168
* @param array|Zend_Config $options Options and translations to be added
146
169
* @throws Zend_Translate_Exception
147
170
* @return Zend_Translate_Adapter Provides fluent interface
149
public function addTranslation($data, $locale = null, array $options = array())
172
public function addTranslation($options = array())
151
$originate = (string) $locale;
152
if (array_key_exists('locale', $options)) {
153
if ($locale == null) {
154
$locale = $options['locale'];
156
unset($options['locale']);
174
if ($options instanceof Zend_Config) {
175
$options = $options->toArray();
176
} else if (func_num_args() > 1) {
177
$args = func_get_args();
179
$options['content'] = array_shift($args);
182
$options['locale'] = array_shift($args);
186
$opt = array_shift($args);
187
$options = array_merge($opt, $options);
189
} else if (!is_array($options)) {
190
$options = array('content' => $options);
194
if (!empty($options['locale'])) {
195
$originate = (string) $options['locale'];
159
198
if ((array_key_exists('log', $options)) && !($options['log'] instanceof Zend_Log)) {
165
$locale = Zend_Locale::findLocale($locale);
204
if (!($options['content'] instanceof Zend_Translate) && !($options['content'] instanceof Zend_Translate_Adapter)) {
205
if (empty($options['locale'])) {
206
$options['locale'] = null;
209
$options['locale'] = Zend_Locale::findLocale($options['locale']);
166
211
} catch (Zend_Locale_Exception $e) {
167
212
require_once 'Zend/Translate/Exception.php';
168
throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
213
throw new Zend_Translate_Exception("The given Language '{$options['locale']}' does not exist", 0, $e);
171
216
$options = $options + $this->_options;
172
if (is_string($data) and is_dir($data)) {
173
$data = realpath($data);
217
if (is_string($options['content']) and is_dir($options['content'])) {
218
$options['content'] = realpath($options['content']);
175
220
foreach (new RecursiveIteratorIterator(
176
new RecursiveDirectoryIterator($data, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
221
new RecursiveDirectoryIterator($options['content'], RecursiveDirectoryIterator::KEY_AS_PATHNAME),
177
222
RecursiveIteratorIterator::SELF_FIRST) as $directory => $info) {
178
223
$file = $info->getFilename();
179
if (strpos($directory, DIRECTORY_SEPARATOR . $options['ignore']) !== false) {
180
// ignore files matching first characters from option 'ignore' and all files below
224
if (is_array($options['ignore'])) {
225
foreach ($options['ignore'] as $key => $ignore) {
226
if (strpos($key, 'regex') !== false) {
227
if (preg_match($ignore, $directory)) {
228
// ignore files matching the given regex from option 'ignore' and all files below
231
} else if (strpos($directory, DIRECTORY_SEPARATOR . $ignore) !== false) {
232
// ignore files matching first characters from option 'ignore' and all files below
237
if (strpos($directory, DIRECTORY_SEPARATOR . $options['ignore']) !== false) {
238
// ignore files matching first characters from option 'ignore' and all files below
184
243
if ($info->isDir()) {
185
244
// pathname as locale
186
245
if (($options['scan'] === self::LOCALE_DIRECTORY) and (Zend_Locale::isLocale($file, true, false))) {
188
$prev = (string) $locale;
246
$options['locale'] = $file;
247
$prev = (string) $options['locale'];
190
249
} else if ($info->isFile()) {
191
250
// filename as locale
224
$this->_addTranslationData($info->getPathname(), (string) $locale, $options);
283
$options['content'] = $info->getPathname();
284
$this->_addTranslationData($options);
225
285
} catch (Zend_Translate_Exception $e) {
226
286
// ignore failed sources while scanning
231
$this->_addTranslationData($data, (string) $locale, $options);
291
$this->_addTranslationData($options);
234
294
if ((isset($this->_translate[$originate]) === true) and (count($this->_translate[$originate]) > 0)) {
447
507
* Internal function for adding translation data
449
* It may be a new language or additional data for existing language
450
* If $clear parameter is true, then translation data for specified
509
* This may be a new language or additional data for an existing language
510
* If the options 'clear' is true, then the translation data for the specified
451
511
* language is replaced and added otherwise
453
513
* @see Zend_Locale
454
* @param array|string $data Translation data
455
* @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier,
456
* @see Zend_Locale for more information
457
* @param array $options (optional) Option for this Adapter
514
* @param array|Zend_Config $content Translation data to add
458
515
* @throws Zend_Translate_Exception
459
516
* @return Zend_Translate_Adapter Provides fluent interface
461
private function _addTranslationData($data, $locale, array $options = array())
518
private function _addTranslationData($options = array())
520
if ($options instanceof Zend_Config) {
521
$options = $options->toArray();
522
} else if (func_num_args() > 1) {
523
$args = func_get_args();
524
$options['content'] = array_shift($args);
527
$options['locale'] = array_shift($args);
531
$options += array_shift($args);
535
if (($options['content'] instanceof Zend_Translate) || ($options['content'] instanceof Zend_Translate_Adapter)) {
536
$options['usetranslateadapter'] = true;
537
if (!empty($options['locale'])) {
538
$options['content'] = $options['content']->getMessages($options['locale']);
540
$locales = $options['content']->getList();
541
foreach ($locales as $locale) {
542
$options['locale'] = $locale;
543
$options['content'] = $options['content']->getMessages($locale);
544
$this->_addTranslationData($options);
464
$locale = Zend_Locale::findLocale($locale);
552
$options['locale'] = Zend_Locale::findLocale($options['locale']);
465
553
} catch (Zend_Locale_Exception $e) {
466
554
require_once 'Zend/Translate/Exception.php';
467
throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
555
throw new Zend_Translate_Exception("The given Language '{$options['locale']}' does not exist", 0, $e);
470
if ($options['clear'] || !isset($this->_translate[$locale])) {
471
$this->_translate[$locale] = array();
558
if ($options['clear'] || !isset($this->_translate[$options['locale']])) {
559
$this->_translate[$options['locale']] = array();
475
563
if (isset(self::$_cache)) {
476
$id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString();
564
$id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
477
565
$temp = self::$_cache->load($id);