~ubuntu-branches/ubuntu/utopic/zendframework/utopic

« back to all changes in this revision

Viewing changes to library/Zend/Translate/Adapter.php

  • Committer: Bazaar Package Importer
  • Author(s): Frank Habermann
  • Date: 2010-04-28 20:10:00 UTC
  • mfrom: (1.3.1 upstream) (9.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100428201000-o347bj5qb5i3tpot
Tags: 1.10.4-1
new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 * @subpackage Zend_Translate_Adapter
18
18
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
20
 
 * @version    $Id: Adapter.php 20403 2010-01-18 21:29:52Z thomas $
 
20
 * @version    $Id: Adapter.php 21770 2010-04-05 20:16:08Z thomas $
21
21
 */
22
22
 
23
23
/**
66
66
 
67
67
    /**
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
80
81
     */
81
82
    protected $_options = array(
82
83
        'clear'           => false,
 
84
        'content'         => null,
83
85
        'disableNotices'  => false,
84
86
        'ignore'          => '.',
85
87
        'locale'          => 'auto',
99
101
    /**
100
102
     * Generates the adapter
101
103
     *
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
107
106
     * @return void
108
107
     */
109
 
    public function __construct($data = null, $locale = null, array $options = array())
 
108
    public function __construct($options = array())
110
109
    {
 
110
        if ($options instanceof Zend_Config) {
 
111
            $options = $options->toArray();
 
112
        } else if (func_num_args() > 1) {
 
113
            $args               = func_get_args();
 
114
            $options            = array();
 
115
            $options['content'] = array_shift($args);
 
116
 
 
117
            if (!empty($args)) {
 
118
                $options['locale'] = array_shift($args);
 
119
            }
 
120
 
 
121
            if (!empty($args)) {
 
122
                $opt     = array_shift($args);
 
123
                $options = array_merge($opt, $options);
 
124
            }
 
125
        } else if (!is_array($options)) {
 
126
            $options = array('content' => $options);
 
127
        }
 
128
 
111
129
        if (isset(self::$_cache)) {
112
130
            $id = 'Zend_Translate_' . $this->toString() . '_Options';
113
131
            $result = self::$_cache->load($id);
116
134
            }
117
135
        }
118
136
 
119
 
        if (($locale === "auto") or ($locale === null)) {
 
137
        if (empty($options['locale']) || ($options['locale'] === "auto")) {
120
138
            $this->_automatic = true;
121
139
        } else {
122
140
            $this->_automatic = false;
123
141
        }
124
142
 
 
143
        $locale = null;
 
144
        if (!empty($options['locale'])) {
 
145
            $locale = $options['locale'];
 
146
            unset($options['locale']);
 
147
        }
 
148
 
125
149
        $this->setOptions($options);
126
 
        if ($data !== null) {
127
 
            $this->addTranslation($data, $locale, $options);
 
150
        $options['locale'] = $locale;
 
151
 
 
152
        if (!empty($options['content'])) {
 
153
            $this->addTranslation($options);
128
154
        }
129
155
 
130
 
        if ($this->getLocale() !== (string) $locale) {
131
 
            $this->setLocale($locale);
 
156
        if ($this->getLocale() !== (string) $options['locale']) {
 
157
            $this->setLocale($options['locale']);
132
158
        }
133
159
    }
134
160
 
135
161
    /**
136
 
     * Add translation data
137
 
     *
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
141
 
     *
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
 
162
     * Add translations
 
163
     *
 
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
 
167
     *
 
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
148
171
     */
149
 
    public function addTranslation($data, $locale = null, array $options = array())
 
172
    public function addTranslation($options = array())
150
173
    {
151
 
        $originate = (string) $locale;
152
 
        if (array_key_exists('locale', $options)) {
153
 
            if ($locale == null) {
154
 
                $locale = $options['locale'];
155
 
            }
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();
 
178
            $options            = array();
 
179
            $options['content'] = array_shift($args);
 
180
 
 
181
            if (!empty($args)) {
 
182
                $options['locale'] = array_shift($args);
 
183
            }
 
184
 
 
185
            if (!empty($args)) {
 
186
                $opt     = array_shift($args);
 
187
                $options = array_merge($opt, $options);
 
188
            }
 
189
        } else if (!is_array($options)) {
 
190
            $options = array('content' => $options);
 
191
        }
 
192
 
 
193
        $originate = null;
 
194
        if (!empty($options['locale'])) {
 
195
            $originate = (string) $options['locale'];
157
196
        }
158
197
 
159
198
        if ((array_key_exists('log', $options)) && !($options['log'] instanceof Zend_Log)) {
162
201
        }
163
202
 
164
203
        try {
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;
 
207
                }
 
208
 
 
209
                $options['locale'] = Zend_Locale::findLocale($options['locale']);
 
210
            }
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);
169
214
        }
170
215
 
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']);
174
219
            $prev = '';
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
181
 
                    continue;
 
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
 
229
                                continue 2;
 
230
                            }
 
231
                        } else if (strpos($directory, DIRECTORY_SEPARATOR . $ignore) !== false) {
 
232
                            // ignore files matching first characters from option 'ignore' and all files below
 
233
                            continue 2;
 
234
                        }
 
235
                    }
 
236
                } else {
 
237
                    if (strpos($directory, DIRECTORY_SEPARATOR . $options['ignore']) !== false) {
 
238
                        // ignore files matching first characters from option 'ignore' and all files below
 
239
                        continue;
 
240
                    }
182
241
                }
183
242
 
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))) {
187
 
                        $locale = $file;
188
 
                        $prev   = (string) $locale;
 
246
                        $options['locale'] = $file;
 
247
                        $prev              = (string) $options['locale'];
189
248
                    }
190
249
                } else if ($info->isFile()) {
191
250
                    // filename as locale
194
253
                        array_pop($filename);
195
254
                        $filename = implode('.', $filename);
196
255
                        if (Zend_Locale::isLocale((string) $filename, true, false)) {
197
 
                            $locale = (string) $filename;
 
256
                            $options['locale'] = (string) $filename;
198
257
                        } else {
199
258
                            $parts  = explode('.', $file);
200
259
                            $parts2 = array();
212
271
                            foreach($parts as $token) {
213
272
                                if (Zend_Locale::isLocale($token, true, false)) {
214
273
                                    if (strlen($prev) <= strlen($token)) {
215
 
                                        $locale = $token;
216
 
                                        $prev   = $token;
 
274
                                        $options['locale'] = $token;
 
275
                                        $prev              = $token;
217
276
                                    }
218
277
                                }
219
278
                            }
221
280
                    }
222
281
 
223
282
                    try {
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
227
287
                    }
228
288
                }
229
289
            }
230
290
        } else {
231
 
            $this->_addTranslationData($data, (string) $locale, $options);
 
291
            $this->_addTranslationData($options);
232
292
        }
233
293
 
234
294
        if ((isset($this->_translate[$originate]) === true) and (count($this->_translate[$originate]) > 0)) {
446
506
    /**
447
507
     * Internal function for adding translation data
448
508
     *
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
452
512
     *
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
460
517
     */
461
 
    private function _addTranslationData($data, $locale, array $options = array())
 
518
    private function _addTranslationData($options = array())
462
519
    {
 
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);
 
525
 
 
526
            if (!empty($args)) {
 
527
                $options['locale'] = array_shift($args);
 
528
            }
 
529
 
 
530
            if (!empty($args)) {
 
531
                $options += array_shift($args);
 
532
            }
 
533
        }
 
534
 
 
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']);
 
539
            } else {
 
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);
 
545
                }
 
546
 
 
547
                return $this;
 
548
            }
 
549
        }
 
550
 
463
551
        try {
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);
468
556
        }
469
557
 
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();
472
560
        }
473
561
 
474
562
        $read = true;
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);
478
566
            if ($temp) {
479
567
                $read = false;
485
573
        }
486
574
 
487
575
        if ($read) {
488
 
            $temp = $this->_loadTranslationData($data, $locale, $options);
 
576
            if (!empty($options['usetranslateadapter'])) {
 
577
                $temp = array($options['locale'] => $options['content']);
 
578
            } else {
 
579
                $temp = $this->_loadTranslationData($options['content'], $options['locale'], $options);
 
580
            }
489
581
        }
490
582
 
491
583
        if (empty($temp)) {
504
596
        }
505
597
 
506
598
        if ($this->_automatic === true) {
507
 
            $find = new Zend_Locale($locale);
 
599
            $find = new Zend_Locale($options['locale']);
508
600
            $browser = $find->getEnvironment() + $find->getBrowser();
509
601
            arsort($browser);
510
602
            foreach($browser as $language => $quality) {
516
608
        }
517
609
 
518
610
        if (($read) and (isset(self::$_cache))) {
519
 
            $id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString();
 
611
            $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
520
612
            self::$_cache->save($temp, $id, array('Zend_Translate'));
521
613
        }
522
614