You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
6.6 KiB
192 lines
6.6 KiB
12 years ago
|
<?php
|
||
12 years ago
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
12 years ago
|
|
||
|
namespace yii\i18n;
|
||
|
|
||
|
use Yii;
|
||
|
use yii\base\Component;
|
||
12 years ago
|
use yii\base\InvalidConfigException;
|
||
12 years ago
|
use yii\base\InvalidParamException;
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* I18N provides features related with internationalization (I18N) and localization (L10N).
|
||
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class I18N extends Component
|
||
|
{
|
||
12 years ago
|
/**
|
||
|
* @var array list of [[MessageSource]] configurations or objects. The array keys are message
|
||
|
* categories, and the array values are the corresponding [[MessageSource]] objects or the configurations
|
||
|
* for creating the [[MessageSource]] objects. The message categories can contain the wildcard '*' at the end
|
||
|
* to match multiple categories with the same prefix. For example, 'app\*' matches both 'app\cat1' and 'app\cat2'.
|
||
12 years ago
|
*
|
||
|
* This property may be modified on the fly by extensions who want to have their own message sources
|
||
|
* registered under their own namespaces.
|
||
|
*
|
||
|
* The category "yii" and "app" are always defined. The former refers to the messages used in the Yii core
|
||
|
* framework code, while the latter refers to the default message category for custom application code.
|
||
|
* By default, both of these categories use [[PhpMessageSource]] and the corresponding message files are
|
||
|
* stored under "@yii/messages" and "@app/messages", respectively.
|
||
|
*
|
||
|
* You may override the configuration of both categories.
|
||
12 years ago
|
*/
|
||
|
public $translations;
|
||
12 years ago
|
/**
|
||
|
* @var string the path or path alias of the file that contains the plural rules.
|
||
|
* By default, this refers to a file shipped with the Yii distribution. The file is obtained
|
||
|
* by converting from the data file in the CLDR project.
|
||
|
*
|
||
|
* If the default rule file does not contain the expected rules, you may copy and modify it
|
||
|
* for your application, and then configure this property to point to your modified copy.
|
||
|
*
|
||
|
* @see http://www.unicode.org/cldr/charts/supplemental/language_plural_rules.html
|
||
|
*/
|
||
|
public $pluralRuleFile = '@yii/i18n/data/plurals.php';
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* Initializes the component by configuring the default message categories.
|
||
|
*/
|
||
12 years ago
|
public function init()
|
||
|
{
|
||
12 years ago
|
parent::init();
|
||
12 years ago
|
if (!isset($this->translations['yii'])) {
|
||
|
$this->translations['yii'] = array(
|
||
|
'class' => 'yii\i18n\PhpMessageSource',
|
||
|
'sourceLanguage' => 'en_US',
|
||
|
'basePath' => '@yii/messages',
|
||
|
);
|
||
|
}
|
||
|
if (!isset($this->translations['app'])) {
|
||
|
$this->translations['app'] = array(
|
||
|
'class' => 'yii\i18n\PhpMessageSource',
|
||
|
'sourceLanguage' => 'en_US',
|
||
|
'basePath' => '@app/messages',
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Translates a message to the specified language.
|
||
|
* If the first parameter in `$params` is a number and it is indexed by 0, appropriate plural rules
|
||
|
* will be applied to the translated message.
|
||
12 years ago
|
* @param string $category the message category.
|
||
12 years ago
|
* @param string $message the message to be translated.
|
||
|
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
|
||
|
* @param string $language the language code (e.g. `en_US`, `en`). If this is null, the current
|
||
|
* [[\yii\base\Application::language|application language]] will be used.
|
||
|
* @return string the translated message.
|
||
|
*/
|
||
12 years ago
|
public function translate($category, $message, $params = array(), $language = null)
|
||
12 years ago
|
{
|
||
|
if ($language === null) {
|
||
12 years ago
|
$language = Yii::$app->language;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
$message = $this->getMessageSource($category)->translate($category, $message, $language);
|
||
12 years ago
|
|
||
12 years ago
|
if (!is_array($params)) {
|
||
|
$params = array($params);
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if (isset($params[0])) {
|
||
12 years ago
|
$message = $this->applyPluralRules($message, $params[0], $language);
|
||
12 years ago
|
if (!isset($params['{n}'])) {
|
||
|
$params['{n}'] = $params[0];
|
||
|
}
|
||
|
unset($params[0]);
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
return empty($params) ? $message : strtr($message, $params);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Returns the message source for the given category.
|
||
|
* @param string $category the category name.
|
||
|
* @return MessageSource the message source for the given category.
|
||
|
* @throws InvalidConfigException if there is no message source available for the specified category.
|
||
|
*/
|
||
12 years ago
|
public function getMessageSource($category)
|
||
12 years ago
|
{
|
||
12 years ago
|
if (isset($this->translations[$category])) {
|
||
|
$source = $this->translations[$category];
|
||
12 years ago
|
} else {
|
||
12 years ago
|
// try wildcard matching
|
||
|
foreach ($this->translations as $pattern => $config) {
|
||
|
if (substr($pattern, -1) === '*' && strpos($category, rtrim($pattern, '*')) === 0) {
|
||
|
$source = $config;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
if (isset($source)) {
|
||
|
return $source instanceof MessageSource ? $source : Yii::createObject($source);
|
||
12 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidConfigException("Unable to locate message source for category '$category'.");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Applies appropriate plural rules to the given message.
|
||
|
* @param string $message the message to be applied with plural rules
|
||
|
* @param mixed $number the number by which plural rules will be applied
|
||
|
* @param string $language the language code that determines which set of plural rules to be applied.
|
||
|
* @return string the message that has applied plural rules
|
||
|
*/
|
||
|
protected function applyPluralRules($message, $number, $language)
|
||
12 years ago
|
{
|
||
|
if (strpos($message, '|') === false) {
|
||
|
return $message;
|
||
|
}
|
||
|
$chunks = explode('|', $message);
|
||
12 years ago
|
|
||
|
$rules = $this->getPluralRules($language);
|
||
12 years ago
|
foreach ($rules as $i => $rule) {
|
||
12 years ago
|
if (isset($chunks[$i]) && $this->evaluate($rule, $number)) {
|
||
12 years ago
|
return $chunks[$i];
|
||
|
}
|
||
|
}
|
||
|
$n = count($rules);
|
||
|
return isset($chunks[$n]) ? $chunks[$n] : $chunks[0];
|
||
|
}
|
||
|
|
||
12 years ago
|
private $_pluralRules = array(); // language => rule set
|
||
|
|
||
|
/**
|
||
|
* Returns the plural rules for the given language code.
|
||
|
* @param string $language the language code (e.g. `en_US`, `en`).
|
||
|
* @return array the plural rules
|
||
|
* @throws InvalidParamException if the language code is invalid.
|
||
|
*/
|
||
|
protected function getPluralRules($language)
|
||
|
{
|
||
|
if (isset($this->_pluralRules[$language])) {
|
||
|
return $this->_pluralRules;
|
||
|
}
|
||
|
$allRules = require(Yii::getAlias($this->pluralRuleFile));
|
||
|
if (isset($allRules[$language])) {
|
||
|
return $this->_pluralRules[$language] = $allRules[$language];
|
||
|
} elseif (preg_match('/^[a-z]+/', strtolower($language), $matches)) {
|
||
|
return $this->_pluralRules[$language] = isset($allRules[$matches[0]]) ? $allRules[$matches[0]] : array();
|
||
|
} else {
|
||
|
throw new InvalidParamException("Invalid language code: $language");
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Evaluates a PHP expression with the given number value.
|
||
|
* @param string $expression the PHP expression
|
||
|
* @param mixed $n the number value
|
||
|
* @return boolean the expression result
|
||
|
*/
|
||
12 years ago
|
protected function evaluate($expression, $n)
|
||
12 years ago
|
{
|
||
12 years ago
|
return eval("return $expression;");
|
||
12 years ago
|
}
|
||
|
}
|