1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Translator\Config; |
6
|
|
|
|
7
|
|
|
use Spiral\Core\InjectableConfig; |
8
|
|
|
use Spiral\Translator\Matcher; |
9
|
|
|
use Symfony\Component\Translation\Dumper\DumperInterface; |
10
|
|
|
use Symfony\Component\Translation\Loader\LoaderInterface; |
11
|
|
|
|
12
|
|
|
final class TranslatorConfig extends InjectableConfig |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Configuration section. |
16
|
|
|
*/ |
17
|
|
|
public const CONFIG = 'translator'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array{ |
|
|
|
|
21
|
|
|
* locale: string, |
22
|
|
|
* fallbackLocale?: string, |
23
|
|
|
* directory: non-empty-string, |
24
|
|
|
* directories: array<array-key, non-empty-string>, |
25
|
|
|
* localesDirectory?: non-empty-string, |
26
|
|
|
* registerMessages?: bool, |
27
|
|
|
* cacheLocales: bool, |
28
|
|
|
* autoRegister: bool, |
29
|
|
|
* domains: array<non-empty-string, array<string>>, |
30
|
|
|
* loaders: class-string<LoaderInterface>[], |
31
|
|
|
* dumpers: class-string<DumperInterface>[] |
32
|
|
|
* } |
33
|
|
|
*/ |
34
|
|
|
protected array $config = [ |
35
|
|
|
'locale' => '', |
36
|
|
|
'directory' => '', |
37
|
|
|
'directories' => [], |
38
|
|
|
'cacheLocales' => true, |
39
|
|
|
'autoRegister' => true, |
40
|
|
|
'domains' => [], |
41
|
|
|
'loaders' => [], |
42
|
|
|
'dumpers' => [], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
private readonly Matcher $matcher; |
46
|
|
|
|
47
|
78 |
|
public function __construct(array $config = []) |
48
|
|
|
{ |
49
|
78 |
|
parent::__construct($config); |
50
|
78 |
|
$this->matcher = new Matcher(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Default translation domain. |
55
|
|
|
*/ |
56
|
23 |
|
public function getDefaultDomain(): string |
57
|
|
|
{ |
58
|
23 |
|
return 'messages'; |
59
|
|
|
} |
60
|
|
|
|
61
|
47 |
|
public function getDefaultLocale(): string |
62
|
|
|
{ |
63
|
47 |
|
return $this->config['locale'] ?? ''; |
64
|
|
|
} |
65
|
|
|
|
66
|
19 |
|
public function getFallbackLocale(): string |
67
|
|
|
{ |
68
|
19 |
|
return $this->config['fallbackLocale'] ?? $this->getDefaultLocale(); |
69
|
|
|
} |
70
|
|
|
|
71
|
17 |
|
public function isAutoRegisterMessages(): bool |
72
|
|
|
{ |
73
|
17 |
|
return !empty($this->config['autoRegister']) || !empty($this->config['registerMessages']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns application locales directory. |
78
|
|
|
* |
79
|
|
|
* @return non-empty-string |
|
|
|
|
80
|
|
|
*/ |
81
|
51 |
|
public function getLocalesDirectory(): string |
82
|
|
|
{ |
83
|
51 |
|
return $this->config['localesDirectory'] ?? $this->config['directory'] ?? ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns additional locales directories. |
88
|
|
|
* |
89
|
|
|
* @return array<array-key, non-empty-string> |
|
|
|
|
90
|
|
|
*/ |
91
|
48 |
|
public function getDirectories(): array |
92
|
|
|
{ |
93
|
48 |
|
return $this->config['directories'] ?? []; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param non-empty-string $locale |
|
|
|
|
98
|
|
|
* @param non-empty-string|null $directory |
99
|
|
|
* |
100
|
|
|
* @return non-empty-string |
|
|
|
|
101
|
|
|
*/ |
102
|
48 |
|
public function getLocaleDirectory(string $locale, ?string $directory = null): string |
103
|
|
|
{ |
104
|
48 |
|
if ($directory !== null) { |
105
|
45 |
|
return \rtrim($directory, '/') . '/' . $locale . '/'; |
106
|
|
|
} |
107
|
|
|
|
108
|
14 |
|
return \trim($this->getLocalesDirectory(), '/') . '/' . $locale . '/'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get domain name associated with given bundle. |
113
|
|
|
*/ |
114
|
30 |
|
public function resolveDomain(string $bundle): string |
115
|
|
|
{ |
116
|
30 |
|
$bundle = \strtolower(\str_replace(['/', '\\'], '-', $bundle)); |
117
|
30 |
|
$domains = (array) ($this->config['domains'] ?? []); |
118
|
|
|
|
119
|
30 |
|
foreach ($domains as $domain => $patterns) { |
120
|
30 |
|
foreach ($patterns as $pattern) { |
121
|
30 |
|
if ($this->matcher->matches($bundle, $pattern)) { |
122
|
29 |
|
return $domain; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//We can use bundle itself as domain |
128
|
1 |
|
return $bundle; |
129
|
|
|
} |
130
|
|
|
|
131
|
41 |
|
public function hasLoader(string $extension): bool |
132
|
|
|
{ |
133
|
41 |
|
return isset($this->config['loaders'][$extension]); |
134
|
|
|
} |
135
|
|
|
|
136
|
41 |
|
public function getLoader(string $extension): LoaderInterface |
137
|
|
|
{ |
138
|
41 |
|
$class = $this->config['loaders'][$extension]; |
139
|
|
|
|
140
|
41 |
|
return new $class(); |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
public function hasDumper(string $dumper): bool |
144
|
|
|
{ |
145
|
2 |
|
return isset($this->config['dumpers'][$dumper]); |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
public function getDumper(string $dumper): DumperInterface |
149
|
|
|
{ |
150
|
2 |
|
$class = $this->config['dumpers'][$dumper]; |
151
|
|
|
|
152
|
2 |
|
return new $class(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|