Initial commit

This commit is contained in:
mareksebera 2014-09-10 20:20:58 +02:00
commit 3b93da31de
1004 changed files with 265840 additions and 0 deletions

692
lib/Cake/I18n/I18n.php Normal file
View file

@ -0,0 +1,692 @@
<?php
/**
* Internationalization
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.I18n
* @since CakePHP(tm) v 1.2.0.4116
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakePlugin', 'Core');
App::uses('L10n', 'I18n');
App::uses('Multibyte', 'I18n');
App::uses('CakeSession', 'Model/Datasource');
/**
* I18n handles translation of Text and time format strings.
*
* @package Cake.I18n
*/
class I18n {
/**
* Instance of the L10n class for localization
*
* @var L10n
*/
public $l10n = null;
/**
* Default domain of translation
*
* @var string
*/
public static $defaultDomain = 'default';
/**
* Current domain of translation
*
* @var string
*/
public $domain = null;
/**
* Current category of translation
*
* @var string
*/
public $category = 'LC_MESSAGES';
/**
* Current language used for translations
*
* @var string
*/
protected $_lang = null;
/**
* Translation strings for a specific domain read from the .mo or .po files
*
* @var array
*/
protected $_domains = array();
/**
* Set to true when I18N::_bindTextDomain() is called for the first time.
* If a translation file is found it is set to false again
*
* @var bool
*/
protected $_noLocale = false;
/**
* Translation categories
*
* @var array
*/
protected $_categories = array(
'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
);
/**
* Constants for the translation categories.
*
* The constants may be used in translation fetching
* instead of hardcoded integers.
* Example:
* {{{
* I18n::translate('CakePHP is awesome.', null, null, I18n::LC_MESSAGES)
* }}}
*
* To keep the code more readable, I18n constants are preferred over
* hardcoded integers.
*/
/**
* Constant for LC_ALL.
*
* @var int
*/
const LC_ALL = 0;
/**
* Constant for LC_COLLATE.
*
* @var int
*/
const LC_COLLATE = 1;
/**
* Constant for LC_CTYPE.
*
* @var int
*/
const LC_CTYPE = 2;
/**
* Constant for LC_MONETARY.
*
* @var int
*/
const LC_MONETARY = 3;
/**
* Constant for LC_NUMERIC.
*
* @var int
*/
const LC_NUMERIC = 4;
/**
* Constant for LC_TIME.
*
* @var int
*/
const LC_TIME = 5;
/**
* Constant for LC_MESSAGES.
*
* @var int
*/
const LC_MESSAGES = 6;
/**
* Escape string
*
* @var string
*/
protected $_escape = null;
/**
* Constructor, use I18n::getInstance() to get the i18n translation object.
*/
public function __construct() {
$this->l10n = new L10n();
}
/**
* Return a static instance of the I18n class
*
* @return I18n
*/
public static function getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] = new I18n();
}
return $instance[0];
}
/**
* Used by the translation functions in basics.php
* Returns a translated string based on current language and translation files stored in locale folder
*
* @param string $singular String to translate
* @param string $plural Plural string (if any)
* @param string $domain Domain The domain of the translation. Domains are often used by plugin translations.
* If null, the default domain will be used.
* @param string $category Category The integer value of the category to use.
* @param int $count Count Count is used with $plural to choose the correct plural form.
* @param string $language Language to translate string to.
* If null it checks for language in session followed by Config.language configuration variable.
* @return string translated string.
* @throws CakeException When '' is provided as a domain.
*/
public static function translate($singular, $plural = null, $domain = null, $category = self::LC_MESSAGES, $count = null, $language = null) {
$_this = I18n::getInstance();
if (strpos($singular, "\r\n") !== false) {
$singular = str_replace("\r\n", "\n", $singular);
}
if ($plural !== null && strpos($plural, "\r\n") !== false) {
$plural = str_replace("\r\n", "\n", $plural);
}
if (is_numeric($category)) {
$_this->category = $_this->_categories[$category];
}
if (empty($language)) {
if (CakeSession::started()) {
$language = CakeSession::read('Config.language');
}
if (empty($language)) {
$language = Configure::read('Config.language');
}
}
if (($_this->_lang && $_this->_lang !== $language) || !$_this->_lang) {
$lang = $_this->l10n->get($language);
$_this->_lang = $lang;
}
if ($domain === null) {
$domain = self::$defaultDomain;
}
if ($domain === '') {
throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.'));
}
$_this->domain = $domain . '_' . $_this->l10n->lang;
if (!isset($_this->_domains[$domain][$_this->_lang])) {
$_this->_domains[$domain][$_this->_lang] = Cache::read($_this->domain, '_cake_core_');
}
if (!isset($_this->_domains[$domain][$_this->_lang][$_this->category])) {
$_this->_bindTextDomain($domain);
Cache::write($_this->domain, $_this->_domains[$domain][$_this->_lang], '_cake_core_');
}
if ($_this->category === 'LC_TIME') {
return $_this->_translateTime($singular, $domain);
}
if (!isset($count)) {
$plurals = 0;
} elseif (!empty($_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"]) && $_this->_noLocale === false) {
$header = $_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"];
$plurals = $_this->_pluralGuess($header, $count);
} else {
if ($count != 1) {
$plurals = 1;
} else {
$plurals = 0;
}
}
if (!empty($_this->_domains[$domain][$_this->_lang][$_this->category][$singular])) {
if (($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$singular]) || ($plurals) && ($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$plural])) {
if (is_array($trans)) {
if (isset($trans[$plurals])) {
$trans = $trans[$plurals];
} else {
trigger_error(
__d('cake_dev',
'Missing plural form translation for "%s" in "%s" domain, "%s" locale. ' .
' Check your po file for correct plurals and valid Plural-Forms header.',
$singular,
$domain,
$_this->_lang
),
E_USER_WARNING
);
$trans = $trans[0];
}
}
if (strlen($trans)) {
return $trans;
}
}
}
if (!empty($plurals)) {
return $plural;
}
return $singular;
}
/**
* Clears the domains internal data array. Useful for testing i18n.
*
* @return void
*/
public static function clear() {
$self = I18n::getInstance();
$self->_domains = array();
}
/**
* Get the loaded domains cache.
*
* @return array
*/
public static function domains() {
$self = I18n::getInstance();
return $self->_domains;
}
/**
* Attempts to find the plural form of a string.
*
* @param string $header Type
* @param int $n Number
* @return int plural match
*/
protected function _pluralGuess($header, $n) {
if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
return 0;
}
if ($header === "nplurals=2;plural=n!=1;") {
return $n != 1 ? 1 : 0;
} elseif ($header === "nplurals=2;plural=n>1;") {
return $n > 1 ? 1 : 0;
}
if (strpos($header, "plurals=3")) {
if (strpos($header, "100!=11")) {
if (strpos($header, "10<=4")) {
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
} elseif (strpos($header, "100<10")) {
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
}
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
} elseif (strpos($header, "n==2")) {
return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
} elseif (strpos($header, "n==0")) {
return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
} elseif (strpos($header, "n>=2")) {
return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
} elseif (strpos($header, "10>=2")) {
return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
}
return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
} elseif (strpos($header, "plurals=4")) {
if (strpos($header, "100==2")) {
return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
} elseif (strpos($header, "n>=3")) {
return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
} elseif (strpos($header, "100>=1")) {
return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
}
} elseif (strpos($header, "plurals=5")) {
return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
}
}
/**
* Binds the given domain to a file in the specified directory.
*
* @param string $domain Domain to bind
* @return string Domain binded
*/
protected function _bindTextDomain($domain) {
$this->_noLocale = true;
$core = true;
$merge = array();
$searchPaths = App::path('locales');
$plugins = CakePlugin::loaded();
if (!empty($plugins)) {
foreach ($plugins as $plugin) {
$pluginDomain = Inflector::underscore($plugin);
if ($pluginDomain === $domain) {
$searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
$searchPaths = array_reverse($searchPaths);
break;
}
}
}
foreach ($searchPaths as $directory) {
foreach ($this->l10n->languagePath as $lang) {
$localeDef = $directory . $lang . DS . $this->category;
if (is_file($localeDef)) {
$definitions = self::loadLocaleDefinition($localeDef);
if ($definitions !== false) {
$this->_domains[$domain][$this->_lang][$this->category] = $definitions;
$this->_noLocale = false;
return $domain;
}
}
if ($core) {
$app = $directory . $lang . DS . $this->category . DS . 'core';
$translations = false;
if (is_file($app . '.mo')) {
$translations = self::loadMo($app . '.mo');
}
if ($translations === false && is_file($app . '.po')) {
$translations = self::loadPo($app . '.po');
}
if ($translations !== false) {
$this->_domains[$domain][$this->_lang][$this->category] = $translations;
$merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category];
$this->_noLocale = false;
$core = null;
}
}
$file = $directory . $lang . DS . $this->category . DS . $domain;
$translations = false;
if (is_file($file . '.mo')) {
$translations = self::loadMo($file . '.mo');
}
if ($translations === false && is_file($file . '.po')) {
$translations = self::loadPo($file . '.po');
}
if ($translations !== false) {
$this->_domains[$domain][$this->_lang][$this->category] = $translations;
$this->_noLocale = false;
break 2;
}
}
}
if (empty($this->_domains[$domain][$this->_lang][$this->category])) {
$this->_domains[$domain][$this->_lang][$this->category] = array();
return $domain;
}
if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) {
$head = $this->_domains[$domain][$this->_lang][$this->category][""];
foreach (explode("\n", $head) as $line) {
$header = strtok($line, ':');
$line = trim(strtok("\n"));
$this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line;
}
if (isset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"])) {
$switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"]);
$this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch;
unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]);
}
$this->_domains = Hash::mergeDiff($this->_domains, $merge);
if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
unset($this->_domains[$domain][$this->_lang][$this->category][null]);
}
}
return $domain;
}
/**
* Loads the binary .mo file and returns array of translations
*
* @param string $filename Binary .mo file to load
* @return mixed Array of translations on success or false on failure
*/
public static function loadMo($filename) {
$translations = false;
// @codingStandardsIgnoreStart
// Binary files extracted makes non-standard local variables
if ($data = file_get_contents($filename)) {
$translations = array();
$header = substr($data, 0, 20);
$header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
extract($header);
if ((dechex($magic) === '950412de' || dechex($magic) === 'ffffffff950412de') && !$version) {
for ($n = 0; $n < $count; $n++) {
$r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
$msgid = substr($data, $r["offs"], $r["len"]);
unset($msgid_plural);
if (strpos($msgid, "\000")) {
list($msgid, $msgid_plural) = explode("\000", $msgid);
}
$r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
$msgstr = substr($data, $r["offs"], $r["len"]);
if (strpos($msgstr, "\000")) {
$msgstr = explode("\000", $msgstr);
}
$translations[$msgid] = $msgstr;
if (isset($msgid_plural)) {
$translations[$msgid_plural] =& $translations[$msgid];
}
}
}
}
// @codingStandardsIgnoreEnd
return $translations;
}
/**
* Loads the text .po file and returns array of translations
*
* @param string $filename Text .po file to load
* @return mixed Array of translations on success or false on failure
*/
public static function loadPo($filename) {
if (!$file = fopen($filename, 'r')) {
return false;
}
$type = 0;
$translations = array();
$translationKey = '';
$plural = 0;
$header = '';
do {
$line = trim(fgets($file));
if ($line === '' || $line[0] === '#') {
continue;
}
if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
$type = 1;
$translationKey = stripcslashes($regs[1]);
} elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
$type = 2;
$translationKey = '';
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
$type = 3;
$translationKey .= stripcslashes($regs[1]);
} elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
$translations[$translationKey] = stripcslashes($regs[1]);
$type = 4;
} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
$type = 4;
$translations[$translationKey] = '';
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
$translations[$translationKey] .= stripcslashes($regs[1]);
} elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
$type = 6;
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
$type = 6;
} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
$plural = $regs[1];
$translations[$translationKey][$plural] = stripcslashes($regs[2]);
$type = 7;
} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
$plural = $regs[1];
$translations[$translationKey][$plural] = '';
$type = 7;
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
$translations[$translationKey][$plural] .= stripcslashes($regs[1]);
} elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
$header .= stripcslashes($regs[1]);
$type = 5;
} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
$header = '';
$type = 5;
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
$header .= stripcslashes($regs[1]);
} else {
unset($translations[$translationKey]);
$type = 0;
$translationKey = '';
$plural = 0;
}
} while (!feof($file));
fclose($file);
$merge[''] = $header;
return array_merge($merge, $translations);
}
/**
* Parses a locale definition file following the POSIX standard
*
* @param string $filename Locale definition filename
* @return mixed Array of definitions on success or false on failure
*/
public static function loadLocaleDefinition($filename) {
if (!$file = fopen($filename, 'r')) {
return false;
}
$definitions = array();
$comment = '#';
$escape = '\\';
$currentToken = false;
$value = '';
$_this = I18n::getInstance();
while ($line = fgets($file)) {
$line = trim($line);
if (empty($line) || $line[0] === $comment) {
continue;
}
$parts = preg_split("/[[:space:]]+/", $line);
if ($parts[0] === 'comment_char') {
$comment = $parts[1];
continue;
}
if ($parts[0] === 'escape_char') {
$escape = $parts[1];
continue;
}
$count = count($parts);
if ($count === 2) {
$currentToken = $parts[0];
$value = $parts[1];
} elseif ($count === 1) {
$value = is_array($value) ? $parts[0] : $value . $parts[0];
} else {
continue;
}
$len = strlen($value) - 1;
if ($value[$len] === $escape) {
$value = substr($value, 0, $len);
continue;
}
$mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
$replacements = array_map('crc32', $mustEscape);
$value = str_replace($mustEscape, $replacements, $value);
$value = explode(';', $value);
$_this->_escape = $escape;
foreach ($value as $i => $val) {
$val = trim($val, '"');
$val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
$val = str_replace($replacements, $mustEscape, $val);
$value[$i] = $val;
}
if (count($value) === 1) {
$definitions[$currentToken] = array_pop($value);
} else {
$definitions[$currentToken] = $value;
}
}
return $definitions;
}
/**
* Auxiliary function to parse a symbol from a locale definition file
*
* @param string $string Symbol to be parsed
* @return string parsed symbol
*/
protected function _parseLiteralValue($string) {
$string = $string[1];
if (substr($string, 0, 2) === $this->_escape . 'x') {
$delimiter = $this->_escape . 'x';
return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
}
if (substr($string, 0, 2) === $this->_escape . 'd') {
$delimiter = $this->_escape . 'd';
return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
}
if ($string[0] === $this->_escape && isset($string[1]) && is_numeric($string[1])) {
$delimiter = $this->_escape;
return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
}
if (substr($string, 0, 3) === 'U00') {
$delimiter = 'U00';
return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
}
if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
return Multibyte::ascii(array(hexdec($match[1])));
}
return $string;
}
/**
* Returns a Time format definition from corresponding domain
*
* @param string $format Format to be translated
* @param string $domain Domain where format is stored
* @return mixed translated format string if only value or array of translated strings for corresponding format.
*/
protected function _translateTime($format, $domain) {
if (!empty($this->_domains[$domain][$this->_lang]['LC_TIME'][$format])) {
if (($trans = $this->_domains[$domain][$this->_lang][$this->category][$format])) {
return $trans;
}
}
return $format;
}
}

491
lib/Cake/I18n/L10n.php Normal file
View file

@ -0,0 +1,491 @@
<?php
/**
* Localization
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.I18n
* @since CakePHP(tm) v 1.2.0.4116
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeRequest', 'Network');
/**
* Localization
*
* @package Cake.I18n
*/
class L10n {
/**
* The language for current locale
*
* @var string
*/
public $language = 'English (United States)';
/**
* Locale search paths
*
* @var array
*/
public $languagePath = array('en_us', 'eng');
/**
* ISO 639-3 for current locale
*
* @var string
*/
public $lang = 'eng';
/**
* Locale
*
* @var string
*/
public $locale = 'en_us';
/**
* Default language.
*
* If config value 'Config.language' is set in an application this will be set
* as a fall back else if DEFAULT_LANGUAGE it defined it will be used.
* Constant DEFAULT_LANGUAGE has been deprecated in 2.4
*
* @var string
*/
public $default = null;
/**
* Encoding used for current locale
*
* @var string
*/
public $charset = 'utf-8';
/**
* Text direction for current locale
*
* @var string
*/
public $direction = 'ltr';
/**
* Maps ISO 639-3 to I10n::_l10nCatalog
* The terminological codes (first one per language) should be used if possible.
* They are the ones building the path in `/APP/Locale/[code]/`
* The bibliographic codes are aliases.
*
* @var array
*/
protected $_l10nMap = array(
/* Afrikaans */ 'afr' => 'af',
/* Albanian */ 'sqi' => 'sq',
/* Albanian - bibliographic */ 'alb' => 'sq',
/* Arabic */ 'ara' => 'ar',
/* Armenian/Armenia */ 'hye' => 'hy',
/* Basque */ 'eus' => 'eu',
/* Basque */ 'baq' => 'eu',
/* Tibetan */ 'bod' => 'bo',
/* Tibetan - bibliographic */ 'tib' => 'bo',
/* Bosnian */ 'bos' => 'bs',
/* Bulgarian */ 'bul' => 'bg',
/* Byelorussian */ 'bel' => 'be',
/* Catalan */ 'cat' => 'ca',
/* Chinese */ 'zho' => 'zh',
/* Chinese - bibliographic */ 'chi' => 'zh',
/* Croatian */ 'hrv' => 'hr',
/* Czech */ 'ces' => 'cs',
/* Czech - bibliographic */ 'cze' => 'cs',
/* Danish */ 'dan' => 'da',
/* Dutch (Standard) */ 'nld' => 'nl',
/* Dutch (Standard) - bibliographic */ 'dut' => 'nl',
/* English */ 'eng' => 'en',
/* Estonian */ 'est' => 'et',
/* Faeroese */ 'fao' => 'fo',
/* Farsi/Persian */ 'fas' => 'fa',
/* Farsi/Persian - bibliographic */ 'per' => 'fa',
/* Finnish */ 'fin' => 'fi',
/* French (Standard) */ 'fra' => 'fr',
/* French (Standard) - bibliographic */ 'fre' => 'fr',
/* Gaelic (Scots) */ 'gla' => 'gd',
/* Galician */ 'glg' => 'gl',
/* German (Standard) */ 'deu' => 'de',
/* German (Standard) - bibliographic */ 'ger' => 'de',
/* Greek */ 'gre' => 'el',
/* Greek */ 'ell' => 'el',
/* Hebrew */ 'heb' => 'he',
/* Hindi */ 'hin' => 'hi',
/* Hungarian */ 'hun' => 'hu',
/* Icelandic */ 'isl' => 'is',
/* Icelandic - bibliographic */ 'ice' => 'is',
/* Indonesian */ 'ind' => 'id',
/* Irish */ 'gle' => 'ga',
/* Italian */ 'ita' => 'it',
/* Japanese */ 'jpn' => 'ja',
/* Kazakh */ 'kaz' => 'kk',
/* Kalaallisut (Greenlandic) */ 'kal' => 'kl',
/* Korean */ 'kor' => 'ko',
/* Latvian */ 'lav' => 'lv',
/* Limburgish */ 'lim' => 'li',
/* Lithuanian */ 'lit' => 'lt',
/* Macedonian */ 'mkd' => 'mk',
/* Macedonian - bibliographic */ 'mac' => 'mk',
/* Malaysian */ 'msa' => 'ms',
/* Malaysian - bibliographic */ 'may' => 'ms',
/* Maltese */ 'mlt' => 'mt',
/* Norwegian */ 'nor' => 'no',
/* Norwegian Bokmal */ 'nob' => 'nb',
/* Norwegian Nynorsk */ 'nno' => 'nn',
/* Polish */ 'pol' => 'pl',
/* Portuguese (Portugal) */ 'por' => 'pt',
/* Rhaeto-Romanic */ 'roh' => 'rm',
/* Romanian */ 'ron' => 'ro',
/* Romanian - bibliographic */ 'rum' => 'ro',
/* Russian */ 'rus' => 'ru',
/* Sami */ 'sme' => 'se',
/* Serbian */ 'srp' => 'sr',
/* Slovak */ 'slk' => 'sk',
/* Slovak - bibliographic */ 'slo' => 'sk',
/* Slovenian */ 'slv' => 'sl',
/* Sorbian */ 'wen' => 'sb',
/* Spanish (Spain - Traditional) */ 'spa' => 'es',
/* Swedish */ 'swe' => 'sv',
/* Thai */ 'tha' => 'th',
/* Tsonga */ 'tso' => 'ts',
/* Tswana */ 'tsn' => 'tn',
/* Turkish */ 'tur' => 'tr',
/* Ukrainian */ 'ukr' => 'uk',
/* Urdu */ 'urd' => 'ur',
/* Venda */ 'ven' => 've',
/* Vietnamese */ 'vie' => 'vi',
/* Welsh */ 'cym' => 'cy',
/* Welsh - bibliographic */ 'wel' => 'cy',
/* Xhosa */ 'xho' => 'xh',
/* Yiddish */ 'yid' => 'yi',
/* Zulu */ 'zul' => 'zu'
);
/**
* HTTP_ACCEPT_LANGUAGE catalog
*
* holds all information related to a language
*
* @var array
*/
protected $_l10nCatalog = array(
'af' => array('language' => 'Afrikaans', 'locale' => 'afr', 'localeFallback' => 'afr', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ar' => array('language' => 'Arabic', 'locale' => 'ara', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-ae' => array('language' => 'Arabic (U.A.E.)', 'locale' => 'ar_ae', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-bh' => array('language' => 'Arabic (Bahrain)', 'locale' => 'ar_bh', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-dz' => array('language' => 'Arabic (Algeria)', 'locale' => 'ar_dz', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-eg' => array('language' => 'Arabic (Egypt)', 'locale' => 'ar_eg', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-iq' => array('language' => 'Arabic (Iraq)', 'locale' => 'ar_iq', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-jo' => array('language' => 'Arabic (Jordan)', 'locale' => 'ar_jo', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-kw' => array('language' => 'Arabic (Kuwait)', 'locale' => 'ar_kw', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-lb' => array('language' => 'Arabic (Lebanon)', 'locale' => 'ar_lb', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-ly' => array('language' => 'Arabic (Libya)', 'locale' => 'ar_ly', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-ma' => array('language' => 'Arabic (Morocco)', 'locale' => 'ar_ma', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-om' => array('language' => 'Arabic (Oman)', 'locale' => 'ar_om', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-qa' => array('language' => 'Arabic (Qatar)', 'locale' => 'ar_qa', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-sa' => array('language' => 'Arabic (Saudi Arabia)', 'locale' => 'ar_sa', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-sy' => array('language' => 'Arabic (Syria)', 'locale' => 'ar_sy', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-tn' => array('language' => 'Arabic (Tunisia)', 'locale' => 'ar_tn', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'ar-ye' => array('language' => 'Arabic (Yemen)', 'locale' => 'ar_ye', 'localeFallback' => 'ara', 'charset' => 'utf-8', 'direction' => 'rtl'),
'be' => array('language' => 'Byelorussian', 'locale' => 'bel', 'localeFallback' => 'bel', 'charset' => 'utf-8', 'direction' => 'ltr'),
'bg' => array('language' => 'Bulgarian', 'locale' => 'bul', 'localeFallback' => 'bul', 'charset' => 'utf-8', 'direction' => 'ltr'),
'bo' => array('language' => 'Tibetan', 'locale' => 'bod', 'localeFallback' => 'bod', 'charset' => 'utf-8', 'direction' => 'ltr'),
'bo-cn' => array('language' => 'Tibetan (China)', 'locale' => 'bo_cn', 'localeFallback' => 'bod', 'charset' => 'utf-8', 'direction' => 'ltr'),
'bo-in' => array('language' => 'Tibetan (India)', 'locale' => 'bo_in', 'localeFallback' => 'bod', 'charset' => 'utf-8', 'direction' => 'ltr'),
'bs' => array('language' => 'Bosnian', 'locale' => 'bos', 'localeFallback' => 'bos', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ca' => array('language' => 'Catalan', 'locale' => 'cat', 'localeFallback' => 'cat', 'charset' => 'utf-8', 'direction' => 'ltr'),
'cs' => array('language' => 'Czech', 'locale' => 'ces', 'localeFallback' => 'ces', 'charset' => 'utf-8', 'direction' => 'ltr'),
'da' => array('language' => 'Danish', 'locale' => 'dan', 'localeFallback' => 'dan', 'charset' => 'utf-8', 'direction' => 'ltr'),
'de' => array('language' => 'German (Standard)', 'locale' => 'deu', 'localeFallback' => 'deu', 'charset' => 'utf-8', 'direction' => 'ltr'),
'de-at' => array('language' => 'German (Austria)', 'locale' => 'de_at', 'localeFallback' => 'deu', 'charset' => 'utf-8', 'direction' => 'ltr'),
'de-ch' => array('language' => 'German (Swiss)', 'locale' => 'de_ch', 'localeFallback' => 'deu', 'charset' => 'utf-8', 'direction' => 'ltr'),
'de-de' => array('language' => 'German (Germany)', 'locale' => 'de_de', 'localeFallback' => 'deu', 'charset' => 'utf-8', 'direction' => 'ltr'),
'de-li' => array('language' => 'German (Liechtenstein)', 'locale' => 'de_li', 'localeFallback' => 'deu', 'charset' => 'utf-8', 'direction' => 'ltr'),
'de-lu' => array('language' => 'German (Luxembourg)', 'locale' => 'de_lu', 'localeFallback' => 'deu', 'charset' => 'utf-8', 'direction' => 'ltr'),
'el' => array('language' => 'Greek', 'locale' => 'ell', 'localeFallback' => 'ell', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en' => array('language' => 'English', 'locale' => 'eng', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-au' => array('language' => 'English (Australian)', 'locale' => 'en_au', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-bz' => array('language' => 'English (Belize)', 'locale' => 'en_bz', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-ca' => array('language' => 'English (Canadian)', 'locale' => 'en_ca', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-gb' => array('language' => 'English (British)', 'locale' => 'en_gb', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-ie' => array('language' => 'English (Ireland)', 'locale' => 'en_ie', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-jm' => array('language' => 'English (Jamaica)', 'locale' => 'en_jm', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-nz' => array('language' => 'English (New Zealand)', 'locale' => 'en_nz', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-tt' => array('language' => 'English (Trinidad)', 'locale' => 'en_tt', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-us' => array('language' => 'English (United States)', 'locale' => 'en_us', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'en-za' => array('language' => 'English (South Africa)', 'locale' => 'en_za', 'localeFallback' => 'eng', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es' => array('language' => 'Spanish (Spain - Traditional)', 'locale' => 'spa', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-ar' => array('language' => 'Spanish (Argentina)', 'locale' => 'es_ar', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-bo' => array('language' => 'Spanish (Bolivia)', 'locale' => 'es_bo', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-cl' => array('language' => 'Spanish (Chile)', 'locale' => 'es_cl', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-co' => array('language' => 'Spanish (Colombia)', 'locale' => 'es_co', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-cr' => array('language' => 'Spanish (Costa Rica)', 'locale' => 'es_cr', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-do' => array('language' => 'Spanish (Dominican Republic)', 'locale' => 'es_do', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-ec' => array('language' => 'Spanish (Ecuador)', 'locale' => 'es_ec', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-es' => array('language' => 'Spanish (Spain)', 'locale' => 'es_es', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-gt' => array('language' => 'Spanish (Guatemala)', 'locale' => 'es_gt', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-hn' => array('language' => 'Spanish (Honduras)', 'locale' => 'es_hn', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-mx' => array('language' => 'Spanish (Mexican)', 'locale' => 'es_mx', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-ni' => array('language' => 'Spanish (Nicaragua)', 'locale' => 'es_ni', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-pa' => array('language' => 'Spanish (Panama)', 'locale' => 'es_pa', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-pe' => array('language' => 'Spanish (Peru)', 'locale' => 'es_pe', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-pr' => array('language' => 'Spanish (Puerto Rico)', 'locale' => 'es_pr', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-py' => array('language' => 'Spanish (Paraguay)', 'locale' => 'es_py', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-sv' => array('language' => 'Spanish (El Salvador)', 'locale' => 'es_sv', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-uy' => array('language' => 'Spanish (Uruguay)', 'locale' => 'es_uy', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'es-ve' => array('language' => 'Spanish (Venezuela)', 'locale' => 'es_ve', 'localeFallback' => 'spa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'et' => array('language' => 'Estonian', 'locale' => 'est', 'localeFallback' => 'est', 'charset' => 'utf-8', 'direction' => 'ltr'),
'eu' => array('language' => 'Basque', 'locale' => 'eus', 'localeFallback' => 'eus', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fa' => array('language' => 'Farsi', 'locale' => 'fas', 'localeFallback' => 'fas', 'charset' => 'utf-8', 'direction' => 'rtl'),
'fi' => array('language' => 'Finnish', 'locale' => 'fin', 'localeFallback' => 'fin', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fo' => array('language' => 'Faeroese', 'locale' => 'fao', 'localeFallback' => 'fao', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fr' => array('language' => 'French (Standard)', 'locale' => 'fra', 'localeFallback' => 'fra', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fr-be' => array('language' => 'French (Belgium)', 'locale' => 'fr_be', 'localeFallback' => 'fra', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fr-ca' => array('language' => 'French (Canadian)', 'locale' => 'fr_ca', 'localeFallback' => 'fra', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fr-ch' => array('language' => 'French (Swiss)', 'locale' => 'fr_ch', 'localeFallback' => 'fra', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fr-fr' => array('language' => 'French (France)', 'locale' => 'fr_fr', 'localeFallback' => 'fra', 'charset' => 'utf-8', 'direction' => 'ltr'),
'fr-lu' => array('language' => 'French (Luxembourg)', 'locale' => 'fr_lu', 'localeFallback' => 'fra', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ga' => array('language' => 'Irish', 'locale' => 'gle', 'localeFallback' => 'gle', 'charset' => 'utf-8', 'direction' => 'ltr'),
'gd' => array('language' => 'Gaelic (Scots)', 'locale' => 'gla', 'localeFallback' => 'gla', 'charset' => 'utf-8', 'direction' => 'ltr'),
'gd-ie' => array('language' => 'Gaelic (Irish)', 'locale' => 'gd_ie', 'localeFallback' => 'gla', 'charset' => 'utf-8', 'direction' => 'ltr'),
'gl' => array('language' => 'Galician', 'locale' => 'glg', 'localeFallback' => 'glg', 'charset' => 'utf-8', 'direction' => 'ltr'),
'he' => array('language' => 'Hebrew', 'locale' => 'heb', 'localeFallback' => 'heb', 'charset' => 'utf-8', 'direction' => 'rtl'),
'hi' => array('language' => 'Hindi', 'locale' => 'hin', 'localeFallback' => 'hin', 'charset' => 'utf-8', 'direction' => 'ltr'),
'hr' => array('language' => 'Croatian', 'locale' => 'hrv', 'localeFallback' => 'hrv', 'charset' => 'utf-8', 'direction' => 'ltr'),
'hu' => array('language' => 'Hungarian', 'locale' => 'hun', 'localeFallback' => 'hun', 'charset' => 'utf-8', 'direction' => 'ltr'),
'hy' => array('language' => 'Armenian - Armenia', 'locale' => 'hye', 'localeFallback' => 'hye', 'charset' => 'utf-8', 'direction' => 'ltr'),
'id' => array('language' => 'Indonesian', 'locale' => 'ind', 'localeFallback' => 'ind', 'charset' => 'utf-8', 'direction' => 'ltr'),
'is' => array('language' => 'Icelandic', 'locale' => 'isl', 'localeFallback' => 'isl', 'charset' => 'utf-8', 'direction' => 'ltr'),
'it' => array('language' => 'Italian', 'locale' => 'ita', 'localeFallback' => 'ita', 'charset' => 'utf-8', 'direction' => 'ltr'),
'it-ch' => array('language' => 'Italian (Swiss) ', 'locale' => 'it_ch', 'localeFallback' => 'ita', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ja' => array('language' => 'Japanese', 'locale' => 'jpn', 'localeFallback' => 'jpn', 'charset' => 'utf-8', 'direction' => 'ltr'),
'kk' => array('language' => 'Kazakh', 'locale' => 'kaz', 'localeFallback' => 'kaz', 'charset' => 'utf-8', 'direction' => 'ltr'),
'kl' => array('language' => 'Kalaallisut (Greenlandic)', 'locale' => 'kal', 'localeFallback' => 'kal', 'charset' => 'kl', 'direction' => 'ltr'),
'ko' => array('language' => 'Korean', 'locale' => 'kor', 'localeFallback' => 'kor', 'charset' => 'kr', 'direction' => 'ltr'),
'ko-kp' => array('language' => 'Korea (North)', 'locale' => 'ko_kp', 'localeFallback' => 'kor', 'charset' => 'kr', 'direction' => 'ltr'),
'ko-kr' => array('language' => 'Korea (South)', 'locale' => 'ko_kr', 'localeFallback' => 'kor', 'charset' => 'kr', 'direction' => 'ltr'),
'koi8-r' => array('language' => 'Russian', 'locale' => 'koi8_r', 'localeFallback' => 'rus', 'charset' => 'koi8-r', 'direction' => 'ltr'),
'li' => array('language' => 'Limburgish', 'locale' => 'lim', 'localeFallback' => 'nld', 'charset' => 'utf-8', 'direction' => 'ltr'),
'lt' => array('language' => 'Lithuanian', 'locale' => 'lit', 'localeFallback' => 'lit', 'charset' => 'utf-8', 'direction' => 'ltr'),
'lv' => array('language' => 'Latvian', 'locale' => 'lav', 'localeFallback' => 'lav', 'charset' => 'utf-8', 'direction' => 'ltr'),
'mk' => array('language' => 'FYRO Macedonian', 'locale' => 'mkd', 'localeFallback' => 'mkd', 'charset' => 'utf-8', 'direction' => 'ltr'),
'mk-mk' => array('language' => 'Macedonian', 'locale' => 'mk_mk', 'localeFallback' => 'mkd', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ms' => array('language' => 'Malaysian', 'locale' => 'msa', 'localeFallback' => 'msa', 'charset' => 'utf-8', 'direction' => 'ltr'),
'mt' => array('language' => 'Maltese', 'locale' => 'mlt', 'localeFallback' => 'mlt', 'charset' => 'utf-8', 'direction' => 'ltr'),
'nb' => array('language' => 'Norwegian Bokmal', 'locale' => 'nob', 'localeFallback' => 'nor', 'charset' => 'utf-8', 'direction' => 'ltr'),
'nl' => array('language' => 'Dutch (Standard)', 'locale' => 'nld', 'localeFallback' => 'nld', 'charset' => 'utf-8', 'direction' => 'ltr'),
'nl-be' => array('language' => 'Dutch (Belgium)', 'locale' => 'nl_be', 'localeFallback' => 'nld', 'charset' => 'utf-8', 'direction' => 'ltr'),
'nn' => array('language' => 'Norwegian Nynorsk', 'locale' => 'nno', 'localeFallback' => 'nor', 'charset' => 'utf-8', 'direction' => 'ltr'),
'no' => array('language' => 'Norwegian', 'locale' => 'nor', 'localeFallback' => 'nor', 'charset' => 'utf-8', 'direction' => 'ltr'),
'pl' => array('language' => 'Polish', 'locale' => 'pol', 'localeFallback' => 'pol', 'charset' => 'utf-8', 'direction' => 'ltr'),
'pt' => array('language' => 'Portuguese (Portugal)', 'locale' => 'por', 'localeFallback' => 'por', 'charset' => 'utf-8', 'direction' => 'ltr'),
'pt-br' => array('language' => 'Portuguese (Brazil)', 'locale' => 'pt_br', 'localeFallback' => 'por', 'charset' => 'utf-8', 'direction' => 'ltr'),
'rm' => array('language' => 'Rhaeto-Romanic', 'locale' => 'roh', 'localeFallback' => 'roh', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ro' => array('language' => 'Romanian', 'locale' => 'ron', 'localeFallback' => 'ron', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ro-mo' => array('language' => 'Romanian (Moldavia)', 'locale' => 'ro_mo', 'localeFallback' => 'ron', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ru' => array('language' => 'Russian', 'locale' => 'rus', 'localeFallback' => 'rus', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ru-mo' => array('language' => 'Russian (Moldavia)', 'locale' => 'ru_mo', 'localeFallback' => 'rus', 'charset' => 'utf-8', 'direction' => 'ltr'),
'sb' => array('language' => 'Sorbian', 'locale' => 'wen', 'localeFallback' => 'wen', 'charset' => 'utf-8', 'direction' => 'ltr'),
'sk' => array('language' => 'Slovak', 'locale' => 'slk', 'localeFallback' => 'slk', 'charset' => 'utf-8', 'direction' => 'ltr'),
'sl' => array('language' => 'Slovenian', 'locale' => 'slv', 'localeFallback' => 'slv', 'charset' => 'utf-8', 'direction' => 'ltr'),
'sq' => array('language' => 'Albanian', 'locale' => 'sqi', 'localeFallback' => 'sqi', 'charset' => 'utf-8', 'direction' => 'ltr'),
'sr' => array('language' => 'Serbian', 'locale' => 'srp', 'localeFallback' => 'srp', 'charset' => 'utf-8', 'direction' => 'ltr'),
'sv' => array('language' => 'Swedish', 'locale' => 'swe', 'localeFallback' => 'swe', 'charset' => 'utf-8', 'direction' => 'ltr'),
'sv-fi' => array('language' => 'Swedish (Finland)', 'locale' => 'sv_fi', 'localeFallback' => 'swe', 'charset' => 'utf-8', 'direction' => 'ltr'),
'se' => array('language' => 'Sami', 'locale' => 'sme', 'localeFallback' => 'sme', 'charset' => 'utf-8', 'direction' => 'ltr'),
'th' => array('language' => 'Thai', 'locale' => 'tha', 'localeFallback' => 'tha', 'charset' => 'utf-8', 'direction' => 'ltr'),
'tn' => array('language' => 'Tswana', 'locale' => 'tsn', 'localeFallback' => 'tsn', 'charset' => 'utf-8', 'direction' => 'ltr'),
'tr' => array('language' => 'Turkish', 'locale' => 'tur', 'localeFallback' => 'tur', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ts' => array('language' => 'Tsonga', 'locale' => 'tso', 'localeFallback' => 'tso', 'charset' => 'utf-8', 'direction' => 'ltr'),
'uk' => array('language' => 'Ukrainian', 'locale' => 'ukr', 'localeFallback' => 'ukr', 'charset' => 'utf-8', 'direction' => 'ltr'),
'ur' => array('language' => 'Urdu', 'locale' => 'urd', 'localeFallback' => 'urd', 'charset' => 'utf-8', 'direction' => 'rtl'),
've' => array('language' => 'Venda', 'locale' => 'ven', 'localeFallback' => 'ven', 'charset' => 'utf-8', 'direction' => 'ltr'),
'vi' => array('language' => 'Vietnamese', 'locale' => 'vie', 'localeFallback' => 'vie', 'charset' => 'utf-8', 'direction' => 'ltr'),
'cy' => array('language' => 'Welsh', 'locale' => 'cym', 'localeFallback' => 'cym', 'charset' => 'utf-8', 'direction' => 'ltr'),
'xh' => array('language' => 'Xhosa', 'locale' => 'xho', 'localeFallback' => 'xho', 'charset' => 'utf-8', 'direction' => 'ltr'),
'yi' => array('language' => 'Yiddish', 'locale' => 'yid', 'localeFallback' => 'yid', 'charset' => 'utf-8', 'direction' => 'ltr'),
'zh' => array('language' => 'Chinese', 'locale' => 'zho', 'localeFallback' => 'zho', 'charset' => 'utf-8', 'direction' => 'ltr'),
'zh-cn' => array('language' => 'Chinese (PRC)', 'locale' => 'zh_cn', 'localeFallback' => 'zho', 'charset' => 'GB2312', 'direction' => 'ltr'),
'zh-hk' => array('language' => 'Chinese (Hong Kong)', 'locale' => 'zh_hk', 'localeFallback' => 'zho', 'charset' => 'utf-8', 'direction' => 'ltr'),
'zh-sg' => array('language' => 'Chinese (Singapore)', 'locale' => 'zh_sg', 'localeFallback' => 'zho', 'charset' => 'utf-8', 'direction' => 'ltr'),
'zh-tw' => array('language' => 'Chinese (Taiwan)', 'locale' => 'zh_tw', 'localeFallback' => 'zho', 'charset' => 'utf-8', 'direction' => 'ltr'),
'zu' => array('language' => 'Zulu', 'locale' => 'zul', 'localeFallback' => 'zul', 'charset' => 'utf-8', 'direction' => 'ltr')
);
/**
* Class constructor
*/
public function __construct() {
if (defined('DEFAULT_LANGUAGE')) {
$this->default = DEFAULT_LANGUAGE;
}
$default = Configure::read('Config.language');
if ($default) {
$this->default = $default;
}
}
/**
* Gets the settings for $language.
* If $language is null it attempt to get settings from L10n::_autoLanguage(); if this fails
* the method will get the settings from L10n::_setLanguage();
*
* @param string $language Language (if null will use DEFAULT_LANGUAGE if defined)
* @return mixed
*/
public function get($language = null) {
if ($language !== null) {
return $this->_setLanguage($language);
}
if (!$this->_autoLanguage()) {
$this->_setLanguage();
}
return $this->lang;
}
/**
* Sets the class vars to correct values for $language.
* If $language is null it will use the L10n::$default if defined
*
* @param string $language Language (if null will use L10n::$default if defined)
* @return mixed
*/
protected function _setLanguage($language = null) {
$catalog = false;
if ($language !== null) {
$catalog = $this->catalog($language);
}
if (!$catalog && $this->default) {
$language = $this->default;
$catalog = $this->catalog($language);
}
if ($catalog) {
$this->language = $catalog['language'];
$this->languagePath = array_unique(array(
$catalog['locale'],
$catalog['localeFallback']
));
$this->lang = $language;
$this->locale = $catalog['locale'];
$this->charset = $catalog['charset'];
$this->direction = $catalog['direction'];
} elseif ($language) {
$this->lang = $language;
$this->languagePath = array($language);
}
if ($this->default && $language !== $this->default) {
$catalog = $this->catalog($this->default);
$fallback = $catalog['localeFallback'];
if (!in_array($fallback, $this->languagePath)) {
$this->languagePath[] = $fallback;
}
}
if (Configure::read('Config.language') === null) {
Configure::write('Config.language', $this->lang);
}
if ($language) {
return $language;
}
}
/**
* Attempts to find the locale settings based on the HTTP_ACCEPT_LANGUAGE variable
*
* @return bool Success
*/
protected function _autoLanguage() {
$_detectableLanguages = CakeRequest::acceptLanguage();
foreach ($_detectableLanguages as $langKey) {
if (isset($this->_l10nCatalog[$langKey])) {
$this->_setLanguage($langKey);
return true;
}
if (strpos($langKey, '-') !== false) {
$langKey = substr($langKey, 0, 2);
if (isset($this->_l10nCatalog[$langKey])) {
$this->_setLanguage($langKey);
return true;
}
}
}
return false;
}
/**
* Attempts to find locale for language, or language for locale
*
* @param string|array $mixed 2/3 char string (language/locale), array of those strings, or null
* @return string|array|bool string language/locale, array of those values, whole map as an array,
* or false when language/locale doesn't exist
*/
public function map($mixed = null) {
if (is_array($mixed)) {
$result = array();
foreach ($mixed as $_mixed) {
if ($_result = $this->map($_mixed)) {
$result[$_mixed] = $_result;
}
}
return $result;
}
if (is_string($mixed)) {
if (strlen($mixed) === 2 && in_array($mixed, $this->_l10nMap)) {
return array_search($mixed, $this->_l10nMap);
}
if (isset($this->_l10nMap[$mixed])) {
return $this->_l10nMap[$mixed];
}
return false;
}
return $this->_l10nMap;
}
/**
* Attempts to find catalog record for requested language
*
* @param string|array $language string requested language, array of requested languages, or null for whole catalog
* @return array|bool array catalog record for requested language, array of catalog records, whole catalog,
* or false when language doesn't exist
*/
public function catalog($language = null) {
if (is_array($language)) {
$result = array();
foreach ($language as $_language) {
if ($_result = $this->catalog($_language)) {
$result[$_language] = $_result;
}
}
return $result;
}
if (is_string($language)) {
if (isset($this->_l10nCatalog[$language])) {
return $this->_l10nCatalog[$language];
}
if (isset($this->_l10nMap[$language]) && isset($this->_l10nCatalog[$this->_l10nMap[$language]])) {
return $this->_l10nCatalog[$this->_l10nMap[$language]];
}
return false;
}
return $this->_l10nCatalog;
}
}

879
lib/Cake/I18n/Multibyte.php Normal file
View file

@ -0,0 +1,879 @@
<?php
/**
* Multibyte handling methods.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.I18n
* @since CakePHP(tm) v 1.2.0.6833
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Multibyte handling methods.
*
* @package Cake.I18n
*/
class Multibyte {
/**
* Holds the case folding values
*
* @var array
*/
protected static $_caseFold = array();
/**
* Holds an array of Unicode code point ranges
*
* @var array
*/
protected static $_codeRange = array();
/**
* Holds the current code point range
*
* @var string
*/
protected static $_table = null;
/**
* Converts a multibyte character string
* to the decimal value of the character
*
* @param string $string String to convert.
* @return array
*/
public static function utf8($string) {
$map = array();
$values = array();
$find = 1;
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$value = ord($string[$i]);
if ($value < 128) {
$map[] = $value;
} else {
if (empty($values)) {
$find = ($value < 224) ? 2 : 3;
}
$values[] = $value;
if (count($values) === $find) {
if ($find == 3) {
$map[] = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
} else {
$map[] = (($values[0] % 32) * 64) + ($values[1] % 64);
}
$values = array();
$find = 1;
}
}
}
return $map;
}
/**
* Converts the decimal value of a multibyte character string
* to a string
*
* @param array $array Values array.
* @return string
*/
public static function ascii($array) {
$ascii = '';
foreach ($array as $utf8) {
if ($utf8 < 128) {
$ascii .= chr($utf8);
} elseif ($utf8 < 2048) {
$ascii .= chr(192 + (($utf8 - ($utf8 % 64)) / 64));
$ascii .= chr(128 + ($utf8 % 64));
} else {
$ascii .= chr(224 + (($utf8 - ($utf8 % 4096)) / 4096));
$ascii .= chr(128 + ((($utf8 % 4096) - ($utf8 % 64)) / 64));
$ascii .= chr(128 + ($utf8 % 64));
}
}
return $ascii;
}
/**
* Find position of first occurrence of a case-insensitive string.
*
* @param string $haystack The string from which to get the position of the first occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param int $offset The position in $haystack to start searching.
* @return int|bool The numeric position of the first occurrence of $needle in the $haystack string,
* or false if $needle is not found.
*/
public static function stripos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$haystack = Multibyte::strtoupper($haystack);
$needle = Multibyte::strtoupper($needle);
return Multibyte::strpos($haystack, $needle, $offset);
}
return stripos($haystack, $needle, $offset);
}
/**
* Finds first occurrence of a string within another, case insensitive.
*
* @param string $haystack The string from which to get the first occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param bool $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
* If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
* Default value is false.
* @return int|bool The portion of $haystack, or false if $needle is not found.
*/
public static function stristr($haystack, $needle, $part = false) {
$php = (PHP_VERSION < 5.3);
if (($php && $part) || Multibyte::checkMultibyte($haystack)) {
$check = Multibyte::strtoupper($haystack);
$check = Multibyte::utf8($check);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$needle = Multibyte::strtoupper($needle);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
break;
}
}
if ($i === $needleCount) {
$found = true;
}
}
if (!$found) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
if (!$php) {
return stristr($haystack, $needle, $part);
}
return stristr($haystack, $needle);
}
/**
* Get string length.
*
* @param string $string The string being checked for length.
* @return int The number of characters in string $string
*/
public static function strlen($string) {
if (Multibyte::checkMultibyte($string)) {
$string = Multibyte::utf8($string);
return count($string);
}
return strlen($string);
}
/**
* Find position of first occurrence of a string.
*
* @param string $haystack The string being checked.
* @param string $needle The position counted from the beginning of haystack.
* @param int $offset The search offset. If it is not specified, 0 is used.
* @return int|bool The numeric position of the first occurrence of $needle in the $haystack string.
* If $needle is not found, it returns false.
*/
public static function strpos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$position = $offset;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $haystack[$position + $i]) {
break;
}
}
if ($i === $needleCount) {
$found = true;
$position--;
}
}
$position++;
}
if ($found) {
return $position;
}
return false;
}
return strpos($haystack, $needle, $offset);
}
/**
* Finds the last occurrence of a character in a string within another.
*
* @param string $haystack The string from which to get the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param bool $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
* If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
* Default value is false.
* @return string|bool The portion of $haystack. or false if $needle is not found.
*/
public static function strrchr($haystack, $needle, $part = false) {
$check = Multibyte::utf8($haystack);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($check);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
if ($needle[$i] === $check[($position + $i) - 1]) {
$found = true;
}
unset($parts[$position - 1]);
$haystack = array_merge(array($haystack[$position]), $haystack);
break;
}
}
if (isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
}
}
if (!$found && isset($haystack[$position])) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
/**
* Finds the last occurrence of a character in a string within another, case insensitive.
*
* @param string $haystack The string from which to get the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param bool $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
* If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
* Default value is false.
* @return string|bool The portion of $haystack. or false if $needle is not found.
*/
public static function strrichr($haystack, $needle, $part = false) {
$check = Multibyte::strtoupper($haystack);
$check = Multibyte::utf8($check);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($check);
$needle = Multibyte::strtoupper($needle);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
if ($needle[$i] === $check[($position + $i) - 1]) {
$found = true;
}
unset($parts[$position - 1]);
$haystack = array_merge(array($haystack[$position]), $haystack);
break;
}
}
if (isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
}
}
if (!$found && isset($haystack[$position])) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
/**
* Finds position of last occurrence of a string within another, case insensitive
*
* @param string $haystack The string from which to get the position of the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param int $offset The position in $haystack to start searching.
* @return int|bool The numeric position of the last occurrence of $needle in the $haystack string,
* or false if $needle is not found.
*/
public static function strripos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$found = false;
$haystack = Multibyte::strtoupper($haystack);
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($haystack);
$needle = Multibyte::strtoupper($needle);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$position = $offset;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $haystack[$position + $i]) {
if ($needle[$i] === $haystack[($position + $i) - 1]) {
$position--;
$found = true;
continue;
}
}
}
if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
$position--;
}
}
$position++;
}
return ($found) ? $position : false;
}
return strripos($haystack, $needle, $offset);
}
/**
* Find position of last occurrence of a string in a string.
*
* @param string $haystack The string being checked, for the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param int $offset May be specified to begin searching an arbitrary number of characters into the string.
* Negative values will stop searching at an arbitrary point prior to the end of the string.
* @return int|bool The numeric position of the last occurrence of $needle in the $haystack string.
* If $needle is not found, it returns false.
*/
public static function strrpos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$position = $offset;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $haystack[$position + $i]) {
if ($needle[$i] === $haystack[($position + $i) - 1]) {
$position--;
$found = true;
continue;
}
}
}
if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
$position--;
}
}
$position++;
}
return ($found) ? $position : false;
}
return strrpos($haystack, $needle, $offset);
}
/**
* Finds first occurrence of a string within another
*
* @param string $haystack The string from which to get the first occurrence of $needle.
* @param string $needle The string to find in $haystack
* @param bool $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
* If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
* Default value is FALSE.
* @return string|bool The portion of $haystack, or true if $needle is not found.
*/
public static function strstr($haystack, $needle, $part = false) {
$php = (PHP_VERSION < 5.3);
if (($php && $part) || Multibyte::checkMultibyte($haystack)) {
$check = Multibyte::utf8($haystack);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
break;
}
}
if ($i === $needleCount) {
$found = true;
}
}
if (!$found) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
if (!$php) {
return strstr($haystack, $needle, $part);
}
return strstr($haystack, $needle);
}
/**
* Make a string lowercase
*
* @param string $string The string being lowercased.
* @return string with all alphabetic characters converted to lowercase.
*/
public static function strtolower($string) {
$utf8Map = Multibyte::utf8($string);
$length = count($utf8Map);
$lowerCase = array();
for ($i = 0; $i < $length; $i++) {
$char = $utf8Map[$i];
if ($char < 128) {
$str = strtolower(chr($char));
$strlen = strlen($str);
for ($ii = 0; $ii < $strlen; $ii++) {
$lower = ord(substr($str, $ii, 1));
}
$lowerCase[] = $lower;
$matched = true;
} else {
$matched = false;
$keys = self::_find($char, 'upper');
if (!empty($keys)) {
foreach ($keys as $key => $value) {
if ($keys[$key]['upper'] == $char && count($keys[$key]['lower'][0]) === 1) {
$lowerCase[] = $keys[$key]['lower'][0];
$matched = true;
break 1;
}
}
}
}
if ($matched === false) {
$lowerCase[] = $char;
}
}
return Multibyte::ascii($lowerCase);
}
/**
* Make a string uppercase
*
* @param string $string The string being uppercased.
* @return string with all alphabetic characters converted to uppercase.
*/
public static function strtoupper($string) {
$utf8Map = Multibyte::utf8($string);
$length = count($utf8Map);
$replaced = array();
$upperCase = array();
for ($i = 0; $i < $length; $i++) {
$char = $utf8Map[$i];
if ($char < 128) {
$str = strtoupper(chr($char));
$strlen = strlen($str);
for ($ii = 0; $ii < $strlen; $ii++) {
$upper = ord(substr($str, $ii, 1));
}
$upperCase[] = $upper;
$matched = true;
} else {
$matched = false;
$keys = self::_find($char);
$keyCount = count($keys);
if (!empty($keys)) {
foreach ($keys as $key => $value) {
$matched = false;
$replace = 0;
if ($length > 1 && count($keys[$key]['lower']) > 1) {
$j = 0;
for ($ii = 0, $count = count($keys[$key]['lower']); $ii < $count; $ii++) {
$nextChar = $utf8Map[$i + $ii];
if (isset($nextChar) && ($nextChar == $keys[$key]['lower'][$j + $ii])) {
$replace++;
}
}
if ($replace == $count) {
$upperCase[] = $keys[$key]['upper'];
$replaced = array_merge($replaced, array_values($keys[$key]['lower']));
$matched = true;
break 1;
}
} elseif ($length > 1 && $keyCount > 1) {
$j = 0;
for ($ii = 1; $ii < $keyCount; $ii++) {
$nextChar = $utf8Map[$i + $ii - 1];
if (in_array($nextChar, $keys[$ii]['lower'])) {
for ($jj = 0, $count = count($keys[$ii]['lower']); $jj < $count; $jj++) {
$nextChar = $utf8Map[$i + $jj];
if (isset($nextChar) && ($nextChar == $keys[$ii]['lower'][$j + $jj])) {
$replace++;
}
}
if ($replace == $count) {
$upperCase[] = $keys[$ii]['upper'];
$replaced = array_merge($replaced, array_values($keys[$ii]['lower']));
$matched = true;
break 2;
}
}
}
}
if ($keys[$key]['lower'][0] == $char) {
$upperCase[] = $keys[$key]['upper'];
$matched = true;
break 1;
}
}
}
}
if ($matched === false && !in_array($char, $replaced, true)) {
$upperCase[] = $char;
}
}
return Multibyte::ascii($upperCase);
}
/**
* Count the number of substring occurrences
*
* @param string $haystack The string being checked.
* @param string $needle The string being found.
* @return int The number of times the $needle substring occurs in the $haystack string.
*/
public static function substrCount($haystack, $needle) {
$count = 0;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
if ($needleCount === 1 && isset($matches[$needle[0]])) {
return $matches[$needle[0]];
}
for ($i = 0; $i < $haystackCount; $i++) {
if (isset($needle[0]) && $needle[0] === $haystack[$i]) {
for ($ii = 1; $ii < $needleCount; $ii++) {
if ($needle[$ii] === $haystack[$i + 1]) {
if ((isset($needle[$ii + 1]) && $haystack[$i + 2]) && $needle[$ii + 1] !== $haystack[$i + 2]) {
$count--;
} else {
$count++;
}
}
}
}
}
return $count;
}
/**
* Get part of string
*
* @param string $string The string being checked.
* @param int $start The first position used in $string.
* @param int $length The maximum length of the returned string.
* @return string The portion of $string specified by the $string and $length parameters.
*/
public static function substr($string, $start, $length = null) {
if ($start === 0 && $length === null) {
return $string;
}
$string = Multibyte::utf8($string);
for ($i = 1; $i <= $start; $i++) {
unset($string[$i - 1]);
}
if ($length === null || count($string) < $length) {
return Multibyte::ascii($string);
}
$string = array_values($string);
$value = array();
for ($i = 0; $i < $length; $i++) {
$value[] = $string[$i];
}
return Multibyte::ascii($value);
}
/**
* Prepare a string for mail transport, using the provided encoding
*
* @param string $string value to encode
* @param string $charset charset to use for encoding. defaults to UTF-8
* @param string $newline Newline string.
* @return string
*/
public static function mimeEncode($string, $charset = null, $newline = "\r\n") {
if (!Multibyte::checkMultibyte($string) && strlen($string) < 75) {
return $string;
}
if (empty($charset)) {
$charset = Configure::read('App.encoding');
}
$charset = strtoupper($charset);
$start = '=?' . $charset . '?B?';
$end = '?=';
$spacer = $end . $newline . ' ' . $start;
$length = 75 - strlen($start) - strlen($end);
$length = $length - ($length % 4);
if ($charset === 'UTF-8') {
$parts = array();
$maxchars = floor(($length * 3) / 4);
$stringLength = strlen($string);
while ($stringLength > $maxchars) {
$i = (int)$maxchars;
$test = ord($string[$i]);
while ($test >= 128 && $test <= 191) {
$i--;
$test = ord($string[$i]);
}
$parts[] = base64_encode(substr($string, 0, $i));
$string = substr($string, $i);
$stringLength = strlen($string);
}
$parts[] = base64_encode($string);
$string = implode($spacer, $parts);
} else {
$string = chunk_split(base64_encode($string), $length, $spacer);
$string = preg_replace('/' . preg_quote($spacer) . '$/', '', $string);
}
return $start . $string . $end;
}
/**
* Return the Code points range for Unicode characters
*
* @param int $decimal Decimal value.
* @return string
*/
protected static function _codepoint($decimal) {
if ($decimal > 128 && $decimal < 256) {
$return = '0080_00ff'; // Latin-1 Supplement
} elseif ($decimal < 384) {
$return = '0100_017f'; // Latin Extended-A
} elseif ($decimal < 592) {
$return = '0180_024F'; // Latin Extended-B
} elseif ($decimal < 688) {
$return = '0250_02af'; // IPA Extensions
} elseif ($decimal >= 880 && $decimal < 1024) {
$return = '0370_03ff'; // Greek and Coptic
} elseif ($decimal < 1280) {
$return = '0400_04ff'; // Cyrillic
} elseif ($decimal < 1328) {
$return = '0500_052f'; // Cyrillic Supplement
} elseif ($decimal < 1424) {
$return = '0530_058f'; // Armenian
} elseif ($decimal >= 7680 && $decimal < 7936) {
$return = '1e00_1eff'; // Latin Extended Additional
} elseif ($decimal < 8192) {
$return = '1f00_1fff'; // Greek Extended
} elseif ($decimal >= 8448 && $decimal < 8528) {
$return = '2100_214f'; // Letterlike Symbols
} elseif ($decimal < 8592) {
$return = '2150_218f'; // Number Forms
} elseif ($decimal >= 9312 && $decimal < 9472) {
$return = '2460_24ff'; // Enclosed Alphanumerics
} elseif ($decimal >= 11264 && $decimal < 11360) {
$return = '2c00_2c5f'; // Glagolitic
} elseif ($decimal < 11392) {
$return = '2c60_2c7f'; // Latin Extended-C
} elseif ($decimal < 11520) {
$return = '2c80_2cff'; // Coptic
} elseif ($decimal >= 65280 && $decimal < 65520) {
$return = 'ff00_ffef'; // Halfwidth and Fullwidth Forms
} else {
$return = false;
}
self::$_codeRange[$decimal] = $return;
return $return;
}
/**
* Find the related code folding values for $char
*
* @param int $char decimal value of character
* @param string $type Type 'lower' or 'upper'. Defaults to 'lower'.
* @return array
*/
protected static function _find($char, $type = 'lower') {
$found = array();
if (!isset(self::$_codeRange[$char])) {
$range = self::_codepoint($char);
if ($range === false) {
return null;
}
if (!Configure::configured('_cake_core_')) {
App::uses('PhpReader', 'Configure');
Configure::config('_cake_core_', new PhpReader(CAKE . 'Config' . DS));
}
Configure::load('unicode' . DS . 'casefolding' . DS . $range, '_cake_core_');
self::$_caseFold[$range] = Configure::read($range);
Configure::delete($range);
}
if (!self::$_codeRange[$char]) {
return null;
}
self::$_table = self::$_codeRange[$char];
$count = count(self::$_caseFold[self::$_table]);
for ($i = 0; $i < $count; $i++) {
if ($type === 'lower' && self::$_caseFold[self::$_table][$i][$type][0] === $char) {
$found[] = self::$_caseFold[self::$_table][$i];
} elseif ($type === 'upper' && self::$_caseFold[self::$_table][$i][$type] === $char) {
$found[] = self::$_caseFold[self::$_table][$i];
}
}
return $found;
}
/**
* Check the $string for multibyte characters
*
* @param string $string Value to test.
* @return bool
*/
public static function checkMultibyte($string) {
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$value = ord(($string[$i]));
if ($value > 128) {
return true;
}
}
return false;
}
}