3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
15
* I18N provides features related with internationalization (I18N) and localization (L10N).
17
* I18N is configured as an application component in [[\yii\base\Application]] by default.
18
* You can access that instance via `Yii::$app->i18n`.
20
* @property MessageFormatter $messageFormatter The message formatter to be used to format message via ICU
21
* message format. Note that the type of this property differs in getter and setter. See
22
* [[getMessageFormatter()]] and [[setMessageFormatter()]] for details.
24
* @author Qiang Xue <qiang.xue@gmail.com>
27
class I18N extends Component
30
* @var array list of [[MessageSource]] configurations or objects. The array keys are message
31
* category patterns, and the array values are the corresponding [[MessageSource]] objects or the configurations
32
* for creating the [[MessageSource]] objects.
34
* The message category patterns can contain the wildcard `*` at the end to match multiple categories with the same prefix.
35
* For example, `app/*` matches both `app/cat1` and `app/cat2`.
37
* The `*` category pattern will match all categories that do not match any other category patterns.
39
* This property may be modified on the fly by extensions who want to have their own message sources
40
* registered under their own namespaces.
42
* The category `yii` and `app` are always defined. The former refers to the messages used in the Yii core
43
* framework code, while the latter refers to the default message category for custom application code.
44
* By default, both of these categories use [[PhpMessageSource]] and the corresponding message files are
45
* stored under `@yii/messages` and `@app/messages`, respectively.
47
* You may override the configuration of both categories.
53
* Initializes the component by configuring the default message categories.
55
public function init()
58
if (!isset($this->translations['yii']) && !isset($this->translations['yii*'])) {
59
$this->translations['yii'] = [
60
'class' => 'yii\i18n\PhpMessageSource',
61
'sourceLanguage' => 'en-US',
62
'basePath' => '@yii/messages',
66
if (!isset($this->translations['app']) && !isset($this->translations['app*'])) {
67
$this->translations['app'] = [
68
'class' => 'yii\i18n\PhpMessageSource',
69
'sourceLanguage' => Yii::$app->sourceLanguage,
70
'basePath' => '@app/messages',
76
* Translates a message to the specified language.
78
* After translation the message will be formatted using [[MessageFormatter]] if it contains
79
* ICU message format and `$params` are not empty.
81
* @param string $category the message category.
82
* @param string $message the message to be translated.
83
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
84
* @param string $language the language code (e.g. `en-US`, `en`).
85
* @return string the translated and formatted message.
87
public function translate($category, $message, $params, $language)
89
$messageSource = $this->getMessageSource($category);
90
$translation = $messageSource->translate($category, $message, $language);
91
if ($translation === false) {
92
return $this->format($message, $params, $messageSource->sourceLanguage);
95
return $this->format($translation, $params, $language);
99
* Formats a message using [[MessageFormatter]].
101
* @param string $message the message to be formatted.
102
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
103
* @param string $language the language code (e.g. `en-US`, `en`).
104
* @return string the formatted message.
106
public function format($message, $params, $language)
108
$params = (array) $params;
109
if ($params === []) {
113
if (preg_match('~{\s*[\w.]+\s*,~u', $message)) {
114
$formatter = $this->getMessageFormatter();
115
$result = $formatter->format($message, $params, $language);
116
if ($result === false) {
117
$errorMessage = $formatter->getErrorMessage();
118
Yii::warning("Formatting message for language '$language' failed with error: $errorMessage. The message being formatted was: $message.", __METHOD__);
127
foreach ($params as $name => $value) {
128
$p['{' . $name . '}'] = $value;
131
return strtr($message, $p);
135
* @var string|array|MessageFormatter
137
private $_messageFormatter;
140
* Returns the message formatter instance.
141
* @return MessageFormatter the message formatter to be used to format message via ICU message format.
143
public function getMessageFormatter()
145
if ($this->_messageFormatter === null) {
146
$this->_messageFormatter = new MessageFormatter();
147
} elseif (is_array($this->_messageFormatter) || is_string($this->_messageFormatter)) {
148
$this->_messageFormatter = Yii::createObject($this->_messageFormatter);
151
return $this->_messageFormatter;
155
* @param string|array|MessageFormatter $value the message formatter to be used to format message via ICU message format.
156
* Can be given as array or string configuration that will be given to [[Yii::createObject]] to create an instance
157
* or a [[MessageFormatter]] instance.
159
public function setMessageFormatter($value)
161
$this->_messageFormatter = $value;
165
* Returns the message source for the given category.
166
* @param string $category the category name.
167
* @return MessageSource the message source for the given category.
168
* @throws InvalidConfigException if there is no message source available for the specified category.
170
public function getMessageSource($category)
172
if (isset($this->translations[$category])) {
173
$source = $this->translations[$category];
174
if ($source instanceof MessageSource) {
178
return $this->translations[$category] = Yii::createObject($source);
180
// try wildcard matching
181
foreach ($this->translations as $pattern => $source) {
182
if (strpos($pattern, '*') > 0 && strpos($category, rtrim($pattern, '*')) === 0) {
183
if ($source instanceof MessageSource) {
187
return $this->translations[$category] = $this->translations[$pattern] = Yii::createObject($source);
191
// match '*' in the last
192
if (isset($this->translations['*'])) {
193
$source = $this->translations['*'];
194
if ($source instanceof MessageSource) {
198
return $this->translations[$category] = $this->translations['*'] = Yii::createObject($source);
201
throw new InvalidConfigException("Unable to locate message source for category '$category'.");