1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Translator; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
// From 'charcoal-config' |
8
|
|
|
use Charcoal\Config\AbstractConfig; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Translator Configset |
12
|
|
|
* |
13
|
|
|
* Stores the translator's settings, catalogs to be loaded, |
14
|
|
|
* catalog loaders, and extra translations. |
15
|
|
|
*/ |
16
|
|
|
class TranslatorConfig extends AbstractConfig |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Available resource loaders. |
20
|
|
|
* |
21
|
|
|
* @var string[] |
22
|
|
|
*/ |
23
|
|
|
private $loaders; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Translation resource paths. |
27
|
|
|
* |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $paths; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Mapping of domains/locales/messages. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $translations; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Debug mode. |
41
|
|
|
* |
42
|
|
|
* @var boolean |
43
|
|
|
*/ |
44
|
|
|
private $debug; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The directory to use for the cache. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $cacheDir; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function defaults() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
'loaders' => [ |
60
|
|
|
'csv' |
61
|
|
|
], |
62
|
|
|
'paths' => [ |
63
|
|
|
'translations/' |
64
|
|
|
], |
65
|
|
|
'translations' => [], |
66
|
|
|
'debug' => false, |
67
|
|
|
'cache_dir' => '../cache/translator' |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string[] $loaders The list of active loaders. |
73
|
|
|
* @throws InvalidArgumentException If the loader is invalid. |
74
|
|
|
* @return TranslatorConfig Chainable |
75
|
|
|
*/ |
76
|
|
|
public function setLoaders(array $loaders) |
77
|
|
|
{ |
78
|
|
|
$this->loaders = []; |
79
|
|
|
foreach ($loaders as $loader) { |
80
|
|
|
if (!in_array($loader, $this->availableLoaders())) { |
81
|
|
|
throw new InvalidArgumentException(sprintf( |
82
|
|
|
'Loader "%s" is not a valid loader.', |
83
|
|
|
$loader |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
$this->loaders[] = $loader; |
87
|
|
|
} |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string[] |
93
|
|
|
*/ |
94
|
|
|
public function loaders() |
95
|
|
|
{ |
96
|
|
|
return $this->loaders; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string[] $paths The "paths" (search pattern) to look into for translation resources. |
101
|
|
|
* @throws InvalidArgumentException If the path is not a string. |
102
|
|
|
* @return TranslatorConfig Chainable |
103
|
|
|
*/ |
104
|
|
|
public function setPaths(array $paths) |
105
|
|
|
{ |
106
|
|
|
$this->paths = []; |
107
|
|
|
foreach ($paths as $path) { |
108
|
|
|
if (!is_string($path)) { |
109
|
|
|
throw new InvalidArgumentException( |
110
|
|
|
'Translator path must be a string' |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
$this->paths[] = $path; |
114
|
|
|
} |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string[] |
120
|
|
|
*/ |
121
|
|
|
public function paths() |
122
|
|
|
{ |
123
|
|
|
return $this->paths; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set mapping of additional translations. |
128
|
|
|
* |
129
|
|
|
* Expects: |
130
|
|
|
* ```json |
131
|
|
|
* { |
132
|
|
|
* "<domain>": { |
133
|
|
|
* "<locale>": { |
134
|
|
|
* "<translation-key>": "translation" |
135
|
|
|
* } |
136
|
|
|
* } |
137
|
|
|
* } |
138
|
|
|
* ``` |
139
|
|
|
* |
140
|
|
|
* @param array $translations Mapping of domains/locales/messages. |
141
|
|
|
* @throws InvalidArgumentException If the path is not a string. |
142
|
|
|
* @return TranslatorConfig Chainable |
143
|
|
|
*/ |
144
|
|
|
public function setTranslations(array $translations) |
145
|
|
|
{ |
146
|
|
|
$this->translations = []; |
147
|
|
|
foreach ($translations as $domain => $data) { |
148
|
|
|
if (!is_array($data)) { |
149
|
|
|
throw new InvalidArgumentException( |
150
|
|
|
'Translator translations must be a 3-level array' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
foreach ($data as $locale => $messages) { |
155
|
|
|
if (!is_array($messages)) { |
156
|
|
|
throw new InvalidArgumentException( |
157
|
|
|
'Translator translations must be a 3-level array' |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->translations = $translations; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Retrieve mapping of additional translations. |
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
public function translations() |
174
|
|
|
{ |
175
|
|
|
return $this->translations; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param boolean $debug The debug flag. |
180
|
|
|
* @return TranslatorConfig Chainable |
181
|
|
|
*/ |
182
|
|
|
public function setDebug($debug) |
183
|
|
|
{ |
184
|
|
|
$this->debug = !!$debug; |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return boolean |
190
|
|
|
*/ |
191
|
|
|
public function debug() |
192
|
|
|
{ |
193
|
|
|
return $this->debug; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $cacheDir The cache directory. |
198
|
|
|
* @throws InvalidArgumentException If the cache dir argument is not a string. |
199
|
|
|
* @return TranslatorConfig Chainable |
200
|
|
|
*/ |
201
|
|
|
public function setCacheDir($cacheDir) |
202
|
|
|
{ |
203
|
|
|
if (!is_string($cacheDir)) { |
|
|
|
|
204
|
|
|
throw new InvalidArgumentException( |
205
|
|
|
'Cache dir must be a string' |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
$this->cacheDir = $cacheDir; |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function cacheDir() |
216
|
|
|
{ |
217
|
|
|
return $this->cacheDir; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
private function availableLoaders() |
224
|
|
|
{ |
225
|
|
|
return [ |
226
|
|
|
'csv', |
227
|
|
|
'dat', |
228
|
|
|
'res', |
229
|
|
|
'ini', |
230
|
|
|
'json', |
231
|
|
|
'mo', |
232
|
|
|
'php', |
233
|
|
|
'po', |
234
|
|
|
'qt', |
235
|
|
|
'xliff', |
236
|
|
|
'yaml' |
237
|
|
|
]; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|