|
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
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Initialize singleton instance |
|
39
|
|
|
* |
|
40
|
|
|
* @param MessageConfigInterface $config |
|
41
|
|
|
* @return MessageManager |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public static function init(MessageConfigInterface $config): self |
|
44
|
|
|
{ |
|
45
|
1 |
|
self::$instance = new static($config); |
|
46
|
|
|
|
|
47
|
1 |
|
if ($config->useShortFunctions()) { |
|
48
|
1 |
|
self::$instance->declareFunctions(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return self::$instance; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return MessageManager |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public static function getInstance(): self |
|
58
|
|
|
{ |
|
59
|
1 |
|
return self::$instance; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $locale |
|
64
|
|
|
* @return MessageManager |
|
65
|
|
|
* @throws GettextCmsException |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function setLocale(string $locale): self |
|
68
|
|
|
{ |
|
69
|
1 |
|
$this->locale = $locale; |
|
70
|
|
|
|
|
71
|
1 |
|
$this->loader->load($locale); |
|
72
|
|
|
|
|
73
|
1 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Declare helper functions for easier gettext usage |
|
78
|
|
|
* |
|
79
|
|
|
* @see _n() |
|
80
|
|
|
* @see _d() |
|
81
|
|
|
* @see _x() |
|
82
|
|
|
* @see _dn() |
|
83
|
|
|
* @see _dx() |
|
84
|
|
|
* @see _dxn() |
|
85
|
|
|
* |
|
86
|
|
|
* @return MessageManager |
|
87
|
|
|
*/ |
|
88
|
1 |
|
private function declareFunctions(): self |
|
89
|
|
|
{ |
|
90
|
1 |
|
require __DIR__ . '/helpers/functions.php'; |
|
91
|
|
|
|
|
92
|
1 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get revisioned domain for current locale or the original domain name if revision does not exist for this domain |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $domain |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getRevisionedDomain(string $domain): string |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->revisions->getRevisionedDomain($this->locale, $domain); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Extract translations from files and save them to repository |
|
108
|
|
|
* Careful, previous translations will be disabled, so everything has to be scanned at once |
|
109
|
|
|
* |
|
110
|
|
|
* @param ScanItem[] $scanItems |
|
111
|
|
|
* @throws GettextCmsException |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function extractAndSaveFromFiles(array $scanItems) |
|
114
|
|
|
{ |
|
115
|
1 |
|
if ($this->config->useShortFunctions()) { |
|
116
|
|
|
// If using short functions, we need to pre-fill them |
|
117
|
|
|
// so they are extracted too |
|
118
|
1 |
|
$functions = $this->getFunctions(); |
|
119
|
|
|
|
|
120
|
1 |
|
foreach ($scanItems as $scanItem) { |
|
121
|
1 |
|
$scanItem->functions = $scanItem->functions ?: []; |
|
122
|
1 |
|
$scanItem->functions += $functions; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
$importer = new MessageImporter( |
|
127
|
1 |
|
$this->config, |
|
128
|
1 |
|
$this->storage, |
|
129
|
1 |
|
new MessageExtractor($this->config) |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
1 |
|
$importer->extractAndSave($scanItems); |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Export PO string of untranslated messages |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $locale |
|
139
|
|
|
* @param string $domain |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function exportUntranslated(string $locale, string $domain): string |
|
143
|
|
|
{ |
|
144
|
1 |
|
$exporter = new UntranslatedMessageExporter($this->storage); |
|
145
|
|
|
|
|
146
|
1 |
|
return $exporter->exportToString($locale, $domain); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Import translated messages from PO file |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $poContent |
|
153
|
|
|
* @throws GettextCmsException |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function importTranslated($poContent) |
|
156
|
|
|
{ |
|
157
|
1 |
|
$importer = new TranslatedMessageImporter($this->config, $this->storage); |
|
158
|
1 |
|
$importer->importFromPo($poContent); |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Build MO translation files for each locale and domain |
|
163
|
|
|
* |
|
164
|
|
|
* @throws GettextCmsException |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function buildTranslationFiles() |
|
167
|
|
|
{ |
|
168
|
1 |
|
$builder = new MessageBuilder($this->config, $this->storage, $this->revisions); |
|
169
|
|
|
|
|
170
|
1 |
|
foreach ($this->config->getLocales() as $locale) { |
|
171
|
1 |
|
$domains = $this->config->getOtherDomains(); |
|
172
|
1 |
|
$domains[] = $this->config->getDefaultDomain(); |
|
173
|
|
|
|
|
174
|
1 |
|
foreach ($domains as $domain) { |
|
175
|
1 |
|
$builder->export($locale, $domain); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
1 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Functions to scan for including (default functions + custom functions) |
|
182
|
|
|
* |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
1 |
|
private function getFunctions(): array |
|
186
|
|
|
{ |
|
187
|
|
|
return [ |
|
188
|
|
|
// Default functions |
|
189
|
1 |
|
'_' => 'gettext', |
|
190
|
|
|
'gettext' => 'gettext', |
|
191
|
|
|
'ngettext' => 'ngettext', |
|
192
|
|
|
'pgettext' => 'pgettext', |
|
193
|
|
|
'dgettext' => 'dgettext', |
|
194
|
|
|
'dngettext' => 'dngettext', |
|
195
|
|
|
'dpgettext' => 'dpgettext', |
|
196
|
|
|
'npgettext' => 'npgettext', |
|
197
|
|
|
'dnpgettext' => 'dnpgettext', |
|
198
|
|
|
|
|
199
|
|
|
// Our custom functions |
|
200
|
|
|
'_n' => 'ngettext', |
|
201
|
|
|
'_nc' => 'npgettext', |
|
202
|
|
|
'_c' => 'pgettext', |
|
203
|
|
|
'_dc' => 'dpgettext', |
|
204
|
|
|
'_d' => 'dgettext', |
|
205
|
|
|
'_dn' => 'dngettext', |
|
206
|
|
|
'_dnc' => 'dnpgettext', |
|
207
|
|
|
]; |
|
208
|
|
|
} |
|
209
|
|
|
} |