Пример #1
0
 /**
  * Translate a message
  * You can give multiple params or an array of params.
  * If you want to output another locale just set it as last single parameter
  * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
  *
  * @param  string $messageid Id of the message to be translated
  * @return string|\Zend\View\Helper\Translate Translated message
  */
 public function direct($messageid = null)
 {
     if ($messageid === null) {
         return $this;
     }
     $translate = $this->getTranslator();
     $options = func_get_args();
     array_shift($options);
     $count = count($options);
     $locale = null;
     if ($count > 0) {
         if (\Zend\Locale\Locale::isLocale($options[$count - 1]) !== false) {
             $locale = array_pop($options);
         }
     }
     if (count($options) === 1 and is_array($options[0]) === true) {
         $options = $options[0];
     }
     if ($translate !== null) {
         $messageid = $translate->translate($messageid, $locale);
     }
     if (count($options) === 0) {
         return $messageid;
     }
     return vsprintf($messageid, $options);
 }
Пример #2
0
 /**
  * Tests if the given language is really a language
  */
 public function testIsLocale()
 {
     foreach ($this->_languages as $lang) {
         if (!Locale::isLocale($lang, true)) {
             $this->fail("Language directory '{$lang}' not a valid locale");
         }
     }
 }
Пример #3
0
 /**
  * Output a formatted currency
  *
  * @param  integer|float                    $value    Currency value to output
  * @param  string|Zend_Locale|\Zend\Currency\Currency $currency OPTIONAL Currency to use for this call
  * @return string Formatted currency
  */
 public function direct($value = null, $currency = null)
 {
     if ($value === null) {
         return $this;
     }
     if (is_string($currency) || $currency instanceof Locale\Locale) {
         if (Locale\Locale::isLocale($currency)) {
             $currency = array('locale' => $currency);
         }
     }
     if (is_string($currency)) {
         $currency = array('currency' => $currency);
     }
     if (is_array($currency)) {
         return $this->_currency->toCurrency($value, $currency);
     }
     return $this->_currency->toCurrency($value);
 }
Пример #4
0
 /**
  * Checks if the given date is a real date or datepart.
  * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  * But the check will only be done for the expected dateparts which are given by format.
  * If no format is given the standard dateformat for the actual locale is used.
  * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  *
  * @param  string|array|\Zend\Date\Date $date   Date to parse for correctness
  * @param  string                 $format (Optional) Format for parsing the date string
  * @param  string|\Zend\Locale\Locale     $locale (Optional) Locale for parsing date parts
  * @return boolean                True when all date parts are correct
  */
 public static function isDate($date, $format = null, $locale = null)
 {
     if (!is_string($date) && !is_numeric($date) && !$date instanceof Date && !is_array($date)) {
         return false;
     }
     if ($format !== null && $format != 'ee' && $format != 'ss' && $format != 'GG' && $format != 'MM' && $format != 'EE' && $format != 'TT' && Locale::isLocale($format)) {
         $locale = $format;
         $format = null;
     }
     $locale = Locale::findLocale($locale);
     if ($format === null) {
         $format = Format::getDateFormat($locale);
     } else {
         if (self::$_options['format_type'] == 'php' && !defined($format)) {
             $format = Format::convertPhpToIsoFormat($format);
         }
     }
     $format = self::_getLocalizedToken($format, $locale);
     if (!is_array($date)) {
         try {
             $parsed = Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false));
         } catch (\Zend\Locale\Exception $e) {
             // Date can not be parsed
             return false;
         }
     } else {
         $parsed = $date;
     }
     if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) {
         // Year expected but not found
         return false;
     }
     if (strpos($format, 'M') !== false and !isset($parsed['month'])) {
         // Month expected but not found
         return false;
     }
     if (strpos($format, 'd') !== false and !isset($parsed['day'])) {
         // Day expected but not found
         return false;
     }
     if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) {
         // Hour expected but not found
         return false;
     }
     if (strpos($format, 'm') !== false and !isset($parsed['minute'])) {
         // Minute expected but not found
         return false;
     }
     if (strpos($format, 's') !== false and !isset($parsed['second'])) {
         // Second expected  but not found
         return false;
     }
     // Set not given dateparts
     if (isset($parsed['hour']) === false) {
         $parsed['hour'] = 12;
     }
     if (isset($parsed['minute']) === false) {
         $parsed['minute'] = 0;
     }
     if (isset($parsed['second']) === false) {
         $parsed['second'] = 0;
     }
     if (isset($parsed['month']) === false) {
         $parsed['month'] = 1;
     }
     if (isset($parsed['day']) === false) {
         $parsed['day'] = 1;
     }
     if (isset($parsed['year']) === false) {
         $parsed['year'] = 1970;
     }
     if (self::isYearLeapYear($parsed['year'])) {
         $parsed['year'] = 1972;
     } else {
         $parsed['year'] = 1971;
     }
     $date = new self($parsed, null, $locale);
     $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']);
     if ($parsed['year'] != $date->date('Y', $timestamp)) {
         // Given year differs from parsed year
         return false;
     }
     if ($parsed['month'] != $date->date('n', $timestamp)) {
         // Given month differs from parsed month
         return false;
     }
     if ($parsed['day'] != $date->date('j', $timestamp)) {
         // Given day differs from parsed day
         return false;
     }
     if ($parsed['hour'] != $date->date('G', $timestamp)) {
         // Given hour differs from parsed hour
         return false;
     }
     if ($parsed['minute'] != $date->date('i', $timestamp)) {
         // Given minute differs from parsed minute
         return false;
     }
     if ($parsed['second'] != $date->date('s', $timestamp)) {
         // Given second differs from parsed second
         return false;
     }
     return true;
 }
Пример #5
0
    /**
     * Set a new value
     *
     * @param  integer|string             $value   Value as string, integer, real or float
     * @param  string                     $type    OPTIONAL A measure type f.e. Zend_Measure_Length::METER
     * @param  string|Zend\Locale\Locale  $locale  OPTIONAL Locale for parsing numbers
     * @throws Zend\Measure\Exception
     * @return Zend\Measure\AbstractMeasure
     */
    public function setValue($value, $type = null, $locale = null)
    {
        if (($type !== null) and (Locale\Locale::isLocale($type))) {
            $locale = $type;
            $type = null;
        }

        if ($locale === null) {
            $locale = $this->_locale;
        }

        $this->setLocale($locale, true);
        if ($type === null) {
            $type = $this->_units['STANDARD'];
        }

        if (empty($this->_units[$type])) {
            throw new Exception("Type ($type) is unknown");
        }

        try {
            $value = Locale\Format::getNumber($value, array('locale' => $locale));
        } catch(\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode(), $e);
        }

        $this->_value = $value;
        $this->setType($type);
        return $this;
    }
Пример #6
0
Файл: Tmx.php Проект: rexmac/zf2
 /**
  * Internal method, called by xml element handler at start
  *
  * @param resource $file   File handler
  * @param string   $name   Elements name
  * @param array    $attrib Attributes for this element
  */
 protected function _startElement($file, $name, $attrib)
 {
     if ($this->_seg !== null) {
         $this->_content .= "<" . $name;
         foreach ($attrib as $key => $value) {
             $this->_content .= " {$key}=\"{$value}\"";
         }
         $this->_content .= ">";
     } else {
         switch (strtolower($name)) {
             case 'header':
                 if (empty($this->_useId) && isset($attrib['srclang'])) {
                     if (Locale\Locale::isLocale($attrib['srclang'])) {
                         $this->_srclang = Locale\Locale::findLocale($attrib['srclang']);
                     } else {
                         if (!$this->_options['disableNotices']) {
                             if ($this->_options['log']) {
                                 $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
                             } else {
                                 trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
                             }
                         }
                         $this->_srclang = $attrib['srclang'];
                     }
                 }
                 break;
             case 'tu':
                 if (isset($attrib['tuid'])) {
                     $this->_tu = $attrib['tuid'];
                 }
                 break;
             case 'tuv':
                 if (isset($attrib['xml:lang'])) {
                     if (Locale\Locale::isLocale($attrib['xml:lang'])) {
                         $this->_tuv = Locale\Locale::findLocale($attrib['xml:lang']);
                     } else {
                         if (!$this->_options['disableNotices']) {
                             if ($this->_options['log']) {
                                 $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist.");
                             } else {
                                 trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE);
                             }
                         }
                         $this->_tuv = $attrib['xml:lang'];
                     }
                     if (!isset($this->_data[$this->_tuv])) {
                         $this->_data[$this->_tuv] = array();
                     }
                 }
                 break;
             case 'seg':
                 $this->_seg = true;
                 $this->_content = null;
                 break;
             default:
                 break;
         }
     }
 }
Пример #7
0
 /**
  * Finds the proper locale based on the input
  * Checks if it exists, degrades it when necessary
  * Detects registry locale and when all fails tries to detect a automatic locale
  * Returns the found locale as string
  *
  * @param string $locale
  * @throws \Zend\Locale\Exception\InvalidArgumentException When the given locale is no locale or the autodetection fails
  * @return string
  */
 public static function findLocale($locale = null)
 {
     if ($locale === null) {
         if (Registry::isRegistered('Zend_Locale')) {
             $locale = Registry::get('Zend_Locale');
         }
     }
     if ($locale === null) {
         $locale = new Locale();
     }
     if (!Locale::isLocale($locale, true)) {
         if (!Locale::isLocale($locale, false)) {
             $locale = Locale::getLocaleToTerritory($locale);
             if (empty($locale)) {
                 throw new Exception\InvalidArgumentException("The locale '{$locale}' is no known locale");
             }
         } else {
             $locale = new self($locale);
         }
     }
     $locale = self::_prepareLocale($locale);
     return $locale;
 }
Пример #8
0
 /**
  * Internal method for checking the options array
  *
  * @param  array $options Options to check
  * @throws Zend\Currency\Exception On unknown position
  * @throws Zend\Currency\Exception On unknown locale
  * @throws Zend\Currency\Exception On unknown display
  * @throws Zend\Currency\Exception On precision not between -1 and 30
  * @throws Zend\Currency\Exception On problem with script conversion
  * @throws Zend\Currency\Exception On unknown options
  * @return array
  */
 protected function _checkOptions(array $options = array())
 {
     if (count($options) === 0) {
         return $this->_options;
     }
     foreach ($options as $name => $value) {
         $name = strtolower($name);
         if ($name !== 'format') {
             if (gettype($value) === 'string') {
                 $value = strtolower($value);
             }
         }
         switch ($name) {
             case 'position':
                 if ($value !== self::STANDARD and $value !== self::RIGHT and $value !== self::LEFT) {
                     throw new Exception\InvalidArgumentException("Unknown position '" . $value . "'");
                 }
                 break;
             case 'format':
                 if (empty($value) === false and Locale\Locale::isLocale($value) === false) {
                     if (!is_string($value) || strpos($value, '0') === false) {
                         throw new Exception\InvalidArgumentException('\'' . (gettype($value) === 'object' ? get_class($value) : $value) . '\' is no format token');
                     }
                 }
                 break;
             case 'display':
                 if (is_numeric($value) and $value !== self::NO_SYMBOL and $value !== self::USE_SYMBOL and $value !== self::USE_SHORTNAME and $value !== self::USE_NAME) {
                     throw new Exception\InvalidArgumentException("Unknown display '{$value}'");
                 }
                 break;
             case 'precision':
                 if ($value === null) {
                     $value = -1;
                 }
                 if ($value < -1 or $value > 30) {
                     throw new Exception\InvalidArgumentException("'{$value}' precision has to be between -1 and 30.");
                 }
                 break;
             case 'script':
                 try {
                     Locale\Format::convertNumerals(0, $options['script']);
                 } catch (Locale\Exception $e) {
                     throw new Exception\InvalidArgumentException($e->getMessage());
                 }
                 break;
             default:
                 break;
         }
     }
     return $options;
 }
Пример #9
0
 /**
  * Checks if a string is translated within the source or not
  * returns boolean
  *
  * @param  string             $messageId Translation string
  * @param  boolean            $original  (optional) Allow translation only for original language
  *                                       when true, a translation for 'en_US' would give false when it can
  *                                       be translated with 'en' only
  * @param  string|\Zend\Locale\Locale $locale    (optional) Locale/Language to use, identical with locale identifier,
  *                                       see Zend_Locale for more information
  * @return boolean
  */
 public function isTranslated($messageId, $original = false, $locale = null)
 {
     if ($original !== false and $original !== true) {
         $locale = $original;
         $original = false;
     }
     if ($locale === null) {
         $locale = $this->_options['locale'];
     }
     if (!Locale\Locale::isLocale($locale, true, false)) {
         if (!Locale\Locale::isLocale($locale, false, false)) {
             // language does not exist, return original string
             return false;
         }
         $locale = new Locale\Locale($locale);
     }
     $locale = (string) $locale;
     if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
         // return original translation
         return true;
     } else {
         if (strlen($locale) != 2 and $original === false) {
             // faster than creating a new locale and separate the leading part
             $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
             if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
                 // return regionless translation (en_US -> en)
                 return true;
             }
         }
     }
     // No translation found, return original
     return false;
 }
Пример #10
0
 /**
  * Translate a message
  * You can give multiple params or an array of params.
  * If you want to output another locale just set it as last single parameter
  * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
  *
  * @param  string $messageid Id of the message to be translated
  * @return string|\Zend\View\Helper\Translator Translated message
  */
 public function __invoke($messageid = null)
 {
     if ($messageid === null) {
         return $this;
     }
     $translator = $this->getTranslator();
     $options = func_get_args();
     array_shift($options);
     $count = count($options);
     $locale = null;
     if ($count > 0) {
         if (Locale::isLocale($options[$count - 1]) !== false) {
             // Don't treat last option as the locale if doing so will result in an error
             if (is_array($options[0]) || @vsprintf($messageid, array_slice($options, 0, -1)) !== false) {
                 $locale = array_pop($options);
             }
         }
     }
     if (count($options) === 1 and is_array($options[0]) === true) {
         $options = $options[0];
     }
     if ($translator !== null) {
         $messageid = $translator->translate($messageid, $locale);
     }
     if (count($options) === 0) {
         return $messageid;
     }
     return vsprintf($messageid, $options);
 }
Пример #11
0
 /**
  * Set the locales which are accepted
  *
  * @param  string|array|\Zend\Locale\Locale $locale
  * @throws \Zend\Filter\Exception
  * @return \Zend\Filter\Boolean
  */
 public function setLocale($locale = null)
 {
     if (is_string($locale)) {
         $locale = array($locale);
     } elseif ($locale instanceof Locale) {
         $locale = array($locale->toString());
     } elseif (!is_array($locale)) {
         throw new Exception\InvalidArgumentException('Locale has to be string, array or an instance of Zend_Locale');
     }
     foreach ($locale as $single) {
         if (!Locale::isLocale($single)) {
             throw new Exception\InvalidArgumentException("Unknown locale '{$single}'");
         }
     }
     $this->_locale = $locale;
     return $this;
 }
Пример #12
0
 /**
  * Finds the proper locale based on the input
  * Checks if it exists, degrades it when necessary
  * Detects registry locale and when all fails tries to detect a automatic locale
  * Returns the found locale as string
  *
  * @param string $locale
  * @throws \Zend\Locale\Exception When the given locale is no locale or the autodetection fails
  * @return string
  */
 public static function findLocale($locale = null)
 {
     if ($locale === null) {
         if (Registry::isRegistered('Zend_Locale')) {
             $locale = Registry::get('Zend_Locale');
         }
     }
     if ($locale === null) {
         $locale = new Locale();
     }
     if (!Locale::isLocale($locale, true, false)) {
         if (!Locale::isLocale($locale, false, false)) {
             throw new Exception("The locale '{$locale}' is no known locale");
         }
         $locale = new Locale($locale);
     }
     $locale = self::_prepareLocale($locale);
     return $locale;
 }
Пример #13
0
 /**
  * Internal function for checking the locale
  *
  * @param string|\Zend\Locale $locale Locale to check
  * @return string
  */
 private static function _checkLocale($locale)
 {
     if (empty($locale)) {
         $locale = new Locale();
     }
     if (!Locale::isLocale((string) $locale)) {
         throw new Exception\InvalidArgumentException("Locale (" . (string) $locale . ") is a unknown locale");
     }
     return (string) $locale;
 }
Пример #14
0
 /**
  * Zend\Measure\AbstractMeasure is an abstract class for the different measurement types
  *
  * @param  integer                   $value  Value
  * @param  string                    $type   (Optional) A Zend\Measure\Number Type
  * @param  string|Zend\Locale\Locale $locale (Optional) A Zend\Locale\Locale
  * @throws Zend\Measure\Exception When language is unknown
  * @throws Zend\Measure\Exception When type is unknown
  */
 public function __construct($value, $type, $locale = null)
 {
     if ($type !== null and Locale\Locale::isLocale($type, null, false)) {
         $locale = $type;
         $type = null;
     }
     if ($locale === null) {
         $locale = new Locale\Locale();
     }
     if (!Locale\Locale::isLocale($locale, true, false)) {
         if (!Locale\Locale::isLocale($locale, true, false)) {
             throw new Exception("Language (" . (string) $locale . ") is unknown");
         }
         $locale = new Locale\Locale($locale);
     }
     $this->_locale = (string) $locale;
     if ($type === null) {
         $type = $this->_units['STANDARD'];
     }
     if (isset($this->_units[$type]) === false) {
         throw new Exception("Type ({$type}) is unknown");
     }
     $this->setValue($value, $type, $this->_locale);
 }
Пример #15
0
    /**
     * Internal function for checking the locale
     *
     * @param string|\Zend\Locale $locale Locale to check
     * @return string
     */
    protected static function _checkLocale($locale)
    {
        if (empty($locale)) {
            $locale = new Locale();
        }

        if (!(Locale::isLocale((string) $locale))) {
            throw new InvalidArgumentException(
              "Locale (" . (string) $locale . ") is no known locale"
            );
        }

        return (string) $locale;
    }