locale.php
来自「Bug tracker, and reporter.」· PHP 代码 · 共 838 行 · 第 1/2 页
PHP
838 行
public function getLanguage() { $locale = explode('_', $this->_Locale); return $locale[0]; } /** * Returns the region part of the locale if available * * @return string|false - Regionstring */ public function getRegion() { $locale = explode('_', $this->_Locale); if (isset($locale[1])) { return $locale[1]; } return false; } /** * Return the accepted charset of the client * * @return string */ public function getHttpCharset() { $httpcharsets = getenv("HTTP_ACCEPT_CHARSET"); $charsets = array(); if ($httpcharsets === false) { return $charsets; } $accepted = preg_split('/,\s*/', $httpcharsets); foreach ($accepted as $accept) { if (empty($accept)) { continue; } if (strpos($accept, ';')) { $quality = (float) substr($accept, strpos($accept, '=') + 1); $charsets[substr($accept, 0, strpos($accept, ';'))] = $quality; } else { $quality = 1.0; $charsets[$accept] = $quality; } } return $charsets; } /** * Returns true if both locales are equal * * @return boolean */ public function equals($object) { if ($object->toString() == $this->toString()) { return true; } return false; } /** * Returns localized informations as array, supported are several * types of informations. * For detailed information about the types look into the documentation * * @param string $path OPTIONAL Type of information to return * @param string|locale $locale OPTIONAL Locale|Language for which this informations should be returned * @param string $value OPTIONAL Value for detail list * @return array Array with the wished information in the given language */ public function getTranslationList($path = null, $locale = null, $value = null) { // load class within method for speed require_once 'Zend/Locale/Data.php'; require_once 'Zend/Locale/Exception.php'; if ($locale === null) { $locale = $this->_Locale; } if ($locale == 'auto') { $locale = self::$_auto; } if ($locale == 'browser') { $locale = self::$_browser; } if ($locale == 'environment') { $locale = self::$_environment; } if (is_array($locale)) { $locale = key($locale); } $result = Zend_Locale_Data::getList($locale, $path, $value); if (empty($result)) { return false; } return $result; } /** * Returns an array with the name of all languages translated to the given language * * @param string $locale OPTIONAL locale for language translation * @return array */ public function getLanguageTranslationList($locale = null) { return $this->getTranslationList('language', $locale); } /** * Returns an array with the name of all scripts translated to the given language * * @param string $locale OPTIONAL locale for script translation * @return array */ public function getScriptTranslationList($locale = null) { return $this->getTranslationList('script', $locale); } /** * Returns an array with the name of all countries translated to the given language * * @param string $locale OPTIONAL locale for country translation * @return array */ public function getCountryTranslationList($locale = null) { return $this->getTranslationList('territory', $locale, 2); } /** * Returns an array with the name of all territories translated to the given language * All territories contains other countries. * * @param string $locale OPTIONAL locale for territory translation * @return array */ public function getTerritoryTranslationList($locale = null) { return $this->getTranslationList('territory', $locale, 1); } /** * Returns a localized information string, supported are several types of informations. * For detailed information about the types look into the documentation * * @param string $value Name to get detailed information about * @param string $path OPTIONAL Type of information to return * @param string|locale $locale OPTIONAL Locale|Language for which this informations should be returned * @return string The wished information in the given language */ public function getTranslation($value = null, $path = null, $locale = null) { // load class within method for speed require_once 'Zend/Locale/Data.php'; require_once 'Zend/Locale/Exception.php'; if ($locale === null) { $locale = $this->_Locale; } if ($locale == 'auto') { $locale = self::$_auto; } if ($locale == 'browser') { $locale = self::$_browser; } if ($locale == 'environment') { $locale = self::$_environment; } if (is_array($locale)) { $locale = key($locale); } $result = Zend_Locale_Data::getContent($locale, $path, $value); if (empty($result)) { return false; } return $result; } /** * Returns the localized language name * * @param string $value Name to get detailed information about * @param string $locale OPTIONAL locale for language translation * @return array */ public function getLanguageTranslation($value, $locale = null) { return $this->getTranslation($value, 'language', $locale); } /** * Returns the localized script name * * @param string $what Name to get detailed information about * @param string $locale OPTIONAL locale for script translation * @return array */ public function getScriptTranslation($value, $locale = null) { return $this->getTranslation($value, 'script', $locale); } /** * Returns the localized country name * * @param string $what Name to get detailed information about * @param string $locale OPTIONAL locale for country translation * @return array */ public function getCountryTranslation($value, $locale = null) { return $this->getTranslation($value, 'country', $locale); } /** * Returns the localized territory name * All territories contains other countries. * * @param string $what Name to get detailed information about * @param string $locale OPTIONAL locale for territory translation * @return array */ public function getTerritoryTranslation($value, $locale = null) { return $this->getTranslation($value, 'territory', $locale); } /** * Returns an array with translated yes strings * * @param string $locale OPTIONAL locale for language translation (defaults to $this locale) * @return array */ public function getQuestion($locale = null) { // load class within method for speed require_once 'Zend/Locale/Data.php'; if ($locale === null) { $locale = $this->_Locale; } if ($locale == 'auto') { $locale = self::$_auto; } if ($locale == 'browser') { $locale = self::$_browser; } if ($locale == 'environment') { $locale = self::$_environment; } if (is_array($locale)) { $locale = key($locale); } $quest = Zend_Locale_Data::getList($locale, 'question'); $yes = explode(':', $quest['yes']); $no = explode(':', $quest['no']); $quest['yes'] = $yes[0]; $quest['yesarray'] = $yes; $quest['no'] = $no[0]; $quest['noarray'] = $no; $quest['yesexpr'] = $this->_getRegex($yes); $quest['noexpr'] = $this->_getRegex($no); return $quest; } /** * Internal function for creating a regex * * @param string $input * @return string */ private function _getRegex($input) { $regex = ""; if (is_array($input)) { $regex = "^"; $start = true; foreach($input as $row) { if ($start === false) { $regex .= "|"; } $start = false; $regex .= "("; $one = null; if (strlen($row) > 2) { $one = true; } foreach (str_split($row, 1) as $char) { $regex .= "[" . $char; $regex .= strtoupper($char) . "]"; if ($one === true) { $one = false; $regex .= "("; } } if ($one === false) { $regex .= ")"; } $regex .= "?)"; } } return $regex; } /** * Checks if a locale identifier is a real locale or not * Examples: * "en_XX" refers to "en", which returns true * "XX_yy" refers to "root", which returns false * * @param string|Zend_Locale $locale Locale to check for * @param boolean $create If true, create a default locale, if $locale is empty * @return false|string false if given locale is not a locale, else the locale identifier is returned */ public static function isLocale($locale, $create = false) { if (empty($locale) and ($create === true)) { $locale = new self(); } if ($locale instanceof Zend_Locale) { return $locale->toString(); } if (!is_string($locale)) { return false; } if (empty(self::$_auto)) { $temp = new self($locale); self::$_auto = $temp->getDefault(null, false); self::$_browser = $temp->getDefault(self::BROWSER, false); self::$_environment = $temp->getDefault(self::ENVIRONMENT, false); } if ($locale == 'auto') { $locale = self::$_auto; } if ($locale == 'browser') { $locale = self::$_browser; } if ($locale == 'environment') { $locale = self::$_environment; } if (is_array($locale)) { $locale = key($locale); } if (array_key_exists($locale, self::$_localeData)) { return $locale; } else { $locale = explode('_', $locale); if (array_key_exists($locale[0], self::$_localeData)) { return $locale[0]; } } return false; } /** * Returns a list of all known locales where the locale is the key * Only real locales are returned, the internal locales 'root', 'auto', 'browser' * and 'environment' are suppressed * * @return array */ public static function getLocaleList() { $list = self::$_localeData; unset($list['root']); unset($list['auto']); unset($list['browser']); unset($list['environment']); return $list; } /** * Sets a cache * * @param Zend_Cache_Core $cache */ public static function setCache(Zend_Cache_Core $cache) { // load class within method for speed require_once 'Zend/Locale/Data.php'; Zend_Locale_Data::setCache($cache); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?