MessageSource.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <[email protected]>
  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. * Child classes should override this method to return the message translations of
  50. * the specified language and category.
  51. * @param string $category the message category
  52. * @param string $language the target language
  53. * @return array the loaded messages. The keys are original messages, and the values
  54. * are translated messages.
  55. */
  56. protected function loadMessages($category, $language)
  57. {
  58. return [];
  59. }
  60. /**
  61. * Translates a message to the specified language.
  62. *
  63. * Note that unless [[forceTranslation]] is true, if the target language
  64. * is the same as the [[sourceLanguage|source language]], the message
  65. * will NOT be translated.
  66. *
  67. * If a translation is not found, a [[missingTranslation]] event will be triggered.
  68. *
  69. * @param string $category the message category
  70. * @param string $message the message to be translated
  71. * @param string $language the target language
  72. * @return string the translated message (or the original message if translation is not needed)
  73. */
  74. public function translate($category, $message, $language)
  75. {
  76. if ($this->forceTranslation || $language !== $this->sourceLanguage) {
  77. return $this->translateMessage($category, $message, $language);
  78. } else {
  79. return $message;
  80. }
  81. }
  82. /**
  83. * Translates the specified message.
  84. * If the message is not found, a [[missingTranslation]] event will be triggered
  85. * and the original message will be returned.
  86. * @param string $category the category that the message belongs to
  87. * @param string $message the message to be translated
  88. * @param string $language the target language
  89. * @return string the translated message
  90. */
  91. protected function translateMessage($category, $message, $language)
  92. {
  93. $key = $language . '/' . $category;
  94. if (!isset($this->_messages[$key])) {
  95. $this->_messages[$key] = $this->loadMessages($category, $language);
  96. }
  97. if (isset($this->_messages[$key][$message]) && $this->_messages[$key][$message] !== '') {
  98. return $this->_messages[$key][$message];
  99. } elseif ($this->hasEventHandlers(self::EVENT_MISSING_TRANSLATION)) {
  100. $event = new MissingTranslationEvent([
  101. 'category' => $category,
  102. 'message' => $message,
  103. 'language' => $language,
  104. ]);
  105. $this->trigger(self::EVENT_MISSING_TRANSLATION, $event);
  106. return $this->_messages[$key] = $event->message;
  107. } else {
  108. return $message;
  109. }
  110. }
  111. }