Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

133 lines
4.4KB

  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. * ~~~
  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. * ~~~
  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`.
  53. *
  54. * @param string $category the message category
  55. * @param string $language the target language
  56. * @return array the loaded messages. The keys are original messages, and the values
  57. * are translated messages.
  58. */
  59. protected function loadMessages($category, $language)
  60. {
  61. $messageFile = $this->getMessageFilePath($category, $language);
  62. $messages = $this->loadMessagesFromFile($messageFile);
  63. $fallbackLanguage = substr($language, 0, 2);
  64. if ($fallbackLanguage != $language) {
  65. $fallbackMessageFile = $this->getMessageFilePath($category, $fallbackLanguage);
  66. $fallbackMessages = $this->loadMessagesFromFile($fallbackMessageFile);
  67. if ($messages === null && $fallbackMessages === null && $fallbackLanguage != $this->sourceLanguage) {
  68. Yii::error("The message file for category '$category' does not exist: $messageFile Fallback file does not exist as well: $fallbackMessageFile", __METHOD__);
  69. } elseif (empty($messages)) {
  70. return $fallbackMessages;
  71. } elseif (!empty($fallbackMessages)) {
  72. foreach ($fallbackMessages as $key => $value) {
  73. if (!empty($value) && empty($messages[$key])) {
  74. $messages[$key] = $fallbackMessages[$key];
  75. }
  76. }
  77. }
  78. } else {
  79. if ($messages === null) {
  80. Yii::error("The message file for category '$category' does not exist: $messageFile", __METHOD__);
  81. }
  82. }
  83. return (array) $messages;
  84. }
  85. /**
  86. * Returns message file path for the specified language and category.
  87. *
  88. * @param string $category the message category
  89. * @param string $language the target language
  90. * @return string path to message file
  91. */
  92. protected function getMessageFilePath($category, $language)
  93. {
  94. $messageFile = Yii::getAlias($this->basePath) . "/$language/";
  95. if (isset($this->fileMap[$category])) {
  96. $messageFile .= $this->fileMap[$category];
  97. } else {
  98. $messageFile .= str_replace('\\', '/', $category) . '.php';
  99. }
  100. return $messageFile;
  101. }
  102. /**
  103. * Loads the message translation for the specified language and category or returns null if file doesn't exist.
  104. *
  105. * @param $messageFile string path to message file
  106. * @return array|null array of messages or null if file not found
  107. */
  108. protected function loadMessagesFromFile($messageFile)
  109. {
  110. if (is_file($messageFile)) {
  111. $messages = include($messageFile);
  112. if (!is_array($messages)) {
  113. $messages = [];
  114. }
  115. return $messages;
  116. } else {
  117. return null;
  118. }
  119. }
  120. }