mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 06:48:59 +08:00
refactoring I18N.
This commit is contained in:
@@ -45,14 +45,14 @@ class I18N extends Component
|
|||||||
parent::init();
|
parent::init();
|
||||||
if (!isset($this->translations['yii'])) {
|
if (!isset($this->translations['yii'])) {
|
||||||
$this->translations['yii'] = [
|
$this->translations['yii'] = [
|
||||||
'class' => 'yii\i18n\PhpMessageSource',
|
'class' => PhpMessageSource::className(),
|
||||||
'sourceLanguage' => 'en_US',
|
'sourceLanguage' => 'en_US',
|
||||||
'basePath' => '@yii/messages',
|
'basePath' => '@yii/messages',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
if (!isset($this->translations['app'])) {
|
if (!isset($this->translations['app'])) {
|
||||||
$this->translations['app'] = [
|
$this->translations['app'] = [
|
||||||
'class' => 'yii\i18n\PhpMessageSource',
|
'class' => PhpMessageSource::className(),
|
||||||
'sourceLanguage' => 'en_US',
|
'sourceLanguage' => 'en_US',
|
||||||
'basePath' => '@app/messages',
|
'basePath' => '@app/messages',
|
||||||
];
|
];
|
||||||
@@ -74,33 +74,7 @@ class I18N extends Component
|
|||||||
public function translate($category, $message, $params, $language)
|
public function translate($category, $message, $params, $language)
|
||||||
{
|
{
|
||||||
$message = $this->getMessageSource($category)->translate($category, $message, $language);
|
$message = $this->getMessageSource($category)->translate($category, $message, $language);
|
||||||
|
return $this->format($message, $params, $language);
|
||||||
$params = (array)$params;
|
|
||||||
if ($params === []) {
|
|
||||||
return $message;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preg_match('~{\s*[\d\w]+\s*,~u', $message)) {
|
|
||||||
$formatter = new MessageFormatter($language, $message);
|
|
||||||
if ($formatter === null) {
|
|
||||||
Yii::warning("$language message from category $category is invalid. Message is: $message.");
|
|
||||||
return $message;
|
|
||||||
}
|
|
||||||
$result = $formatter->format($params);
|
|
||||||
if ($result === false) {
|
|
||||||
$errorMessage = $formatter->getErrorMessage();
|
|
||||||
Yii::warning("$language message from category $category failed with error: $errorMessage. Message is: $message.");
|
|
||||||
return $message;
|
|
||||||
} else {
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$p = [];
|
|
||||||
foreach($params as $name => $value) {
|
|
||||||
$p['{' . $name . '}'] = $value;
|
|
||||||
}
|
|
||||||
return strtr($message, $p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,13 +95,13 @@ class I18N extends Component
|
|||||||
if (preg_match('~{\s*[\d\w]+\s*,~u', $message)) {
|
if (preg_match('~{\s*[\d\w]+\s*,~u', $message)) {
|
||||||
$formatter = new MessageFormatter($language, $message);
|
$formatter = new MessageFormatter($language, $message);
|
||||||
if ($formatter === null) {
|
if ($formatter === null) {
|
||||||
Yii::warning("Message for $language is invalid: $message.");
|
Yii::warning("Unable to format message in language '$language': $message.");
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
$result = $formatter->format($params);
|
$result = $formatter->format($params);
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
$errorMessage = $formatter->getErrorMessage();
|
$errorMessage = $formatter->getErrorMessage();
|
||||||
Yii::warning("Formatting message for $language failed with error: $errorMessage. Message is: $message.");
|
Yii::warning("Formatting message for language '$language' failed with error: $errorMessage. The message being formatted was: $message.");
|
||||||
return $message;
|
return $message;
|
||||||
} else {
|
} else {
|
||||||
return $result;
|
return $result;
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ if (!class_exists('MessageFormatter', false)) {
|
|||||||
defined('YII_INTL_MESSAGE_FALLBACK') || define('YII_INTL_MESSAGE_FALLBACK', false);
|
defined('YII_INTL_MESSAGE_FALLBACK') || define('YII_INTL_MESSAGE_FALLBACK', false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MessageFormatter is an enhanced version of PHP intl class that no matter which PHP and ICU versions are used:
|
* MessageFormatter enhances the message formatter class provided by PHP intl extension.
|
||||||
|
*
|
||||||
|
* The following enhancements are provided:
|
||||||
*
|
*
|
||||||
* - Accepts named arguments and mixed numeric and named arguments.
|
* - Accepts named arguments and mixed numeric and named arguments.
|
||||||
* - Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be
|
* - Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be
|
||||||
|
|||||||
Reference in New Issue
Block a user