|
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
|
154 |
|
* @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
|
154 |
|
* @see loadFallbackMessages |
|
67
|
154 |
|
* @see sourceLanguage |
|
68
|
|
|
*/ |
|
69
|
154 |
|
protected function loadMessages($category, $language) |
|
70
|
154 |
|
{ |
|
71
|
151 |
|
$messageFile = $this->getMessageFilePath($category, $language); |
|
72
|
151 |
|
$messages = $this->loadMessagesFromFile($messageFile); |
|
73
|
|
|
|
|
74
|
151 |
|
$fallbackLanguage = substr($language, 0, 2); |
|
75
|
4 |
|
$fallbackSourceLanguage = substr($this->sourceLanguage, 0, 2); |
|
76
|
151 |
|
|
|
77
|
148 |
|
if ($language !== $fallbackLanguage) { |
|
78
|
5 |
|
$messages = $this->loadFallbackMessages($category, $fallbackLanguage, $messages, $messageFile); |
|
|
|
|
|
|
79
|
5 |
|
} elseif ($language === $fallbackSourceLanguage) { |
|
80
|
5 |
|
$messages = $this->loadFallbackMessages($category, $this->sourceLanguage, $messages, $messageFile); |
|
|
|
|
|
|
81
|
5 |
|
} else { |
|
82
|
5 |
|
if ($messages === null) { |
|
83
|
5 |
|
Yii::error("The message file for category '$category' does not exist: $messageFile", __METHOD__); |
|
84
|
5 |
|
} |
|
85
|
8 |
|
} |
|
86
|
5 |
|
|
|
87
|
4 |
|
return (array) $messages; |
|
88
|
4 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
11 |
|
* 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
|
154 |
|
* @since 2.0.7 |
|
102
|
|
|
*/ |
|
103
|
154 |
|
protected function loadFallbackMessages($category, $fallbackLanguage, $messages, $originalMessageFile) |
|
104
|
154 |
|
{ |
|
105
|
1 |
|
$fallbackMessageFile = $this->getMessageFilePath($category, $fallbackLanguage); |
|
106
|
1 |
|
$fallbackMessages = $this->loadMessagesFromFile($fallbackMessageFile); |
|
107
|
154 |
|
|
|
108
|
|
|
if ($messages === null && $fallbackMessages === null && $fallbackLanguage !== $this->sourceLanguage) { |
|
109
|
|
|
Yii::error("The message file for category '$category' does not exist: $originalMessageFile " |
|
110
|
154 |
|
. "Fallback file does not exist as well: $fallbackMessageFile", __METHOD__); |
|
111
|
|
|
} elseif (empty($messages)) { |
|
112
|
|
|
return $fallbackMessages; |
|
113
|
|
|
} elseif (!empty($fallbackMessages)) { |
|
114
|
|
|
foreach ($fallbackMessages as $key => $value) { |
|
115
|
|
|
if (!empty($value) && empty($messages[$key])) { |
|
116
|
|
|
$messages[$key] = $fallbackMessages[$key]; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
154 |
|
} |
|
120
|
|
|
|
|
121
|
154 |
|
return (array) $messages; |
|
122
|
152 |
|
} |
|
123
|
152 |
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns message file path for the specified language and category. |
|
126
|
|
|
* |
|
127
|
152 |
|
* @param string $category the message category |
|
128
|
|
|
* @param string $language the target language |
|
129
|
150 |
|
* @return string path to message file |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getMessageFilePath($category, $language) |
|
132
|
|
|
{ |
|
133
|
|
|
$messageFile = Yii::getAlias($this->basePath) . "/$language/"; |
|
134
|
|
|
if (isset($this->fileMap[$category])) { |
|
135
|
|
|
$messageFile .= $this->fileMap[$category]; |
|
136
|
|
|
} else { |
|
137
|
|
|
$messageFile .= str_replace('\\', '/', $category) . '.php'; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
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
|
|
|
protected function loadMessagesFromFile($messageFile) |
|
150
|
|
|
{ |
|
151
|
|
|
if (is_file($messageFile)) { |
|
152
|
|
|
$messages = include($messageFile); |
|
153
|
|
|
if (!is_array($messages)) { |
|
154
|
|
|
$messages = []; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $messages; |
|
158
|
|
|
} else { |
|
159
|
|
|
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.