PhpMessageSource.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /**
  10. * PhpMessageSource represents a message source that stores translated messages in PHP scripts.
  11. *
  12. * PhpMessageSource uses PHP arrays to keep message translations.
  13. *
  14. * - Each PHP script contains one array which stores the message translations in one particular
  15. * language and for a single message category;
  16. * - Each PHP script is saved as a file named as "[[basePath]]/LanguageID/CategoryName.php";
  17. * - Within each PHP script, the message translations are returned as an array like the following:
  18. *
  19. * ```php
  20. * return [
  21. * 'original message 1' => 'translated message 1',
  22. * 'original message 2' => 'translated message 2',
  23. * ];
  24. * ```
  25. *
  26. * You may use [[fileMap]] to customize the association between category names and the file names.
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @since 2.0
  30. */
  31. class PhpMessageSource extends MessageSource
  32. {
  33. /**
  34. * @var string the base path for all translated messages. Defaults to '@app/messages'.
  35. */
  36. public $basePath = '@app/messages';
  37. /**
  38. * @var array mapping between message categories and the corresponding message file paths.
  39. * The file paths are relative to [[basePath]]. For example,
  40. *
  41. * ```php
  42. * [
  43. * 'core' => 'core.php',
  44. * 'ext' => 'extensions.php',
  45. * ]
  46. * ```
  47. */
  48. public $fileMap;
  49. /**
  50. * Loads the message translation for the specified $language and $category.
  51. * If translation for specific locale code such as `en-US` isn't found it
  52. * tries more generic `en`. When both are present, the `en-US` messages will be merged
  53. * over `en`. See [[loadFallbackMessages]] for details.
  54. * If the $language is less specific than [[sourceLanguage]], the method will try to
  55. * load the messages for [[sourceLanguage]]. For example: [[sourceLanguage]] is `en-GB`,
  56. * $language is `en`. The method will load the messages for `en` and merge them over `en-GB`.
  57. *
  58. * @param string $category the message category
  59. * @param string $language the target language
  60. * @return array the loaded messages. The keys are original messages, and the values are the translated messages.
  61. * @see loadFallbackMessages
  62. * @see sourceLanguage
  63. */
  64. protected function loadMessages($category, $language)
  65. {
  66. $messageFile = $this->getMessageFilePath($category, $language);
  67. $messages = $this->loadMessagesFromFile($messageFile);
  68. $fallbackLanguage = substr($language, 0, 2);
  69. $fallbackSourceLanguage = substr($this->sourceLanguage, 0, 2);
  70. if ($language !== $fallbackLanguage) {
  71. $messages = $this->loadFallbackMessages($category, $fallbackLanguage, $messages, $messageFile);
  72. } elseif ($language === $fallbackSourceLanguage) {
  73. $messages = $this->loadFallbackMessages($category, $this->sourceLanguage, $messages, $messageFile);
  74. } else {
  75. if ($messages === null) {
  76. Yii::warning("The message file for category '$category' does not exist: $messageFile", __METHOD__);
  77. }
  78. }
  79. return (array) $messages;
  80. }
  81. /**
  82. * The method is normally called by [[loadMessages]] to load the fallback messages for the language.
  83. * Method tries to load the $category messages for the $fallbackLanguage and adds them to the $messages array.
  84. *
  85. * @param string $category the message category
  86. * @param string $fallbackLanguage the target fallback language
  87. * @param array $messages the array of previously loaded translation messages.
  88. * The keys are original messages, and the values are the translated messages.
  89. * @param string $originalMessageFile the path to the file with messages. Used to log an error message
  90. * in case when no translations were found.
  91. * @return array the loaded messages. The keys are original messages, and the values are the translated messages.
  92. * @since 2.0.7
  93. */
  94. protected function loadFallbackMessages($category, $fallbackLanguage, $messages, $originalMessageFile)
  95. {
  96. $fallbackMessageFile = $this->getMessageFilePath($category, $fallbackLanguage);
  97. $fallbackMessages = $this->loadMessagesFromFile($fallbackMessageFile);
  98. if (
  99. $messages === null && $fallbackMessages === null
  100. && $fallbackLanguage !== $this->sourceLanguage
  101. && $fallbackLanguage !== substr($this->sourceLanguage, 0, 2)
  102. ) {
  103. Yii::error("The message file for category '$category' does not exist: $originalMessageFile "
  104. . "Fallback file does not exist as well: $fallbackMessageFile", __METHOD__);
  105. } elseif (empty($messages)) {
  106. return $fallbackMessages;
  107. } elseif (!empty($fallbackMessages)) {
  108. foreach ($fallbackMessages as $key => $value) {
  109. if (!empty($value) && empty($messages[$key])) {
  110. $messages[$key] = $fallbackMessages[$key];
  111. }
  112. }
  113. }
  114. return (array) $messages;
  115. }
  116. /**
  117. * Returns message file path for the specified language and category.
  118. *
  119. * @param string $category the message category
  120. * @param string $language the target language
  121. * @return string path to message file
  122. */
  123. protected function getMessageFilePath($category, $language)
  124. {
  125. $messageFile = Yii::getAlias($this->basePath) . "/$language/";
  126. if (isset($this->fileMap[$category])) {
  127. $messageFile .= $this->fileMap[$category];
  128. } else {
  129. $messageFile .= str_replace('\\', '/', $category) . '.php';
  130. }
  131. return $messageFile;
  132. }
  133. /**
  134. * Loads the message translation for the specified language and category or returns null if file doesn't exist.
  135. *
  136. * @param string $messageFile path to message file
  137. * @return array|null array of messages or null if file not found
  138. */
  139. protected function loadMessagesFromFile($messageFile)
  140. {
  141. if (is_file($messageFile)) {
  142. $messages = include $messageFile;
  143. if (!is_array($messages)) {
  144. $messages = [];
  145. }
  146. return $messages;
  147. }
  148. return null;
  149. }
  150. }