Commit a43f8b23 by Qiang Xue

Fixes #2157: The '*' category pattern will match all categories that do not…

Fixes #2157: The '*' category pattern will match all categories that do not match any other patterns listed in `I18N::translations`
parent 1c8ace46
...@@ -120,6 +120,7 @@ Yii Framework 2 Change Log ...@@ -120,6 +120,7 @@ Yii Framework 2 Change Log
- Chg #2025: Removed ability to declare scopes in ActiveRecord (samdark) - Chg #2025: Removed ability to declare scopes in ActiveRecord (samdark)
- Chg #2057: AutoTimestamp attributes defaults changed from `create_time` and `update_time` to `created_at` and `updated_at` (creocoder) - Chg #2057: AutoTimestamp attributes defaults changed from `create_time` and `update_time` to `created_at` and `updated_at` (creocoder)
- Chg #2063: Removed `yii\web\Request::acceptTypes` and renamed `yii\web\Request::acceptedContentTypes` to `acceptableContentTypes` (qiangxue) - Chg #2063: Removed `yii\web\Request::acceptTypes` and renamed `yii\web\Request::acceptedContentTypes` to `acceptableContentTypes` (qiangxue)
- Chg #2157: The '*' category pattern will match all categories that do not match any other patterns listed in `I18N::translations` (qiangxue)
- Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue) - Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue)
- Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue) - Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue)
- Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue) - Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue)
......
...@@ -29,9 +29,13 @@ class I18N extends Component ...@@ -29,9 +29,13 @@ class I18N extends Component
{ {
/** /**
* @var array list of [[MessageSource]] configurations or objects. The array keys are message * @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 * category patterns, 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 * for creating the [[MessageSource]] objects.
* to match multiple categories with the same prefix. For example, 'app\*' matches both 'app\cat1' and 'app\cat2'. *
* The message category patterns 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'.
*
* The '*' category pattern will match all categories that do not match any other category patterns.
* *
* This property may be modified on the fly by extensions who want to have their own message sources * This property may be modified on the fly by extensions who want to have their own message sources
* registered under their own namespaces. * registered under their own namespaces.
...@@ -169,15 +173,24 @@ class I18N extends Component ...@@ -169,15 +173,24 @@ class I18N extends Component
} }
} else { } else {
// try wildcard matching // try wildcard matching
foreach ($this->translations as $pattern => $config) { foreach ($this->translations as $pattern => $source) {
if ($pattern === '*' || substr($pattern, -1) === '*' && strpos($category, rtrim($pattern, '*')) === 0) { if (strpos($pattern, '*') > 0 && strpos($category, rtrim($pattern, '*')) === 0) {
if ($config instanceof MessageSource) { if ($source instanceof MessageSource) {
return $config; return $source;
} else { } else {
return $this->translations[$category] = $this->translations[$pattern] = Yii::createObject($config); return $this->translations[$category] = $this->translations[$pattern] = Yii::createObject($source);
} }
} }
} }
// match '*' in the last
if (isset($this->translations['*'])) {
$source = $this->translations['*'];
if ($source instanceof MessageSource) {
return $source;
} else {
return $this->translations[$category] = $this->translations['*'] = Yii::createObject($source);
}
}
} }
throw new InvalidConfigException("Unable to locate message source for category '$category'."); throw new InvalidConfigException("Unable to locate message source for category '$category'.");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment