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