|
- <?php
-
-
- namespace yii\i18n;
-
- use Yii;
- use yii\base\Component;
- use yii\base\InvalidConfigException;
-
-
- class I18N extends Component
- {
-
-
- public $translations;
-
-
-
-
- public function init()
- {
- parent::init();
- if (!isset($this->translations['yii']) && !isset($this->translations['yii*'])) {
- $this->translations['yii'] = [
- 'class' => 'yii\i18n\PhpMessageSource',
- 'sourceLanguage' => 'en-US',
- 'basePath' => '@yii/messages',
- ];
- }
- if (!isset($this->translations['app']) && !isset($this->translations['app*'])) {
- $this->translations['app'] = [
- 'class' => 'yii\i18n\PhpMessageSource',
- 'sourceLanguage' => Yii::$app->sourceLanguage,
- 'basePath' => '@app/messages',
- ];
- }
- }
-
-
-
- public function translate($category, $message, $params, $language)
- {
- $messageSource = $this->getMessageSource($category);
- $translation = $messageSource->translate($category, $message, $language);
- if ($translation === false) {
- return $this->format($message, $params, $messageSource->sourceLanguage);
- } else {
- return $this->format($translation, $params, $language);
- }
- }
-
-
-
- public function format($message, $params, $language)
- {
- $params = (array) $params;
- if ($params === []) {
- return $message;
- }
-
- if (preg_match('~{\s*[\d\w]+\s*,~u', $message)) {
- $formatter = $this->getMessageFormatter();
- $result = $formatter->format($message, $params, $language);
- if ($result === false) {
- $errorMessage = $formatter->getErrorMessage();
- Yii::warning("Formatting message for language '$language' failed with error: $errorMessage. The message being formatted was: $message.", __METHOD__);
-
- return $message;
- } else {
- return $result;
- }
- }
-
- $p = [];
- foreach ($params as $name => $value) {
- $p['{' . $name . '}'] = $value;
- }
-
- return strtr($message, $p);
- }
-
-
-
- private $_messageFormatter;
-
-
-
- public function getMessageFormatter()
- {
- if ($this->_messageFormatter === null) {
- $this->_messageFormatter = new MessageFormatter();
- } elseif (is_array($this->_messageFormatter) || is_string($this->_messageFormatter)) {
- $this->_messageFormatter = Yii::createObject($this->_messageFormatter);
- }
-
- return $this->_messageFormatter;
- }
-
-
-
- public function setMessageFormatter($value)
- {
- $this->_messageFormatter = $value;
- }
-
-
-
- public function getMessageSource($category)
- {
- if (isset($this->translations[$category])) {
- $source = $this->translations[$category];
- if ($source instanceof MessageSource) {
- return $source;
- } else {
- return $this->translations[$category] = Yii::createObject($source);
- }
- } else {
-
- foreach ($this->translations as $pattern => $source) {
- if (strpos($pattern, '*') > 0 && strpos($category, rtrim($pattern, '*')) === 0) {
- if ($source instanceof MessageSource) {
- return $source;
- } else {
- return $this->translations[$category] = $this->translations[$pattern] = Yii::createObject($source);
- }
- }
- }
-
- 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'.");
- }
- }
|