/** * 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|IfwPsn_Vendor_Zend_View_Helper_Translate Translated message */ public function translate($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 (IfwPsn_Vendor_Zend_Locale::isLocale($options[$count - 1], null, false) !== 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); }
/** * Output a formatted currency * * @param integer|float $value Currency value to output * @param string|IfwPsn_Vendor_Zend_Locale|array $currency OPTIONAL Currency to use for * this call * @return string Formatted currency */ public function currency($value = null, $currency = null) { if ($value === null) { return $this; } if (is_string($currency) || $currency instanceof IfwPsn_Vendor_Zend_Locale) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale.php'; if (IfwPsn_Vendor_Zend_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); }
/** * Set the locales which are accepted * * @param string|array|IfwPsn_Vendor_Zend_Locale $locale * @throws IfwPsn_Vendor_Zend_Filter_Exception * @return IfwPsn_Vendor_Zend_Filter_Boolean */ public function setLocale($locale = null) { if (is_string($locale)) { $locale = array($locale); } elseif ($locale instanceof IfwPsn_Vendor_Zend_Locale) { $locale = array($locale->toString()); } elseif (!is_array($locale)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php'; throw new IfwPsn_Vendor_Zend_Filter_Exception('Locale has to be string, array or an instance of IfwPsn_Vendor_Zend_Locale'); } require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale.php'; foreach ($locale as $single) { if (!IfwPsn_Vendor_Zend_Locale::isLocale($single)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php'; throw new IfwPsn_Vendor_Zend_Filter_Exception("Unknown locale '{$single}'"); } } $this->_locale = $locale; return $this; }
/** * 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|IfwPsn_Vendor_Zend_Date $date Date to parse for correctness * @param string $format (Optional) Format for parsing the date string * @param string|IfwPsn_Vendor_Zend_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 IfwPsn_Vendor_Zend_Date && !is_array($date)) { return false; } if ($format !== null && $format != 'ee' && $format != 'ss' && $format != 'GG' && $format != 'MM' && $format != 'EE' && $format != 'TT' && IfwPsn_Vendor_Zend_Locale::isLocale($format, null, false)) { $locale = $format; $format = null; } $locale = IfwPsn_Vendor_Zend_Locale::findLocale($locale); if ($format === null) { $format = IfwPsn_Vendor_Zend_Locale_Format::getDateFormat($locale); } else { if (self::$_options['format_type'] == 'php' && !defined($format)) { $format = IfwPsn_Vendor_Zend_Locale_Format::convertPhpToIsoFormat($format); } } $format = self::_getLocalizedToken($format, $locale); if (!is_array($date)) { try { $parsed = IfwPsn_Vendor_Zend_Locale_Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false)); } catch (IfwPsn_Vendor_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; }
/** * 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|IfwPsn_Vendor_Zend_Locale $locale (optional) Locale/Language to use, identical with locale identifier, * see IfwPsn_Vendor_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 (!IfwPsn_Vendor_Zend_Locale::isLocale($locale, true, false)) { if (!IfwPsn_Vendor_Zend_Locale::isLocale($locale, false, false)) { // language does not exist, return original string return false; } $locale = new IfwPsn_Vendor_Zend_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; }
/** * Internal function for checking the locale * * @param string|IfwPsn_Vendor_Zend_Locale $locale Locale to check * @return string */ private static function _checkLocale($locale) { if (empty($locale)) { $locale = new IfwPsn_Vendor_Zend_Locale(); } if (!IfwPsn_Vendor_Zend_Locale::isLocale((string) $locale, null, false)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("Locale (" . (string) $locale . ") is a unknown locale"); } return (string) $locale; }
/** * 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 IfwPsn_Vendor_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) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Registry.php'; if (IfwPsn_Vendor_Zend_Registry::isRegistered('IfwPsn_Vendor_Zend_Locale')) { $locale = IfwPsn_Vendor_Zend_Registry::get('IfwPsn_Vendor_Zend_Locale'); } } if ($locale === null) { $locale = new IfwPsn_Vendor_Zend_Locale(); } if (!IfwPsn_Vendor_Zend_Locale::isLocale($locale, true, false)) { if (!IfwPsn_Vendor_Zend_Locale::isLocale($locale, false, false)) { $locale = IfwPsn_Vendor_Zend_Locale::getLocaleToTerritory($locale); if (empty($locale)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("The locale '{$locale}' is no known locale"); } } else { $locale = new IfwPsn_Vendor_Zend_Locale($locale); } } $locale = self::_prepareLocale($locale); return $locale; }
/** * 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 (IfwPsn_Vendor_Zend_Locale::isLocale($attrib['srclang'])) { $this->_srclang = IfwPsn_Vendor_Zend_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 (IfwPsn_Vendor_Zend_Locale::isLocale($attrib['xml:lang'])) { $this->_tuv = IfwPsn_Vendor_Zend_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; } } }