1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Common\Storage; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
15
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
16
|
|
|
use Symfony\Component\Translation\Reader\TranslationReaderInterface; |
17
|
|
|
use Symfony\Component\Translation\Writer\TranslationWriterInterface; |
18
|
|
|
use Translation\Common\Model\Message; |
19
|
|
|
use Translation\Common\Model\MessageInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This storage uses Symfony's writer and loader. |
23
|
|
|
* |
24
|
|
|
* @author Tobias Nyholm <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class FileStorage implements StorageInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var TranslationWriterInterface |
30
|
|
|
*/ |
31
|
|
|
private $writer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TranslationReaderInterface |
35
|
|
|
*/ |
36
|
|
|
private $reader; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array directory path |
40
|
|
|
*/ |
41
|
|
|
private $dir; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array with option to the dumper |
45
|
|
|
*/ |
46
|
|
|
private $options; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var MessageCatalogue[] Fetched catalogues |
50
|
|
|
*/ |
51
|
|
|
private $catalogues; |
52
|
|
|
|
53
|
9 |
|
public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, array $dir, array $options = []) |
54
|
|
|
{ |
55
|
9 |
|
if (empty($dir)) { |
56
|
1 |
|
throw new \LogicException('Third parameter of FileStorage cannot be empty'); |
57
|
|
|
} |
58
|
|
|
|
59
|
8 |
|
if (!\array_key_exists('xliff_version', $options)) { |
60
|
|
|
// Set default value for xliff version. |
61
|
8 |
|
$options['xliff_version'] = '2.0'; |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
$this->writer = $writer; |
65
|
8 |
|
$this->reader = $reader; |
66
|
8 |
|
$this->dir = $dir; |
67
|
8 |
|
$this->options = $options; |
68
|
8 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function get(string $locale, string $domain, string $key): ?MessageInterface |
74
|
|
|
{ |
75
|
1 |
|
$catalogue = $this->getCatalogue($locale); |
76
|
1 |
|
$translation = $catalogue->get($key, $domain); |
77
|
|
|
|
78
|
1 |
|
return new Message($key, $domain, $locale, $translation); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
2 |
|
public function create(MessageInterface $m): void |
85
|
|
|
{ |
86
|
2 |
|
$catalogue = $this->getCatalogue($m->getLocale()); |
87
|
2 |
|
if (!$catalogue->defines($m->getKey(), $m->getDomain())) { |
88
|
2 |
|
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
89
|
2 |
|
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
90
|
|
|
} |
91
|
2 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
1 |
|
public function update(MessageInterface $m): void |
97
|
|
|
{ |
98
|
1 |
|
$catalogue = $this->getCatalogue($m->getLocale()); |
99
|
1 |
|
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
100
|
1 |
|
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function delete(string $locale, string $domain, string $key): void |
107
|
|
|
{ |
108
|
1 |
|
$catalogue = $this->getCatalogue($locale); |
109
|
1 |
|
$messages = $catalogue->all($domain); |
110
|
1 |
|
unset($messages[$key]); |
111
|
|
|
|
112
|
1 |
|
$catalogue->replace($messages, $domain); |
113
|
1 |
|
$this->writeCatalogue($catalogue, $locale, $domain); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function export(MessageCatalogueInterface $catalogue, array $options = []): void |
120
|
|
|
{ |
121
|
1 |
|
$locale = $catalogue->getLocale(); |
122
|
1 |
|
$catalogue->addCatalogue($this->getCatalogue($locale)); |
|
|
|
|
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function import(MessageCatalogueInterface $catalogue, array $options = []): void |
129
|
|
|
{ |
130
|
1 |
|
$domains = $catalogue->getDomains(); |
131
|
1 |
|
foreach ($domains as $domain) { |
132
|
1 |
|
$this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain); |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
5 |
|
private function writeCatalogue(MessageCatalogueInterface $catalogue, string $locale, string $domain): void |
137
|
|
|
{ |
138
|
5 |
|
$resources = $catalogue->getResources(); |
139
|
5 |
|
$options = $this->options; |
140
|
5 |
|
$written = false; |
141
|
|
|
// $intlDomainSuffix = '(\\' . MessageCatalogueInterface::INTL_DOMAIN_SUFFIX . ')'; # not available in older Symfony versions |
142
|
5 |
|
$intlDomainSuffix = '(\\'.'+intl-icu'.')'; |
143
|
5 |
|
$searchPatternWithIntl = '|/'.$domain.$intlDomainSuffix.'\.'.$locale.'\.([a-z]+)$|'; |
144
|
5 |
|
$searchPatternWithoutIntl = str_replace($intlDomainSuffix, '', $searchPatternWithIntl); |
145
|
5 |
|
foreach ($resources as $resource) { |
146
|
3 |
|
$path = (string) $resource; |
147
|
3 |
|
if (preg_match($searchPatternWithIntl, $path, $matches)) { |
148
|
|
|
$options['path'] = str_replace($matches[0], '', $path); |
149
|
|
|
$this->writer->write($catalogue, $matches[2], $options); |
|
|
|
|
150
|
|
|
$written = true; |
151
|
3 |
|
} elseif (preg_match($searchPatternWithoutIntl, $path, $matches)) { |
152
|
3 |
|
$options['path'] = str_replace($matches[0], '', $path); |
153
|
3 |
|
$this->writer->write($catalogue, $matches[1], $options); |
|
|
|
|
154
|
3 |
|
$written = true; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
5 |
|
if ($written) { |
159
|
|
|
// We have written the translation to a file. |
160
|
3 |
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
$options['path'] = reset($this->dir); |
164
|
2 |
|
$format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf'; |
165
|
2 |
|
$this->writer->write($catalogue, $format, $options); |
|
|
|
|
166
|
2 |
|
} |
167
|
|
|
|
168
|
6 |
|
private function getCatalogue(string $locale): MessageCatalogue |
169
|
|
|
{ |
170
|
6 |
|
if (empty($this->catalogues[$locale])) { |
171
|
6 |
|
$this->loadCatalogue($locale, $this->dir); |
172
|
|
|
} |
173
|
|
|
|
174
|
6 |
|
return $this->catalogues[$locale]; |
175
|
|
|
} |
176
|
|
|
|
177
|
6 |
|
private function loadCatalogue(string $locale, array $dirs): void |
178
|
|
|
{ |
179
|
6 |
|
$currentCatalogue = new MessageCatalogue($locale); |
180
|
6 |
|
foreach ($dirs as $path) { |
181
|
6 |
|
if (is_dir($path)) { |
182
|
5 |
|
$this->reader->read($path, $currentCatalogue); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
6 |
|
$this->catalogues[$locale] = $currentCatalogue; |
187
|
6 |
|
} |
188
|
|
|
} |
189
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: