MessageSource.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 bool 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|bool 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. }
  80. return false;
  81. }
  82. /**
  83. * Translates the specified message.
  84. * If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
  85. * If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]].
  86. * If no fallback translation is provided this method will return `false`.
  87. * @param string $category the category that the message belongs to.
  88. * @param string $message the message to be translated.
  89. * @param string $language the target language.
  90. * @return string|bool the translated message or false if translation wasn't found.
  91. */
  92. protected function translateMessage($category, $message, $language)
  93. {
  94. $key = $language . '/' . $category;
  95. if (!isset($this->_messages[$key])) {
  96. $this->_messages[$key] = $this->loadMessages($category, $language);
  97. }
  98. if (isset($this->_messages[$key][$message]) && $this->_messages[$key][$message] !== '') {
  99. return $this->_messages[$key][$message];
  100. } elseif ($this->hasEventHandlers(self::EVENT_MISSING_TRANSLATION)) {
  101. $event = new MissingTranslationEvent([
  102. 'category' => $category,
  103. 'message' => $message,
  104. 'language' => $language,
  105. ]);
  106. $this->trigger(self::EVENT_MISSING_TRANSLATION, $event);
  107. if ($event->translatedMessage !== null) {
  108. return $this->_messages[$key][$message] = $event->translatedMessage;
  109. }
  110. }
  111. return $this->_messages[$key][$message] = false;
  112. }
  113. }