GettextMessageSource.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * GettextMessageSource represents a message source that is based on GNU Gettext.
  11. *
  12. * Each GettextMessageSource instance represents the message translations
  13. * for a single domain. And each message category represents a message context
  14. * in Gettext. Translated messages are stored as either a MO or PO file,
  15. * depending on the [[useMoFile]] property value.
  16. *
  17. * All translations are saved under the [[basePath]] directory.
  18. *
  19. * Translations in one language are kept as MO or PO files under an individual
  20. * subdirectory whose name is the language ID. The file name is specified via
  21. * [[catalog]] property, which defaults to 'messages'.
  22. *
  23. * @author Qiang Xue <qiang.xue@gmail.com>
  24. * @since 2.0
  25. */
  26. class GettextMessageSource extends MessageSource
  27. {
  28. const MO_FILE_EXT = '.mo';
  29. const PO_FILE_EXT = '.po';
  30. /**
  31. * @var string
  32. */
  33. public $basePath = '@app/messages';
  34. /**
  35. * @var string
  36. */
  37. public $catalog = 'messages';
  38. /**
  39. * @var boolean
  40. */
  41. public $useMoFile = true;
  42. /**
  43. * @var boolean
  44. */
  45. public $useBigEndian = false;
  46. /**
  47. * Loads the message translation for the specified $language and $category.
  48. * If translation for specific locale code such as `en-US` isn't found it
  49. * tries more generic `en`. When both are present, the `en-US` messages will be merged
  50. * over `en`. See [[loadFallbackMessages]] for details.
  51. * If the $language is less specific than [[sourceLanguage]], the method will try to
  52. * load the messages for [[sourceLanguage]]. For example: [[sourceLanguage]] is `en-GB`,
  53. * $language is `en`. The method will load the messages for `en` and merge them over `en-GB`.
  54. *
  55. * @param string $category the message category
  56. * @param string $language the target language
  57. * @return array the loaded messages. The keys are original messages, and the values are translated messages.
  58. * @see loadFallbackMessages
  59. * @see sourceLanguage
  60. */
  61. protected function loadMessages($category, $language)
  62. {
  63. $messageFile = $this->getMessageFilePath($language);
  64. $messages = $this->loadMessagesFromFile($messageFile, $category);
  65. $fallbackLanguage = substr($language, 0, 2);
  66. $fallbackSourceLanguage = substr($this->sourceLanguage, 0, 2);
  67. if ($fallbackLanguage !== $language) {
  68. $messages = $this->loadFallbackMessages($category, $fallbackLanguage, $messages, $messageFile);
  69. } elseif ($language === $fallbackSourceLanguage) {
  70. $messages = $this->loadFallbackMessages($category, $this->sourceLanguage, $messages, $messageFile);
  71. } else {
  72. if ($messages === null) {
  73. Yii::error("The message file for category '$category' does not exist: $messageFile", __METHOD__);
  74. }
  75. }
  76. return (array) $messages;
  77. }
  78. /**
  79. * The method is normally called by [[loadMessages]] to load the fallback messages for the language.
  80. * Method tries to load the $category messages for the $fallbackLanguage and adds them to the $messages array.
  81. *
  82. * @param string $category the message category
  83. * @param string $fallbackLanguage the target fallback language
  84. * @param array $messages the array of previously loaded translation messages.
  85. * The keys are original messages, and the values are the translated messages.
  86. * @param string $originalMessageFile the path to the file with messages. Used to log an error message
  87. * in case when no translations were found.
  88. * @return array the loaded messages. The keys are original messages, and the values are the translated messages.
  89. * @since 2.0.7
  90. */
  91. protected function loadFallbackMessages($category, $fallbackLanguage, $messages, $originalMessageFile)
  92. {
  93. $fallbackMessageFile = $this->getMessageFilePath($fallbackLanguage);
  94. $fallbackMessages = $this->loadMessagesFromFile($fallbackMessageFile, $category);
  95. if (
  96. $messages === null && $fallbackMessages === null
  97. && $fallbackLanguage !== $this->sourceLanguage
  98. && $fallbackLanguage !== substr($this->sourceLanguage, 0, 2)
  99. ) {
  100. Yii::error("The message file for category '$category' does not exist: $originalMessageFile "
  101. . "Fallback file does not exist as well: $fallbackMessageFile", __METHOD__);
  102. } elseif (empty($messages)) {
  103. return $fallbackMessages;
  104. } elseif (!empty($fallbackMessages)) {
  105. foreach ($fallbackMessages as $key => $value) {
  106. if (!empty($value) && empty($messages[$key])) {
  107. $messages[$key] = $fallbackMessages[$key];
  108. }
  109. }
  110. }
  111. return (array) $messages;
  112. }
  113. /**
  114. * Returns message file path for the specified language and category.
  115. *
  116. * @param string $language the target language
  117. * @return string path to message file
  118. */
  119. protected function getMessageFilePath($language)
  120. {
  121. $messageFile = Yii::getAlias($this->basePath) . '/' . $language . '/' . $this->catalog;
  122. if ($this->useMoFile) {
  123. $messageFile .= self::MO_FILE_EXT;
  124. } else {
  125. $messageFile .= self::PO_FILE_EXT;
  126. }
  127. return $messageFile;
  128. }
  129. /**
  130. * Loads the message translation for the specified language and category or returns null if file doesn't exist.
  131. *
  132. * @param string $messageFile path to message file
  133. * @param string $category the message category
  134. * @return array|null array of messages or null if file not found
  135. */
  136. protected function loadMessagesFromFile($messageFile, $category)
  137. {
  138. if (is_file($messageFile)) {
  139. if ($this->useMoFile) {
  140. $gettextFile = new GettextMoFile(['useBigEndian' => $this->useBigEndian]);
  141. } else {
  142. $gettextFile = new GettextPoFile();
  143. }
  144. $messages = $gettextFile->load($messageFile, $category);
  145. if (!is_array($messages)) {
  146. $messages = [];
  147. }
  148. return $messages;
  149. } else {
  150. return null;
  151. }
  152. }
  153. }