1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Printful\GettextCms; |
4
|
|
|
|
5
|
|
|
use Printful\GettextCms\Exceptions\GettextCmsException; |
6
|
|
|
use Printful\GettextCms\Interfaces\MessageConfigInterface; |
7
|
|
|
use Printful\GettextCms\Structures\ScanItem; |
8
|
|
|
|
9
|
|
|
class MessageManager |
10
|
|
|
{ |
11
|
|
|
/** @var LocaleLoader */ |
12
|
|
|
private $loader; |
13
|
|
|
|
14
|
|
|
/** @var string Current locale set */ |
15
|
|
|
private $locale; |
16
|
|
|
|
17
|
|
|
/** @var MessageRevisions */ |
18
|
|
|
private $revisions; |
19
|
|
|
|
20
|
|
|
/** @var self */ |
21
|
|
|
private static $instance; |
22
|
|
|
|
23
|
|
|
/** @var MessageConfigInterface */ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** @var MessageStorage */ |
27
|
|
|
private $storage; |
28
|
|
|
|
29
|
1 |
|
private function __construct(MessageConfigInterface $config) |
30
|
|
|
{ |
31
|
1 |
|
$this->revisions = new MessageRevisions($config); |
32
|
1 |
|
$this->loader = new LocaleLoader($config, new MessageRevisions($config)); |
33
|
1 |
|
$this->config = $config; |
34
|
1 |
|
$this->storage = new MessageStorage($this->config->getRepository()); |
35
|
|
|
|
36
|
1 |
|
if ($config->useShortFunctions()) { |
37
|
1 |
|
$this->declareFunctions(); |
38
|
|
|
} |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initialize singleton instance |
43
|
|
|
* Singleton is necessary for short functions because they need to access the revision functionality globally. |
44
|
|
|
* |
45
|
|
|
* @param MessageConfigInterface $config |
46
|
|
|
* @return MessageManager |
47
|
|
|
*/ |
48
|
1 |
|
public static function init(MessageConfigInterface $config): self |
49
|
|
|
{ |
50
|
1 |
|
self::$instance = new static($config); |
51
|
|
|
|
52
|
1 |
|
return self::$instance; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return MessageManager |
57
|
|
|
*/ |
58
|
1 |
|
public static function getInstance(): self |
59
|
|
|
{ |
60
|
1 |
|
return self::$instance; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $locale |
65
|
|
|
* @return MessageManager |
66
|
|
|
* @throws GettextCmsException |
67
|
|
|
*/ |
68
|
1 |
|
public function setLocale(string $locale): self |
69
|
|
|
{ |
70
|
1 |
|
$this->locale = $locale; |
71
|
|
|
|
72
|
1 |
|
$this->loader->load($locale); |
73
|
|
|
|
74
|
1 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Declare helper functions for easier gettext usage |
79
|
|
|
* |
80
|
|
|
* @see _n() |
81
|
|
|
* @see _d() |
82
|
|
|
* @see _x() |
83
|
|
|
* @see _dn() |
84
|
|
|
* @see _dx() |
85
|
|
|
* @see _dxn() |
86
|
|
|
* |
87
|
|
|
* @return MessageManager |
88
|
|
|
*/ |
89
|
1 |
|
private function declareFunctions(): self |
90
|
|
|
{ |
91
|
1 |
|
require __DIR__ . '/helpers/functions.php'; |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get revisioned domain for current locale or the original domain name if revision does not exist for this domain |
98
|
|
|
* |
99
|
|
|
* @param string $domain |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
1 |
|
public function getRevisionedDomain(string $domain): string |
103
|
|
|
{ |
104
|
1 |
|
return $this->revisions->getRevisionedDomain($this->locale, $domain); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Extract translations from files and save them to repository |
109
|
|
|
* Careful, previous translations will be disabled, so everything has to be scanned at once |
110
|
|
|
* |
111
|
|
|
* @param ScanItem[] $scanItems |
112
|
|
|
* @throws GettextCmsException |
113
|
|
|
*/ |
114
|
1 |
|
public function extractAndSaveFromFiles(array $scanItems) |
115
|
|
|
{ |
116
|
1 |
|
if ($this->config->useShortFunctions()) { |
117
|
|
|
// If using short functions, we need to pre-fill them |
118
|
|
|
// so they are extracted too |
119
|
1 |
|
$functions = $this->getFunctions(); |
120
|
|
|
|
121
|
1 |
|
foreach ($scanItems as $scanItem) { |
122
|
1 |
|
$scanItem->functions = $scanItem->functions ?: []; |
123
|
1 |
|
$scanItem->functions += $functions; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$importer = new MessageImporter( |
128
|
1 |
|
$this->config, |
129
|
1 |
|
$this->storage, |
130
|
1 |
|
new MessageExtractor($this->config) |
131
|
|
|
); |
132
|
|
|
|
133
|
1 |
|
$importer->extractAndSave($scanItems); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Export PO string of untranslated messages |
138
|
|
|
* |
139
|
|
|
* @param string $locale |
140
|
|
|
* @param string $domain |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
1 |
|
public function exportUntranslatedPo(string $locale, string $domain): string |
144
|
|
|
{ |
145
|
1 |
|
$exporter = new UntranslatedMessageExporter($this->storage); |
146
|
|
|
|
147
|
1 |
|
return $exporter->exportPoString($locale, $domain); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Create a zip archive with messages that require translations as PO files |
152
|
|
|
* |
153
|
|
|
* @param string $zipPathname Full pathname to the file where the ZIP archive should be written |
154
|
|
|
* @param string $locale |
155
|
|
|
* @param string[]|null $domains Domains to export. If not provided, export all domains defined in config |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
1 |
|
public function exportUntranslatedPoZip(string $zipPathname, string $locale, array $domains = null): bool |
159
|
|
|
{ |
160
|
1 |
|
$exporter = new UntranslatedMessageZipExporter( |
161
|
1 |
|
$this->config, |
162
|
1 |
|
new UntranslatedMessageExporter($this->storage) |
163
|
|
|
); |
164
|
|
|
|
165
|
1 |
|
return $exporter->export($zipPathname, $locale, $domains); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Import translated messages from PO file |
170
|
|
|
* |
171
|
|
|
* @param string $poContent |
172
|
|
|
* @throws GettextCmsException |
173
|
|
|
*/ |
174
|
1 |
|
public function importTranslated($poContent) |
175
|
|
|
{ |
176
|
1 |
|
$importer = new TranslatedMessageImporter($this->config, $this->storage); |
177
|
1 |
|
$importer->importFromPo($poContent); |
178
|
1 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Build MO translation files for each locale and domain |
182
|
|
|
* |
183
|
|
|
* @throws GettextCmsException |
184
|
|
|
*/ |
185
|
1 |
|
public function buildTranslationFiles() |
186
|
|
|
{ |
187
|
1 |
|
$builder = new MessageBuilder($this->config, $this->storage, $this->revisions); |
188
|
|
|
|
189
|
1 |
|
foreach ($this->config->getLocales() as $locale) { |
190
|
1 |
|
$domains = $this->config->getOtherDomains(); |
191
|
1 |
|
$domains[] = $this->config->getDefaultDomain(); |
192
|
|
|
|
193
|
1 |
|
foreach ($domains as $domain) { |
194
|
1 |
|
$builder->export($locale, $domain); |
195
|
|
|
} |
196
|
|
|
} |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the dynamic importer for adding dynamic messages |
201
|
|
|
* |
202
|
|
|
* @return DynamicMessageImporter |
203
|
|
|
*/ |
204
|
1 |
|
public function getDynamicMessageImporter(): DynamicMessageImporter |
205
|
|
|
{ |
206
|
1 |
|
return new DynamicMessageImporter($this->config, $this->storage); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Functions to scan for including (default functions + custom functions) |
211
|
|
|
* |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
1 |
|
private function getFunctions(): array |
215
|
|
|
{ |
216
|
|
|
return [ |
217
|
|
|
// Default functions |
218
|
1 |
|
'_' => 'gettext', |
219
|
|
|
'gettext' => 'gettext', |
220
|
|
|
'ngettext' => 'ngettext', |
221
|
|
|
'pgettext' => 'pgettext', |
222
|
|
|
'dgettext' => 'dgettext', |
223
|
|
|
'dngettext' => 'dngettext', |
224
|
|
|
'dpgettext' => 'dpgettext', |
225
|
|
|
'npgettext' => 'npgettext', |
226
|
|
|
'dnpgettext' => 'dnpgettext', |
227
|
|
|
|
228
|
|
|
// Our custom functions |
229
|
|
|
'_n' => 'ngettext', |
230
|
|
|
'_nc' => 'npgettext', |
231
|
|
|
'_c' => 'pgettext', |
232
|
|
|
'_dc' => 'dpgettext', |
233
|
|
|
'_d' => 'dgettext', |
234
|
|
|
'_dn' => 'dngettext', |
235
|
|
|
'_dnc' => 'dnpgettext', |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
} |