2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Internationalization
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.I18n
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4116
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-05-15 21:53:55 +00:00
|
|
|
App::uses('CakePlugin', 'Core');
|
2010-12-04 18:20:26 +00:00
|
|
|
App::uses('L10n', 'I18n');
|
|
|
|
App::uses('Multibyte', 'I18n');
|
2012-11-26 23:09:21 +00:00
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* I18n handles translation of Text and time format strings.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.I18n
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-18 05:12:00 +00:00
|
|
|
class I18n {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-11-05 12:19:57 +00:00
|
|
|
* Instance of the L10n class for localization
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-11-05 12:19:57 +00:00
|
|
|
* @var L10n
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $l10n = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-11-10 20:16:21 +00:00
|
|
|
/**
|
|
|
|
* Default domain of translation
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $defaultDomain = 'default';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Current domain of translation
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $domain = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-11 03:22:41 +00:00
|
|
|
/**
|
|
|
|
* Current category of translation
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $category = 'LC_MESSAGES';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Current language used for translations
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_lang = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Translation strings for a specific domain read from the .mo or .po files
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_domains = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-08-20 04:43:34 +00:00
|
|
|
* Set to true when I18N::_bindTextDomain() is called for the first time.
|
2008-05-30 11:40:08 +00:00
|
|
|
* If a translation file is found it is set to false again
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_noLocale = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-11-19 19:17:43 +00:00
|
|
|
* Translation categories
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_categories = array(
|
|
|
|
'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
|
2009-07-03 15:54:26 +00:00
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2014-03-23 11:47:35 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2012-11-28 22:30:47 +00:00
|
|
|
/**
|
|
|
|
* Escape string
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-03-04 01:15:21 +00:00
|
|
|
protected $_escape = null;
|
|
|
|
|
2011-11-05 12:19:57 +00:00
|
|
|
/**
|
|
|
|
* Constructor, use I18n::getInstance() to get the i18n translation object.
|
|
|
|
*/
|
2011-12-09 08:38:20 +00:00
|
|
|
public function __construct() {
|
2011-11-05 12:19:57 +00:00
|
|
|
$this->l10n = new L10n();
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return a static instance of the I18n class
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return I18n
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-04-16 01:23:48 +00:00
|
|
|
public static function getInstance() {
|
2008-05-30 11:40:08 +00:00
|
|
|
static $instance = array();
|
|
|
|
if (!$instance) {
|
2010-11-09 03:07:09 +00:00
|
|
|
$instance[0] = new I18n();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $instance[0];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used by the translation functions in basics.php
|
2010-12-09 03:45:18 +00:00
|
|
|
* Returns a translated string based on current language and translation files stored in locale folder
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $singular String to translate
|
|
|
|
* @param string $plural Plural string (if any)
|
2013-07-28 01:41:22 +00:00
|
|
|
* @param string $domain Domain The domain of the translation. Domains are often used by plugin translations.
|
|
|
|
* If null, the default domain will be used.
|
2010-01-25 20:37:55 +00:00
|
|
|
* @param string $category Category The integer value of the category to use.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $count Count Count is used with $plural to choose the correct plural form.
|
2011-11-19 19:17:43 +00:00
|
|
|
* @param string $language Language to translate string to.
|
2013-07-28 01:41:22 +00:00
|
|
|
* If null it checks for language in session followed by Config.language configuration variable.
|
2014-06-08 09:54:28 +00:00
|
|
|
* @param string $context Context The context of the translation, e.g a verb or a noun.
|
2010-01-25 20:37:55 +00:00
|
|
|
* @return string translated string.
|
2013-07-28 01:41:22 +00:00
|
|
|
* @throws CakeException When '' is provided as a domain.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2014-06-08 09:54:28 +00:00
|
|
|
public static function translate($singular, $plural = null, $domain = null, $category = self::LC_MESSAGES,
|
|
|
|
$count = null, $language = null, $context = null
|
|
|
|
) {
|
2010-11-09 03:07:09 +00:00
|
|
|
$_this = I18n::getInstance();
|
2011-05-17 16:41:35 +00:00
|
|
|
|
2008-11-06 22:57:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-10-11 03:22:41 +00:00
|
|
|
if (is_numeric($category)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$_this->category = $_this->_categories[$category];
|
2008-10-11 03:22:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 19:17:43 +00:00
|
|
|
if (empty($language)) {
|
2012-11-26 23:09:21 +00:00
|
|
|
if (CakeSession::started()) {
|
|
|
|
$language = CakeSession::read('Config.language');
|
|
|
|
}
|
|
|
|
if (empty($language)) {
|
2011-11-19 19:17:43 +00:00
|
|
|
$language = Configure::read('Config.language');
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (($_this->_lang && $_this->_lang !== $language) || !$_this->_lang) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$lang = $_this->l10n->get($language);
|
2011-08-20 04:43:34 +00:00
|
|
|
$_this->_lang = $lang;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($domain === null) {
|
2011-11-10 20:16:21 +00:00
|
|
|
$domain = self::$defaultDomain;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-07-28 01:41:22 +00:00
|
|
|
if ($domain === '') {
|
|
|
|
throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.'));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-09-21 18:45:57 +00:00
|
|
|
$_this->domain = $domain . '_' . $_this->l10n->lang;
|
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (!isset($_this->_domains[$domain][$_this->_lang])) {
|
|
|
|
$_this->_domains[$domain][$_this->_lang] = Cache::read($_this->domain, '_cake_core_');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (!isset($_this->_domains[$domain][$_this->_lang][$_this->category])) {
|
|
|
|
$_this->_bindTextDomain($domain);
|
|
|
|
Cache::write($_this->domain, $_this->_domains[$domain][$_this->_lang], '_cake_core_');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($_this->category === 'LC_TIME') {
|
2011-12-16 06:52:07 +00:00
|
|
|
return $_this->_translateTime($singular, $domain);
|
2010-01-14 14:24:45 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($count)) {
|
2008-10-11 03:22:41 +00:00
|
|
|
$plurals = 0;
|
2011-08-20 04:43:34 +00:00
|
|
|
} 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);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if ($count != 1) {
|
2008-10-11 03:22:41 +00:00
|
|
|
$plurals = 1;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2008-10-11 03:22:41 +00:00
|
|
|
$plurals = 0;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 09:54:28 +00:00
|
|
|
if (!empty($_this->_domains[$domain][$_this->_lang][$_this->category][$singular][$context])) {
|
|
|
|
if (($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$singular][$context]) ||
|
|
|
|
($plurals) && ($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$plural][$context])
|
|
|
|
) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($trans)) {
|
2008-10-11 03:22:41 +00:00
|
|
|
if (isset($trans[$plurals])) {
|
|
|
|
$trans = $trans[$plurals];
|
2011-12-26 04:27:01 +00:00
|
|
|
} 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];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strlen($trans)) {
|
2010-03-26 03:01:50 +00:00
|
|
|
return $trans;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-11 03:22:41 +00:00
|
|
|
if (!empty($plurals)) {
|
2010-03-26 03:01:50 +00:00
|
|
|
return $plural;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-03-26 03:01:50 +00:00
|
|
|
return $singular;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-22 01:13:45 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Clears the domains internal data array. Useful for testing i18n.
|
2010-09-22 01:13:45 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 03:27:22 +00:00
|
|
|
public static function clear() {
|
2010-11-09 03:07:09 +00:00
|
|
|
$self = I18n::getInstance();
|
2011-08-20 04:43:34 +00:00
|
|
|
$self->_domains = array();
|
2010-09-22 01:13:45 +00:00
|
|
|
}
|
|
|
|
|
2010-09-25 03:27:22 +00:00
|
|
|
/**
|
|
|
|
* Get the loaded domains cache.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function domains() {
|
2010-11-09 03:07:09 +00:00
|
|
|
$self = I18n::getInstance();
|
2011-08-20 04:43:34 +00:00
|
|
|
return $self->_domains;
|
2010-09-25 03:27:22 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Attempts to find the plural form of a string.
|
|
|
|
*
|
2008-10-11 03:22:41 +00:00
|
|
|
* @param string $header Type
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $n Number
|
|
|
|
* @return int plural match
|
2014-12-08 18:33:24 +00:00
|
|
|
* @link http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
|
|
|
|
* @link https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals#List_of_Plural_Rules
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected function _pluralGuess($header, $n) {
|
2008-10-11 03:22:41 +00:00
|
|
|
if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
|
|
|
|
return 0;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-11 03:22:41 +00:00
|
|
|
if ($header === "nplurals=2;plural=n!=1;") {
|
|
|
|
return $n != 1 ? 1 : 0;
|
|
|
|
} elseif ($header === "nplurals=2;plural=n>1;") {
|
|
|
|
return $n > 1 ? 1 : 0;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-11 03:22:41 +00:00
|
|
|
if (strpos($header, "plurals=3")) {
|
|
|
|
if (strpos($header, "100!=11")) {
|
|
|
|
if (strpos($header, "10<=4")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2008-10-11 03:22:41 +00:00
|
|
|
} elseif (strpos($header, "100<10")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-11 03:22:41 +00:00
|
|
|
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
|
|
|
|
} elseif (strpos($header, "n==2")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
|
2008-10-11 03:22:41 +00:00
|
|
|
} elseif (strpos($header, "n==0")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
|
2008-10-11 03:22:41 +00:00
|
|
|
} elseif (strpos($header, "n>=2")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
|
2008-10-11 03:22:41 +00:00
|
|
|
} elseif (strpos($header, "10>=2")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2008-10-11 03:22:41 +00:00
|
|
|
}
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
|
2008-10-11 03:22:41 +00:00
|
|
|
} elseif (strpos($header, "plurals=4")) {
|
|
|
|
if (strpos($header, "100==2")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
|
2008-10-11 03:22:41 +00:00
|
|
|
} elseif (strpos($header, "n>=3")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
|
2008-10-11 03:22:41 +00:00
|
|
|
} elseif (strpos($header, "100>=1")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
|
2008-10-11 03:22:41 +00:00
|
|
|
}
|
|
|
|
} elseif (strpos($header, "plurals=5")) {
|
2009-02-18 01:23:42 +00:00
|
|
|
return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
|
2014-12-08 18:15:34 +00:00
|
|
|
} elseif (strpos($header, "plurals=6")) {
|
|
|
|
return $n == 0 ? 0 :
|
|
|
|
($n == 1 ? 1 :
|
|
|
|
($n == 2 ? 2 :
|
|
|
|
($n % 100 >= 3 && $n % 100 <= 10 ? 3 :
|
|
|
|
($n % 100 >= 11 ? 4 : 5))));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2014-12-08 18:15:34 +00:00
|
|
|
|
|
|
|
return 0;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Binds the given domain to a file in the specified directory.
|
|
|
|
*
|
|
|
|
* @param string $domain Domain to bind
|
|
|
|
* @return string Domain binded
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected function _bindTextDomain($domain) {
|
|
|
|
$this->_noLocale = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
$core = true;
|
|
|
|
$merge = array();
|
2009-07-01 02:56:15 +00:00
|
|
|
$searchPaths = App::path('locales');
|
2011-05-09 05:06:52 +00:00
|
|
|
$plugins = CakePlugin::loaded();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($plugins)) {
|
Closes #1855, Separate translation files per plugin. This is now supported using !__d() and !__dn().
Example singular usage: !__d('plugin_name', $string, true); returns translated singular string from pluginsPath/plugin_name/locale/"language"/LC_MESSAGE/plugin_name.(po/mo).
Example plural usage: sprintf(!__dn('plugin_name', '%d = 1', '%d = 0 or > 1', $number, true), $number ); returns translated plural strings from pluginsPath/plugin_name/locale/"language"/LC_MESSAGE/plugin_name.(po/mo).
Closes #5007, Support for multiple additional locale paths. Configure::write('localePaths', $paths); accepts an array of $paths. $localePaths = array(); can also be used in bootstrap.php
Added Configure::$shellPaths; additional paths can be set in bootstrap.php $shellPaths = array();
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7754 3807eeeb-6ff5-0310-8944-8be069107fe0
2008-10-16 05:16:00 +00:00
|
|
|
foreach ($plugins as $plugin) {
|
2011-05-09 05:06:52 +00:00
|
|
|
$pluginDomain = Inflector::underscore($plugin);
|
|
|
|
if ($pluginDomain === $domain) {
|
2011-07-27 22:11:25 +00:00
|
|
|
$searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
|
2014-12-23 12:09:53 +00:00
|
|
|
if (!Configure::read('I18n.preferApp')) {
|
|
|
|
$searchPaths = array_reverse($searchPaths);
|
|
|
|
}
|
Closes #1855, Separate translation files per plugin. This is now supported using !__d() and !__dn().
Example singular usage: !__d('plugin_name', $string, true); returns translated singular string from pluginsPath/plugin_name/locale/"language"/LC_MESSAGE/plugin_name.(po/mo).
Example plural usage: sprintf(!__dn('plugin_name', '%d = 1', '%d = 0 or > 1', $number, true), $number ); returns translated plural strings from pluginsPath/plugin_name/locale/"language"/LC_MESSAGE/plugin_name.(po/mo).
Closes #5007, Support for multiple additional locale paths. Configure::write('localePaths', $paths); accepts an array of $paths. $localePaths = array(); can also be used in bootstrap.php
Added Configure::$shellPaths; additional paths can be set in bootstrap.php $shellPaths = array();
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7754 3807eeeb-6ff5-0310-8944-8be069107fe0
2008-10-16 05:16:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
Closes #1855, Separate translation files per plugin. This is now supported using !__d() and !__dn().
Example singular usage: !__d('plugin_name', $string, true); returns translated singular string from pluginsPath/plugin_name/locale/"language"/LC_MESSAGE/plugin_name.(po/mo).
Example plural usage: sprintf(!__dn('plugin_name', '%d = 1', '%d = 0 or > 1', $number, true), $number ); returns translated plural strings from pluginsPath/plugin_name/locale/"language"/LC_MESSAGE/plugin_name.(po/mo).
Closes #5007, Support for multiple additional locale paths. Configure::write('localePaths', $paths); accepts an array of $paths. $localePaths = array(); can also be used in bootstrap.php
Added Configure::$shellPaths; additional paths can be set in bootstrap.php $shellPaths = array();
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7754 3807eeeb-6ff5-0310-8944-8be069107fe0
2008-10-16 05:16:00 +00:00
|
|
|
foreach ($searchPaths as $directory) {
|
2008-10-11 03:22:41 +00:00
|
|
|
foreach ($this->l10n->languagePath as $lang) {
|
2009-11-17 18:47:23 +00:00
|
|
|
$localeDef = $directory . $lang . DS . $this->category;
|
2011-09-29 15:24:08 +00:00
|
|
|
if (is_file($localeDef)) {
|
|
|
|
$definitions = self::loadLocaleDefinition($localeDef);
|
|
|
|
if ($definitions !== false) {
|
2013-08-26 22:34:39 +00:00
|
|
|
$this->_domains[$domain][$this->_lang][$this->category] = $definitions;
|
2011-09-29 15:24:08 +00:00
|
|
|
$this->_noLocale = false;
|
|
|
|
return $domain;
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($core) {
|
2009-09-30 18:25:56 +00:00
|
|
|
$app = $directory . $lang . DS . $this->category . DS . 'core';
|
2011-09-29 15:24:08 +00:00
|
|
|
$translations = false;
|
2009-09-30 18:25:56 +00:00
|
|
|
|
2011-09-29 15:24:08 +00:00
|
|
|
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;
|
2011-08-20 04:43:34 +00:00
|
|
|
$merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category];
|
|
|
|
$this->_noLocale = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
$core = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 15:24:08 +00:00
|
|
|
$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;
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_noLocale = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (empty($this->_domains[$domain][$this->_lang][$this->category])) {
|
|
|
|
$this->_domains[$domain][$this->_lang][$this->category] = array();
|
2010-03-26 03:01:50 +00:00
|
|
|
return $domain;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-05-17 16:41:35 +00:00
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) {
|
|
|
|
$head = $this->_domains[$domain][$this->_lang][$this->category][""];
|
2011-05-17 16:41:35 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach (explode("\n", $head) as $line) {
|
2013-06-09 15:20:08 +00:00
|
|
|
$header = strtok($line, ':');
|
2008-05-30 11:40:08 +00:00
|
|
|
$line = trim(strtok("\n"));
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
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"]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-12 02:57:54 +00:00
|
|
|
$this->_domains = Hash::mergeDiff($this->_domains, $merge);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
|
|
|
|
unset($this->_domains[$domain][$this->_lang][$this->category][null]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-29 15:24:08 +00:00
|
|
|
|
2010-03-26 03:01:50 +00:00
|
|
|
return $domain;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-09-29 15:24:08 +00:00
|
|
|
* Loads the binary .mo file and returns array of translations
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-09-29 15:24:08 +00:00
|
|
|
* @param string $filename Binary .mo file to load
|
|
|
|
* @return mixed Array of translations on success or false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-09-29 15:24:08 +00:00
|
|
|
public static function loadMo($filename) {
|
|
|
|
$translations = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-03-05 03:49:38 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
// Binary files extracted makes non-standard local variables
|
2011-09-29 15:24:08 +00:00
|
|
|
if ($data = file_get_contents($filename)) {
|
|
|
|
$translations = array();
|
2014-06-08 09:54:28 +00:00
|
|
|
$context = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$header = substr($data, 0, 20);
|
2013-06-09 15:20:08 +00:00
|
|
|
$header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($header);
|
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if ((dechex($magic) === '950412de' || dechex($magic) === 'ffffffff950412de') && !$version) {
|
2008-05-30 11:40:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-06-08 09:54:28 +00:00
|
|
|
|
|
|
|
if ($msgid != '') {
|
|
|
|
$msgstr = array($context => $msgstr);
|
|
|
|
}
|
2011-09-29 15:24:08 +00:00
|
|
|
$translations[$msgid] = $msgstr;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($msgid_plural)) {
|
2011-09-29 15:24:08 +00:00
|
|
|
$translations[$msgid_plural] =& $translations[$msgid];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 03:49:38 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2011-09-29 15:24:08 +00:00
|
|
|
|
|
|
|
return $translations;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-09-29 15:24:08 +00:00
|
|
|
* Loads the text .po file and returns array of translations
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-09-29 15:24:08 +00:00
|
|
|
* @param string $filename Text .po file to load
|
|
|
|
* @return mixed Array of translations on success or false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-09-29 15:24:08 +00:00
|
|
|
public static function loadPo($filename) {
|
2013-06-09 15:20:08 +00:00
|
|
|
if (!$file = fopen($filename, 'r')) {
|
2011-09-29 15:24:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$type = 0;
|
|
|
|
$translations = array();
|
2013-06-09 15:20:08 +00:00
|
|
|
$translationKey = '';
|
2014-06-08 09:54:28 +00:00
|
|
|
$translationContext = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$plural = 0;
|
2013-06-09 15:20:08 +00:00
|
|
|
$header = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
do {
|
2009-11-16 16:14:03 +00:00
|
|
|
$line = trim(fgets($file));
|
2013-06-09 15:20:08 +00:00
|
|
|
if ($line === '' || $line[0] === '#') {
|
2014-06-08 09:54:28 +00:00
|
|
|
$translationContext = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
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;
|
2013-06-09 15:20:08 +00:00
|
|
|
$translationKey = '';
|
2014-06-08 09:54:28 +00:00
|
|
|
} elseif (preg_match("/msgctxt[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
|
|
|
|
$translationContext = $regs[1];
|
2008-05-30 11:40:08 +00:00
|
|
|
} 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) {
|
2014-06-08 09:54:28 +00:00
|
|
|
$translations[$translationKey][$translationContext] = stripcslashes($regs[1]);
|
2008-05-30 11:40:08 +00:00
|
|
|
$type = 4;
|
|
|
|
} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
|
|
|
|
$type = 4;
|
2014-06-08 09:54:28 +00:00
|
|
|
$translations[$translationKey][$translationContext] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
|
2014-06-08 09:54:28 +00:00
|
|
|
$translations[$translationKey][$translationContext] .= stripcslashes($regs[1]);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
|
|
|
|
$type = 6;
|
2008-11-06 22:57:50 +00:00
|
|
|
} 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) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$plural = $regs[1];
|
2014-06-08 09:54:28 +00:00
|
|
|
$translations[$translationKey][$translationContext][$plural] = stripcslashes($regs[2]);
|
2008-11-06 22:57:50 +00:00
|
|
|
$type = 7;
|
|
|
|
} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$plural = $regs[1];
|
2014-06-08 09:54:28 +00:00
|
|
|
$translations[$translationKey][$translationContext][$plural] = '';
|
2008-11-06 22:57:50 +00:00
|
|
|
$type = 7;
|
|
|
|
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
|
2014-06-08 09:54:28 +00:00
|
|
|
$translations[$translationKey][$translationContext][$plural] .= stripcslashes($regs[1]);
|
2008-05-30 11:40:08 +00:00
|
|
|
} 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) {
|
2013-06-09 15:20:08 +00:00
|
|
|
$header = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
$type = 5;
|
|
|
|
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
|
|
|
|
$header .= stripcslashes($regs[1]);
|
|
|
|
} else {
|
2014-06-08 09:54:28 +00:00
|
|
|
unset($translations[$translationKey][$translationContext]);
|
2008-05-30 11:40:08 +00:00
|
|
|
$type = 0;
|
2013-06-09 15:20:08 +00:00
|
|
|
$translationKey = '';
|
2014-06-08 09:54:28 +00:00
|
|
|
$translationContext = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$plural = 0;
|
|
|
|
}
|
|
|
|
} while (!feof($file));
|
|
|
|
fclose($file);
|
2011-09-29 15:24:08 +00:00
|
|
|
|
2013-06-09 15:20:08 +00:00
|
|
|
$merge[''] = $header;
|
2011-09-29 15:24:08 +00:00
|
|
|
return array_merge($merge, $translations);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-01-14 14:24:45 +00:00
|
|
|
/**
|
|
|
|
* Parses a locale definition file following the POSIX standard
|
|
|
|
*
|
2011-09-29 15:24:08 +00:00
|
|
|
* @param string $filename Locale definition filename
|
|
|
|
* @return mixed Array of definitions on success or false on failure
|
2010-01-14 14:24:45 +00:00
|
|
|
*/
|
2011-09-29 15:24:08 +00:00
|
|
|
public static function loadLocaleDefinition($filename) {
|
2013-06-09 15:20:08 +00:00
|
|
|
if (!$file = fopen($filename, 'r')) {
|
2011-09-29 15:24:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$definitions = array();
|
2009-11-17 00:00:10 +00:00
|
|
|
$comment = '#';
|
|
|
|
$escape = '\\';
|
|
|
|
$currentToken = false;
|
|
|
|
$value = '';
|
2011-09-29 15:24:08 +00:00
|
|
|
$_this = I18n::getInstance();
|
2009-11-17 18:47:23 +00:00
|
|
|
while ($line = fgets($file)) {
|
2009-11-17 00:00:10 +00:00
|
|
|
$line = trim($line);
|
|
|
|
if (empty($line) || $line[0] === $comment) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-16 06:52:07 +00:00
|
|
|
$parts = preg_split("/[[:space:]]+/", $line);
|
2009-11-17 00:00:10 +00:00
|
|
|
if ($parts[0] === 'comment_char') {
|
|
|
|
$comment = $parts[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($parts[0] === 'escape_char') {
|
|
|
|
$escape = $parts[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$count = count($parts);
|
2013-09-01 04:20:53 +00:00
|
|
|
if ($count === 2) {
|
2009-11-17 00:00:10 +00:00
|
|
|
$currentToken = $parts[0];
|
|
|
|
$value = $parts[1];
|
2013-09-01 04:20:53 +00:00
|
|
|
} elseif ($count === 1) {
|
2013-09-01 04:16:10 +00:00
|
|
|
$value = is_array($value) ? $parts[0] : $value . $parts[0];
|
2009-11-17 00:00:10 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$len = strlen($value) - 1;
|
|
|
|
if ($value[$len] === $escape) {
|
2010-03-03 04:41:10 +00:00
|
|
|
$value = substr($value, 0, $len);
|
2009-11-17 00:00:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:31 +00:00
|
|
|
$mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
|
2010-01-16 23:50:04 +00:00
|
|
|
$replacements = array_map('crc32', $mustEscape);
|
|
|
|
$value = str_replace($mustEscape, $replacements, $value);
|
|
|
|
$value = explode(';', $value);
|
2012-03-04 01:15:21 +00:00
|
|
|
$_this->_escape = $escape;
|
2009-11-17 00:00:10 +00:00
|
|
|
foreach ($value as $i => $val) {
|
2010-01-16 23:50:04 +00:00
|
|
|
$val = trim($val, '"');
|
2011-09-29 15:24:08 +00:00
|
|
|
$val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
|
2010-01-16 23:50:04 +00:00
|
|
|
$val = str_replace($replacements, $mustEscape, $val);
|
2009-11-17 00:00:10 +00:00
|
|
|
$value[$i] = $val;
|
|
|
|
}
|
2013-09-01 04:20:53 +00:00
|
|
|
if (count($value) === 1) {
|
2011-09-29 15:24:08 +00:00
|
|
|
$definitions[$currentToken] = array_pop($value);
|
2009-11-17 00:00:10 +00:00
|
|
|
} else {
|
2011-09-29 15:24:08 +00:00
|
|
|
$definitions[$currentToken] = $value;
|
2009-11-17 00:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-29 15:24:08 +00:00
|
|
|
|
|
|
|
return $definitions;
|
2009-11-17 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-08 11:41:48 +00:00
|
|
|
/**
|
|
|
|
* Puts the parameters in raw translated strings
|
|
|
|
*
|
|
|
|
* @param string $translated The raw translated string
|
|
|
|
* @param array $args The arguments to put in the translation
|
|
|
|
* @return string Translated string with arguments
|
|
|
|
*/
|
|
|
|
public static function insertArgs($translated, array $args) {
|
2014-08-31 01:28:11 +00:00
|
|
|
$len = count($args);
|
|
|
|
if ($len === 0 || ($len === 1 && $args[0] === null)) {
|
2014-06-08 11:41:48 +00:00
|
|
|
return $translated;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($args[0])) {
|
|
|
|
$args = $args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
|
|
|
|
return vsprintf($translated, $args);
|
|
|
|
}
|
|
|
|
|
2010-01-14 14:24:45 +00:00
|
|
|
/**
|
|
|
|
* Auxiliary function to parse a symbol from a locale definition file
|
|
|
|
*
|
|
|
|
* @param string $string Symbol to be parsed
|
|
|
|
* @return string parsed symbol
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected function _parseLiteralValue($string) {
|
2009-11-17 18:47:23 +00:00
|
|
|
$string = $string[1];
|
2012-03-04 01:15:21 +00:00
|
|
|
if (substr($string, 0, 2) === $this->_escape . 'x') {
|
|
|
|
$delimiter = $this->_escape . 'x';
|
2013-05-17 08:36:17 +00:00
|
|
|
return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
|
2009-11-17 18:47:23 +00:00
|
|
|
}
|
2012-03-04 01:15:21 +00:00
|
|
|
if (substr($string, 0, 2) === $this->_escape . 'd') {
|
|
|
|
$delimiter = $this->_escape . 'd';
|
2012-09-15 10:06:02 +00:00
|
|
|
return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
|
2009-11-17 18:47:23 +00:00
|
|
|
}
|
2012-03-04 01:15:21 +00:00
|
|
|
if ($string[0] === $this->_escape && isset($string[1]) && is_numeric($string[1])) {
|
|
|
|
$delimiter = $this->_escape;
|
2012-09-15 10:06:02 +00:00
|
|
|
return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
|
2009-11-17 00:00:10 +00:00
|
|
|
}
|
2010-01-14 20:08:48 +00:00
|
|
|
if (substr($string, 0, 3) === 'U00') {
|
2009-11-17 18:47:23 +00:00
|
|
|
$delimiter = 'U00';
|
2012-09-15 10:06:02 +00:00
|
|
|
return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
|
2009-11-17 00:00:10 +00:00
|
|
|
}
|
2010-03-03 04:41:10 +00:00
|
|
|
if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
|
2010-03-03 02:42:53 +00:00
|
|
|
return Multibyte::ascii(array(hexdec($match[1])));
|
|
|
|
}
|
2009-11-17 00:00:10 +00:00
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2010-01-14 14:24:45 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
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])) {
|
2010-01-14 14:24:45 +00:00
|
|
|
return $trans;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $format;
|
|
|
|
}
|
2012-03-04 01:15:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|