示例#1
0
 protected function _addTranslationData($data, $locale, array $options = array())
 {
     try {
         $locale = Zend_Locale::findLocale($locale);
     } catch (Zend_Locale_Exception $e) {
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist");
     }
     if ($options['clear'] || !isset($this->_translate[$locale])) {
         $this->_translate[$locale] = array();
     }
     $ext = strtolower(ltrim(strrchr($data, '.'), '.'));
     if (!isset($this->_extToParser[$ext])) {
         return false;
     }
     $class = $this->_extToParser[$ext];
     $temp = call_user_func(array($class, 'parse'), $data, $locale, $options);
     if (empty($temp)) {
         $temp = array();
     }
     $keys = array_keys($temp);
     foreach ($keys as $key) {
         if (!isset($this->_translate[$key])) {
             $this->_translate[$key] = array();
         }
         $this->_translate[$key] = $temp[$key] + $this->_translate[$key];
     }
     if ($this->_automatic === true) {
         $find = new Zend_Locale($locale);
         $browser = $find->getEnvironment() + $find->getBrowser();
         arsort($browser);
         foreach ($browser as $language => $quality) {
             if (isset($this->_translate[$language])) {
                 $this->_options['locale'] = $language;
                 break;
             }
         }
     }
     return $this;
 }
示例#2
0
 /**
  * Internal function for adding translation data
  *
  * It may be a new language or additional data for existing language
  * If $clear parameter is true, then translation data for specified
  * language is replaced and added otherwise
  *
  * @see    Zend_Locale
  * @param  array|string       $data    Translation data
  * @param  string|Zend_Locale $locale  Locale/Language to add data for, identical with locale identifier,
  *                                     @see Zend_Locale for more information
  * @param  array              $options (optional) Option for this Adapter
  * @throws Zend_Translate_Exception
  * @return Zend_Translate_Adapter Provides fluent interface
  */
 private function _addTranslationData($data, $locale, array $options = array())
 {
     try {
         $locale = Zend_Locale::findLocale($locale);
     } catch (Zend_Locale_Exception $e) {
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
     }
     if ($options['clear'] || !isset($this->_translate[$locale])) {
         $this->_translate[$locale] = array();
     }
     $read = true;
     if (isset(self::$_cache)) {
         $id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString();
         $temp = self::$_cache->load($id);
         if ($temp) {
             $read = false;
         }
     }
     if ($options['reload']) {
         $read = true;
     }
     if ($read) {
         $temp = $this->_loadTranslationData($data, $locale, $options);
     }
     if (empty($temp)) {
         $temp = array();
     }
     $keys = array_keys($temp);
     foreach ($keys as $key) {
         if (!isset($this->_translate[$key])) {
             $this->_translate[$key] = array();
         }
         if (array_key_exists($key, $temp) && is_array($temp[$key])) {
             $this->_translate[$key] = $temp[$key] + $this->_translate[$key];
         }
     }
     if ($this->_automatic === true) {
         $find = new Zend_Locale($locale);
         $browser = $find->getEnvironment() + $find->getBrowser();
         arsort($browser);
         foreach ($browser as $language => $quality) {
             if (isset($this->_translate[$language])) {
                 $this->_options['locale'] = $language;
                 break;
             }
         }
     }
     if ($read and isset(self::$_cache)) {
         $id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString();
         self::$_cache->save($temp, $id, array('Zend_Translate'));
     }
     return $this;
 }
示例#3
0
 /**
  * Internal function for adding translation data
  *
  * It may be a new language or additional data for existing language
  * If $clear parameter is true, then translation data for specified
  * language is replaced and added otherwise
  *
  * @see    Zend_Locale
  * @param  array|string       $data    Translation data
  * @param  string|Zend_Locale $locale  Locale/Language to add data for, identical with locale identifier,
  *                                     @see Zend_Locale for more information
  * @param  array              $options (optional) Option for this Adapter
  * @throws Zend_Translate_Exception
  * @return Zend_Translate_Adapter Provides fluent interface
  */
 private function _addTranslationData($data, $locale, array $options = array())
 {
     if (!Zend_Locale::isLocale($locale, true, false)) {
         if (!Zend_Locale::isLocale($locale, false, false)) {
             /**
              * @see Zend_Translate_Exception
              */
             require_once 'Zend/Translate/Exception.php';
             throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist");
         }
         $locale = new Zend_Locale($locale);
     }
     $locale = (string) $locale;
     if (isset($this->_translate[$locale]) === false) {
         $this->_translate[$locale] = array();
     }
     $read = true;
     if (isset(self::$_cache)) {
         $id = 'Zend_Translate_' . preg_replace('/[^a-zA-Z0-9_]/', '_', $data) . '_' . $locale . '_' . $this->toString();
         $result = self::$_cache->load($id);
         if ($result) {
             $this->_translate[$locale] = unserialize($result);
             $read = false;
         }
     }
     if ($read) {
         $this->_loadTranslationData($data, $locale, $options);
     }
     if ($this->_automatic === true) {
         $find = new Zend_Locale($locale);
         $browser = $find->getEnvironment() + $find->getBrowser();
         arsort($browser);
         foreach ($browser as $language => $quality) {
             if (isset($this->_translate[$language]) === true) {
                 $this->_options['locale'] = $language;
                 break;
             }
         }
     }
     if ($read and isset(self::$_cache)) {
         $id = 'Zend_Translate_' . preg_replace('/[^a-zA-Z0-9_]/', '_', $data) . '_' . $locale . '_' . $this->toString();
         self::$_cache->save(serialize($this->_translate[$locale]), $id);
     }
     return $this;
 }
示例#4
0
 /**
  * test getEnvironment
  * expected true
  */
 public function testEnvironment()
 {
     $value = new Zend_Locale();
     $default = $value->getEnvironment();
     $this->assertTrue(is_array($default));
 }
示例#5
0
 /**
  * Internal function for adding translation data
  *
  * This may be a new language or additional data for an existing language
  * If the options 'clear' is true, then the translation data for the specified
  * language is replaced and added otherwise
  *
  * @see    Zend_Locale
  * @param  array|Zend_Config $content Translation data to add
  * @throws Zend_Translate_Exception
  * @return Zend_Translate_Adapter Provides fluent interface
  */
 private function _addTranslationData($options = array())
 {
     if ($options instanceof Zend_Config) {
         $options = $options->toArray();
     } else {
         if (func_num_args() > 1) {
             $args = func_get_args();
             $options['content'] = array_shift($args);
             if (!empty($args)) {
                 $options['locale'] = array_shift($args);
             }
             if (!empty($args)) {
                 $options += array_shift($args);
             }
         }
     }
     if ($options['content'] instanceof Zend_Translate || $options['content'] instanceof Zend_Translate_Adapter) {
         $options['usetranslateadapter'] = true;
         if (!empty($options['locale']) && $options['locale'] !== 'auto') {
             $options['content'] = $options['content']->getMessages($options['locale']);
         } else {
             $content = $options['content'];
             $locales = $content->getList();
             foreach ($locales as $locale) {
                 $options['locale'] = $locale;
                 $options['content'] = $content->getMessages($locale);
                 $this->_addTranslationData($options);
             }
             return $this;
         }
     }
     try {
         $options['locale'] = Zend_Locale::findLocale($options['locale']);
     } catch (Zend_Locale_Exception $e) {
         #require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception("The given Language '{$options['locale']}' does not exist", 0, $e);
     }
     if ($options['clear'] || !isset($this->_translate[$options['locale']])) {
         $this->_translate[$options['locale']] = array();
     }
     $read = true;
     if (isset(self::$_cache)) {
         $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
         $temp = self::$_cache->load($id);
         if ($temp) {
             $read = false;
         }
     }
     if ($options['reload']) {
         $read = true;
     }
     if ($read) {
         if (!empty($options['usetranslateadapter'])) {
             $temp = array($options['locale'] => $options['content']);
         } else {
             $temp = $this->_loadTranslationData($options['content'], $options['locale'], $options);
         }
     }
     if (empty($temp)) {
         $temp = array();
     }
     $keys = array_keys($temp);
     foreach ($keys as $key) {
         if (!isset($this->_translate[$key])) {
             $this->_translate[$key] = array();
         }
         if (array_key_exists($key, $temp) && is_array($temp[$key])) {
             $this->_translate[$key] = $temp[$key] + $this->_translate[$key];
         }
     }
     if ($this->_automatic === true) {
         $find = new Zend_Locale($options['locale']);
         $browser = $find->getEnvironment() + $find->getBrowser();
         arsort($browser);
         foreach ($browser as $language => $quality) {
             if (isset($this->_translate[$language])) {
                 $this->_options['locale'] = $language;
                 break;
             }
         }
     }
     if ($read and isset(self::$_cache)) {
         $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
         if (self::$_cacheTags) {
             self::$_cache->save($temp, $id, array($this->_options['tag']));
         } else {
             self::$_cache->save($temp, $id);
         }
     }
     return $this;
 }
示例#6
0
 /**
  * Internal function for adding translation data
  *
  * It may be a new language or additional data for existing language
  * If $clear parameter is true, then translation data for specified
  * language is replaced and added otherwise
  *
  * @see    Zend_Locale
  * @param  array|string       $data    Translation data
  * @param  string|Zend_Locale $locale  Locale/Language to add data for, identical with locale identifier,
  *                                     @see Zend_Locale for more information
  * @param  array              $options (optional) Option for this Adapter
  * @throws Zend_Translate_Exception
  * @return Zend_Translate_Adapter Provides a fluid interface
  */
 private function _addTranslationData($data, $locale, array $options = array())
 {
     if (!($locale = Zend_Locale::isLocale($locale))) {
         /**
          * @see Zend_Translate_Exception
          */
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist");
     }
     if (!array_key_exists($locale, $this->_translate)) {
         $this->_translate[$locale] = array();
     }
     $this->_loadTranslationData($data, $locale, $options);
     if ($this->_automatic === true) {
         $find = new Zend_Locale($locale);
         $browser = $find->getBrowser() + $find->getEnvironment();
         arsort($browser);
         foreach ($browser as $language => $quality) {
             if (array_key_exists($language, $this->_translate)) {
                 $this->_options['locale'] = $language;
                 break;
             }
         }
     }
     if (isset(self::$_cache)) {
         $id = 'Zend_Translate_' . $this->toString();
         $temp = $this->_translate;
         $temp['_options_'] = $this->_options;
         self::$_cache->save(serialize($temp), $id);
     }
     return $this;
 }
示例#7
0
    /**
     * Add translation data
     *
     * It may be a new language or additional data for existing language
     * If $clear parameter is true, then translation data for specified
     * language is replaced and added otherwise
     *
     * @param  array|string          $data    Translation data
     * @param  string|Zend_Locale    $locale  Locale/Language to add data for, identical with locale identifier,
     *                                        see Zend_Locale for more information
     * @param  array                 $options OPTIONAL Option for this Adapter
     * @throws Zend_Translate_Exception
     */
    public function addTranslation($data, $locale, array $options = array())
    {
        if (!$locale = Zend_Locale::isLocale($locale)) {
            throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist");
        }

        if (!in_array($locale, $this->_languages)) {
            $this->_languages[$locale] = $locale;
        }

        $this->_loadTranslationData($data, $locale, $options);
        if ($this->_automatic === true) {
            $find = new Zend_Locale($locale);
            $browser = $find->getBrowser() + $find->getEnvironment();
            arsort($browser);
            foreach($browser as $language => $quality) {
                if (in_array($language, $this->_languages)) {
                    $this->_locale = $language;
                    break;
                }
            }
        }
    }
示例#8
0
 /**
  * test isLocale
  * expected boolean
  */
 public function testIsLocale()
 {
     $locale = new Zend_LocaleTestHelper('ar');
     $this->assertTrue(Zend_LocaleTestHelper::isLocale($locale));
     $this->assertTrue(Zend_LocaleTestHelper::isLocale('de'));
     $this->assertTrue(Zend_LocaleTestHelper::isLocale('de_AT'));
     $this->assertTrue(Zend_LocaleTestHelper::isLocale('de_xx'));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale('yy'));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale(1234));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale('', true));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale('', false));
     $this->assertTrue(Zend_LocaleTestHelper::isLocale('auto'));
     $this->assertTrue(Zend_LocaleTestHelper::isLocale('browser'));
     if (count(Zend_Locale::getEnvironment()) != 0) {
         $this->assertTrue(Zend_LocaleTestHelper::isLocale('environment'));
     }
     set_error_handler(array($this, 'errorHandlerIgnore'));
     Zend_LocaleTestHelper::$compatibilityMode = true;
     $this->assertEquals('ar', Zend_LocaleTestHelper::isLocale($locale));
     $this->assertEquals('de', Zend_LocaleTestHelper::isLocale('de'));
     $this->assertEquals('de_AT', Zend_LocaleTestHelper::isLocale('de_AT'));
     $this->assertEquals('de', Zend_LocaleTestHelper::isLocale('de_xx'));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale('yy'));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale(1234));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale('', true));
     $this->assertFalse(Zend_LocaleTestHelper::isLocale('', false));
     $this->assertTrue(is_string(Zend_LocaleTestHelper::isLocale('auto')));
     $this->assertTrue(is_string(Zend_LocaleTestHelper::isLocale('browser')));
     if (count(Zend_Locale::getEnvironment()) != 0) {
         $this->assertTrue(is_string(Zend_LocaleTestHelper::isLocale('environment')));
     }
     restore_error_handler();
 }
示例#9
0
 /**
  * Add translation data
  *
  * It may be a new language or additional data for existing language
  * If $clear parameter is true, then translation data for specified
  * language is replaced and added otherwise
  *
  * @param  array|string          $data    Translation data
  * @param  string|Zend_Locale    $locale  Locale/Language to add data for, identical with locale identifier,
  *                                        see Zend_Locale for more information
  * @param  array                 $options OPTIONAL Option for this Adapter
  * @throws Zend_Translate_Exception
  */
 public function addTranslation($data, $locale, array $options = array())
 {
     if (!($locale = Zend_Locale::isLocale($locale))) {
         throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist");
     }
     if (!array_key_exists($locale, $this->_translate)) {
         $this->_translate[$locale] = array();
     }
     $this->_loadTranslationData($data, $locale, $options);
     if ($this->_automatic === true) {
         $find = new Zend_Locale($locale);
         $browser = $find->getBrowser() + $find->getEnvironment();
         arsort($browser);
         foreach ($browser as $language => $quality) {
             if (array_key_exists($language, $this->_translate)) {
                 $this->_locale = $language;
                 break;
             }
         }
     }
     if (isset(self::$_cache)) {
         $id = 'Zend_Translate_' . $this->toString();
         self::$_cache->save(serialize($this->_translate), $id);
     }
 }
示例#10
0
文件: test.php 项目: Tony133/zf-web
<?php

require_once "Zend/Locale.php";
echo "Setting locale to: " . setlocale(LC_ALL, 'C') . "\n";
echo "Setting default to de_DE\n";
Zend_Locale::setDefault('de_DE');
echo "Environment: ";
var_dump(Zend_Locale::getEnvironment());
$locale = new Zend_Locale('auto');
echo "This should be de_DE: {$locale}\n";