-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslatorServiceProvider.php
427 lines (379 loc) · 14.5 KB
/
TranslatorServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
namespace Charcoal\Translator\ServiceProvider;
// From Pimple
use Pimple\Container;
use Pimple\ServiceProviderInterface;
// From 'symfony/translation'
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Loader\CsvFileLoader;
use Symfony\Component\Translation\Loader\IcuDatFileLoader;
use Symfony\Component\Translation\Loader\IcuResFileLoader;
use Symfony\Component\Translation\Loader\IniFileLoader;
use Symfony\Component\Translation\Loader\MoFileLoader;
use Symfony\Component\Translation\Loader\PhpFileLoader;
use Symfony\Component\Translation\Loader\PoFileLoader;
use Symfony\Component\Translation\Loader\QtFileLoader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Loader\JsonFileLoader;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\MessageSelector;
// From 'charcoal-translator'
use Charcoal\Translator\LocalesConfig;
use Charcoal\Translator\LocalesManager;
use Charcoal\Translator\Translator;
use Charcoal\Translator\TranslatorConfig;
use Charcoal\Translator\Middleware\LanguageMiddleware;
/**
* Translation Service Provider
*
* Provides a service for translating your application into different languages,
* and manage the target locale of a Charcoal application.
*/
class TranslatorServiceProvider implements ServiceProviderInterface
{
/**
* @param Container $container Pimple DI container.
* @return void
*/
public function register(Container $container)
{
$this->registerLocales($container);
$this->registerTranslator($container);
$this->registerMiddleware($container);
}
/**
* @param Container $container Pimple DI container.
* @return void
*/
private function registerLocales(Container $container)
{
/**
* Instance of the Locales Configset.
*
* @param Container $container Pimple DI container.
* @return LocalesConfig
*/
$container['locales/config'] = function (Container $container) {
$appConfig = isset($container['config']) ? $container['config'] : [];
$localesConfig = isset($appConfig['locales']) ? $appConfig['locales'] : null;
return new LocalesConfig($localesConfig);
};
/**
* Default language of the application, optionally the navigator's preferred language.
*
* @param Container $container Pimple DI container.
* @return string|null
*/
$container['locales/default-language'] = function (Container $container) {
$localesConfig = $container['locales/config'];
if (isset($localesConfig['auto_detect']) && $localesConfig['auto_detect']) {
if ($container['locales/browser-language'] !== null) {
return $container['locales/browser-language'];
}
}
return $localesConfig['default_language'];
};
/**
* Accepted language from the navigator.
*
* Example with Accept-Language "zh-Hant-HK, fr-CH, fr;q=0.9, en;q=0.7":
*
* 1. zh-Hant-HK
* 2. fr-CH
* 3. fr
* 4. en
*
* @param Container $container Pimple DI container.
* @return string|null
*/
$container['locales/browser-language'] = function (Container $container) {
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return null;
}
/**
* Using data from configset instead of LocalesManager
* since the latter might need the browser language
* as the default language.
*/
$localesConfig = $container['locales/config'];
$supportedLocales = array_filter($localesConfig['languages'], function ($locale) {
return !(isset($locale['active']) && !$locale['active']);
});
$acceptableLanguages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($acceptableLanguages as $acceptedLang) {
$lang = explode(';', $acceptedLang);
$lang = trim($lang[0]);
if (isset($supportedLocales[$lang])) {
return $lang;
}
}
return null;
};
/**
* List of fallback language codes for the translator.
*
* @todo Use filtered "fallback_languages" from LocalesManager
* @param Container $container Pimple DI container.
* @return string[]
*/
$container['locales/fallback-languages'] = function (Container $container) {
$localesConfig = $container['locales/config'];
return $localesConfig['fallback_languages'];
};
/**
* List of language codes (locale ident) from the available locales.
*
* @param Container $container Pimple DI container.
* @return string[]
*/
$container['locales/available-languages'] = function (Container $container) {
$manager = $container['locales/manager'];
return $manager->availableLocales();
};
/**
* List of available locales (as configuration structures) of the application.
*
* @param Container $container Pimple DI container.
* @return array
*/
$container['locales/languages'] = function (Container $container) {
$manager = $container['locales/manager'];
return $manager->locales();
};
/**
* Instance of the Locales Manager.
*
* @todo Filter "fallback_languages"
* @param Container $container Pimple DI container.
* @return LocalesManager
*/
$container['locales/manager'] = function (Container $container) {
$localesConfig = $container['locales/config'];
return new LocalesManager([
'locales' => $localesConfig['languages'],
'default_language' => $container['locales/default-language'],
]);
};
}
/**
* @param Container $container Pimple DI container.
* @return void
*/
private function registerTranslator(Container $container)
{
/**
* Instance of the Translator Configset.
*
* @param Container $container Pimple DI container.
* @return TranslatorConfig
*/
$container['translator/config'] = function (Container $container) {
$appConfig = isset($container['config']) ? $container['config'] : [];
$transConfig = isset($appConfig['translator']) ? $appConfig['translator'] : null;
if (isset($transConfig['paths'])) {
$transConfig['paths'] = $appConfig->resolveValues($transConfig['paths']);
}
$transConfig = new TranslatorConfig($transConfig);
if (isset($container['module/classes'])) {
$extraPaths = [];
$basePath = $appConfig['base_path'];
$modules = $container['module/classes'];
foreach ($modules as $module) {
if (defined(sprintf('%s::APP_CONFIG', $module))) {
$configPath = ltrim($module::APP_CONFIG, '/');
$configPath = $basePath . DIRECTORY_SEPARATOR . $configPath;
$configData = $appConfig->loadFile($configPath);
if (isset($configData['translator']['paths'])) {
$extraPaths = array_merge(
$extraPaths,
$appConfig->resolveValues($configData['translator']['paths'])
);
}
};
}
if ($extraPaths) {
$transConfig->addPaths($extraPaths);
}
}
return $transConfig;
};
/**
* Dictionary of translations grouped by domain and locale, from translator config.
*
* @param Container $container Pimple DI container.
* @return array
*/
$container['translator/translations'] = function (Container $container) {
$transConfig = $container['translator/config'];
return $transConfig['translations'];
};
/**
* Instance of the Message Selector, that is used to resolve a translation.
*
* @return MessageSelector
*/
$container['translator/message-selector'] = function () {
return new MessageSelector();
};
/**
* Instance of the Message Formatter, that is used to format a localized message.
*
* @param Container $container Pimple DI container.
* @return MessageFormatter
*/
$container['translator/message-formatter'] = function (Container $container) {
return new MessageFormatter($container['translator/message-selector']);
};
/**
* Instance of the Translator, that is used for translation.
*
* @todo Improve file loader with a map of file formats.
* @param Container $container Pimple DI container.
* @return Translator
*/
$container['translator'] = function (Container $container) {
$transConfig = $container['translator/config'];
$translator = new Translator([
'manager' => $container['locales/manager'],
'message_selector' => $container['translator/message-selector'],
'message_formatter' => $container['translator/message-formatter'],
'cache_dir' => $transConfig['cache_dir'],
'debug' => $transConfig['debug'],
]);
$translator->setFallbackLocales($container['locales/fallback-languages']);
$translator->addLoader('array', $container['translator/loader/array']);
foreach ($transConfig['loaders'] as $loader) {
$translator->addLoader($loader, $container['translator/loader/file/' . $loader]);
$paths = array_reverse($transConfig['paths']);
foreach ($paths as $path) {
$path = realpath($container['config']['base_path'] . DIRECTORY_SEPARATOR . $path);
if ($path === false) {
continue;
}
$files = glob($path . '/*.' . $loader);
foreach ($files as $f) {
$names = explode('.', basename($f));
if (count($names) < 3) {
continue;
}
$lang = $names[1];
$domain = $names[0];
$translator->addResource($loader, $f, $lang, $domain);
}
}
}
foreach ($container['translator/translations'] as $domain => $data) {
foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain);
}
}
return $translator;
};
$this->registerTranslatorLoaders($container);
}
/**
* @param Container $container Pimple DI container.
* @return void
*/
private function registerTranslatorLoaders(Container $container)
{
/**
* @return ArrayLoader
*/
$container['translator/loader/array'] = function () {
return new ArrayLoader();
};
/**
* @return CsvFileLoader
*/
$container['translator/loader/file/csv'] = function () {
return new CsvFileLoader();
};
/**
* @return IcuDatFileLoader
*/
$container['translator/loader/file/dat'] = function () {
return new IcuDatFileLoader();
};
/**
* @return IcuResFileLoader
*/
$container['translator/loader/file/res'] = function () {
return new IcuResFileLoader();
};
/**
* @return IniFileLoader
*/
$container['translator/loader/file/ini'] = function () {
return new IniFileLoader();
};
/**
* @return JsonFileLoader
*/
$container['translator/loader/file/json'] = function () {
return new JsonFileLoader();
};
/**
* @return MoFileLoader
*/
$container['translator/loader/file/mo'] = function () {
return new MoFileLoader();
};
/**
* @return PhpFileLoader
*/
$container['translator/loader/file/php'] = function () {
return new PhpFileLoader();
};
/**
* @return PoFileLoader
*/
$container['translator/loader/file/po'] = function () {
return new PoFileLoader();
};
/**
* @return QtFileLoader
*/
$container['translator/loader/file/qt'] = function () {
return new QtFileLoader();
};
/**
* @return XliffFileLoader
*/
$container['translator/loader/file/xliff'] = function () {
return new XliffFileLoader();
};
/**
* @return YamlFileLoader
*/
$container['translator/loader/file/yaml'] = function () {
return new YamlFileLoader();
};
}
/**
* @param Container $container Pimple DI container.
* @return void
*/
private function registerMiddleware(Container $container)
{
/**
* @param Container $container
* @return LanguageMiddleware
*/
$container['middlewares/charcoal/translator/middleware/language'] = function (Container $container) {
$middlewareConfig = $container['config']['middlewares']['charcoal/translator/middleware/language'];
$middlewareConfig = array_replace(
[
'default_language' => $container['translator']->getLocale(),
],
$middlewareConfig,
[
'translator' => $container['translator'],
'browser_language' => $container['locales/browser-language'],
]
);
return new LanguageMiddleware($middlewareConfig);
};
}
}