您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

126 行
4.5KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\i18n;
  8. use Yii;
  9. use yii\base\Component;
  10. /**
  11. * MessageSource is the base class for message translation repository classes.
  12. *
  13. * A message source stores message translations in some persistent storage.
  14. *
  15. * Child classes should override [[loadMessages()]] to provide translated messages.
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @since 2.0
  19. */
  20. class MessageSource extends Component
  21. {
  22. /**
  23. * @event MissingTranslationEvent an event that is triggered when a message translation is not found.
  24. */
  25. const EVENT_MISSING_TRANSLATION = 'missingTranslation';
  26. /**
  27. * @var boolean whether to force message translation when the source and target languages are the same.
  28. * Defaults to false, meaning translation is only performed when source and target languages are different.
  29. */
  30. public $forceTranslation = false;
  31. /**
  32. * @var string the language that the original messages are in. If not set, it will use the value of
  33. * [[\yii\base\Application::sourceLanguage]].
  34. */
  35. public $sourceLanguage;
  36. private $_messages = [];
  37. /**
  38. * Initializes this component.
  39. */
  40. public function init()
  41. {
  42. parent::init();
  43. if ($this->sourceLanguage === null) {
  44. $this->sourceLanguage = Yii::$app->sourceLanguage;
  45. }
  46. }
  47. /**
  48. * Loads the message translation for the specified language and category.
  49. * If translation for specific locale code such as `en-US` isn't found it
  50. * tries more generic `en`.
  51. *
  52. * @param string $category the message category
  53. * @param string $language the target language
  54. * @return array the loaded messages. The keys are original messages, and the values
  55. * are translated messages.
  56. */
  57. protected function loadMessages($category, $language)
  58. {
  59. return [];
  60. }
  61. /**
  62. * Translates a message to the specified language.
  63. *
  64. * Note that unless [[forceTranslation]] is true, if the target language
  65. * is the same as the [[sourceLanguage|source language]], the message
  66. * will NOT be translated.
  67. *
  68. * If a translation is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
  69. *
  70. * @param string $category the message category
  71. * @param string $message the message to be translated
  72. * @param string $language the target language
  73. * @return string|boolean the translated message or false if translation wasn't found or isn't required
  74. */
  75. public function translate($category, $message, $language)
  76. {
  77. if ($this->forceTranslation || $language !== $this->sourceLanguage) {
  78. return $this->translateMessage($category, $message, $language);
  79. } else {
  80. return false;
  81. }
  82. }
  83. /**
  84. * Translates the specified message.
  85. * If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
  86. * If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]].
  87. * If no fallback translation is provided this method will return `false`.
  88. * @param string $category the category that the message belongs to.
  89. * @param string $message the message to be translated.
  90. * @param string $language the target language.
  91. * @return string|boolean the translated message or false if translation wasn't found.
  92. */
  93. protected function translateMessage($category, $message, $language)
  94. {
  95. $key = $language . '/' . $category;
  96. if (!isset($this->_messages[$key])) {
  97. $this->_messages[$key] = $this->loadMessages($category, $language);
  98. }
  99. if (isset($this->_messages[$key][$message]) && $this->_messages[$key][$message] !== '') {
  100. return $this->_messages[$key][$message];
  101. } elseif ($this->hasEventHandlers(self::EVENT_MISSING_TRANSLATION)) {
  102. $event = new MissingTranslationEvent([
  103. 'category' => $category,
  104. 'message' => $message,
  105. 'language' => $language,
  106. ]);
  107. $this->trigger(self::EVENT_MISSING_TRANSLATION, $event);
  108. if ($event->translatedMessage !== null) {
  109. return $this->_messages[$key][$message] = $event->translatedMessage;
  110. }
  111. }
  112. return $this->_messages[$key][$message] = false;
  113. }
  114. }