1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Bootloader; |
6
|
|
|
|
7
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
8
|
|
|
use Spiral\Boot\DirectoriesInterface; |
9
|
|
|
use Spiral\Boot\Environment\DebugMode; |
10
|
|
|
use Spiral\Boot\EnvironmentInterface; |
11
|
|
|
use Spiral\Config\ConfiguratorInterface; |
12
|
|
|
use Spiral\Config\Patch\Append; |
13
|
|
|
use Spiral\Core\Container\SingletonInterface; |
14
|
|
|
use Spiral\Translator\Catalogue\CacheInterface; |
15
|
|
|
use Spiral\Translator\Catalogue\CatalogueLoader; |
16
|
|
|
use Spiral\Translator\Catalogue\CatalogueManager; |
17
|
|
|
use Spiral\Translator\Catalogue\LoaderInterface; |
18
|
|
|
use Spiral\Translator\CatalogueManagerInterface; |
19
|
|
|
use Spiral\Translator\Config\TranslatorConfig; |
20
|
|
|
use Spiral\Translator\MemoryCache; |
21
|
|
|
use Spiral\Translator\Translator; |
22
|
|
|
use Spiral\Translator\TranslatorInterface; |
23
|
|
|
use Symfony\Component\Translation\Dumper; |
24
|
|
|
use Symfony\Component\Translation\IdentityTranslator; |
25
|
|
|
use Symfony\Component\Translation\Loader; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Attention, the default language would not be automatically reset in finalizers. Make sure to properly design your |
29
|
|
|
* middleware. |
30
|
|
|
*/ |
31
|
|
|
final class I18nBootloader extends Bootloader implements SingletonInterface |
32
|
|
|
{ |
33
|
|
|
protected const SINGLETONS = [ |
34
|
|
|
\Symfony\Contracts\Translation\TranslatorInterface::class => TranslatorInterface::class, |
35
|
|
|
TranslatorInterface::class => Translator::class, |
36
|
|
|
CatalogueManagerInterface::class => CatalogueManager::class, |
37
|
|
|
LoaderInterface::class => CatalogueLoader::class, |
38
|
|
|
CacheInterface::class => MemoryCache::class, |
39
|
|
|
IdentityTranslator::class => [self::class, 'identityTranslator'], |
40
|
|
|
]; |
41
|
|
|
|
42
|
306 |
|
public function __construct( |
43
|
|
|
private readonly ConfiguratorInterface $config |
44
|
|
|
) { |
45
|
306 |
|
} |
46
|
|
|
|
47
|
306 |
|
public function init(EnvironmentInterface $env, DirectoriesInterface $dirs, DebugMode $debugMode): void |
48
|
|
|
{ |
49
|
306 |
|
if (!$dirs->has('locale')) { |
50
|
306 |
|
$dirs->set('locale', $dirs->get('app') . 'locale/'); |
51
|
|
|
} |
52
|
|
|
|
53
|
306 |
|
$this->config->setDefaults( |
54
|
306 |
|
TranslatorConfig::CONFIG, |
55
|
306 |
|
[ |
56
|
306 |
|
'locale' => $env->get('LOCALE', 'en'), |
57
|
306 |
|
'fallbackLocale' => $env->get('LOCALE', 'en'), |
58
|
306 |
|
'directory' => $dirs->get('locale'), |
59
|
306 |
|
'directories' => [], |
60
|
306 |
|
'autoRegister' => $debugMode->isEnabled(), |
61
|
306 |
|
'loaders' => [ |
62
|
306 |
|
'php' => Loader\PhpFileLoader::class, |
63
|
306 |
|
'po' => Loader\PoFileLoader::class, |
64
|
306 |
|
'csv' => Loader\CsvFileLoader::class, |
65
|
306 |
|
'json' => Loader\JsonFileLoader::class, |
66
|
306 |
|
], |
67
|
306 |
|
'dumpers' => [ |
68
|
306 |
|
'php' => Dumper\PhpFileDumper::class, |
69
|
306 |
|
'po' => Dumper\PoFileDumper::class, |
70
|
306 |
|
'csv' => Dumper\CsvFileDumper::class, |
71
|
306 |
|
'json' => Dumper\JsonFileDumper::class, |
72
|
306 |
|
], |
73
|
306 |
|
'domains' => [ |
74
|
|
|
// by default we can store all messages in one domain |
75
|
306 |
|
'messages' => ['*'], |
76
|
306 |
|
], |
77
|
306 |
|
] |
78
|
306 |
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param non-empty-string $directory |
|
|
|
|
83
|
|
|
*/ |
84
|
1 |
|
public function addDirectory(string $directory): void |
85
|
|
|
{ |
86
|
1 |
|
$this->config->modify(TranslatorConfig::CONFIG, new Append('directories', null, $directory)); |
87
|
|
|
} |
88
|
|
|
|
89
|
32 |
|
private function identityTranslator(): IdentityTranslator |
90
|
|
|
{ |
91
|
32 |
|
return new IdentityTranslator(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|