I18N.php 6.4 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
Qiang Xue committed
7 8 9 10 11

namespace yii\i18n;

use Yii;
use yii\base\Component;
12
use yii\base\InvalidConfigException;
Qiang Xue committed
13
use yii\base\InvalidParamException;
Qiang Xue committed
14

Qiang Xue committed
15 16 17 18 19 20
/**
 * I18N provides features related with internationalization (I18N) and localization (L10N).
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
21 22
class I18N extends Component
{
23 24 25 26 27
	/**
	 * @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'.
Qiang Xue committed
28 29 30 31 32 33 34 35 36 37
	 *
	 * 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.
38 39
	 */
	public $translations;
Qiang Xue committed
40 41 42 43 44 45 46 47 48 49 50
	/**
	 * @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';
51

Qiang Xue committed
52 53 54
	/**
	 * Initializes the component by configuring the default message categories.
	 */
55 56
	public function init()
	{
Qiang Xue committed
57
		parent::init();
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		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',
			);
		}
	}

Qiang Xue committed
74 75 76 77
	/**
	 * 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.
78
	 * @param string $category the message category.
Qiang Xue committed
79 80
	 * @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.
Qiang Xue committed
81
	 * @param string $language the language code (e.g. `en_US`, `en`).
Qiang Xue committed
82 83
	 * @return string the translated message.
	 */
Qiang Xue committed
84
	public function translate($category, $message, $params, $language)
Qiang Xue committed
85
	{
86
		$message = $this->getMessageSource($category)->translate($category, $message, $language);
Qiang Xue committed
87

88 89 90
		if (!is_array($params)) {
			$params = array($params);
		}
Qiang Xue committed
91

92
		if (isset($params[0])) {
Qiang Xue committed
93
			$message = $this->applyPluralRules($message, $params[0], $language);
94 95 96 97 98
			if (!isset($params['{n}'])) {
				$params['{n}'] = $params[0];
			}
			unset($params[0]);
		}
Qiang Xue committed
99

100
		return empty($params) ? $message : strtr($message, $params);
Qiang Xue committed
101 102
	}

Qiang Xue committed
103 104 105 106 107 108
	/**
	 * 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.
	 */
Qiang Xue committed
109
	public function getMessageSource($category)
Qiang Xue committed
110
	{
111 112
		if (isset($this->translations[$category])) {
			$source = $this->translations[$category];
Qiang Xue committed
113
		} else {
114 115 116 117 118 119 120
			// try wildcard matching
			foreach ($this->translations as $pattern => $config) {
				if (substr($pattern, -1) === '*' && strpos($category, rtrim($pattern, '*')) === 0) {
					$source = $config;
					break;
				}
			}
Qiang Xue committed
121
		}
122 123
		if (isset($source)) {
			return $source instanceof MessageSource ? $source : Yii::createObject($source);
Qiang Xue committed
124
		} else {
125
			throw new InvalidConfigException("Unable to locate message source for category '$category'.");
Qiang Xue committed
126 127 128
		}
	}

Qiang Xue committed
129 130 131 132 133 134 135 136
	/**
	 * 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)
Qiang Xue committed
137 138 139 140 141
	{
		if (strpos($message, '|') === false) {
			return $message;
		}
		$chunks = explode('|', $message);
Qiang Xue committed
142 143

		$rules = $this->getPluralRules($language);
Qiang Xue committed
144
		foreach ($rules as $i => $rule) {
145
			if (isset($chunks[$i]) && $this->evaluate($rule, $number)) {
Qiang Xue committed
146 147 148 149 150 151 152
				return $chunks[$i];
			}
		}
		$n = count($rules);
		return isset($chunks[$n]) ? $chunks[$n] : $chunks[0];
	}

Qiang Xue committed
153 154 155 156 157 158 159 160 161 162 163
	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])) {
164
			return $this->_pluralRules[$language];
Qiang Xue committed
165 166 167 168 169 170 171 172 173 174 175
		}
		$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");
		}
	}

Qiang Xue committed
176 177 178 179 180 181
	/**
	 * 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
	 */
182
	protected function evaluate($expression, $n)
Qiang Xue committed
183
	{
Qiang Xue committed
184
		return eval("return $expression;");
Qiang Xue committed
185 186
	}
}