-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageLoader.hh
224 lines (187 loc) · 5.99 KB
/
MessageLoader.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Intl;
use Titon\Cache\Item;
use Titon\Cache\Storage;
use Titon\Intl\Exception\UndetectedLocaleException;
use Titon\Io\Reader;
use Titon\Io\ReaderList;
use \MessageFormatter;
/**
* The `MessageLoader` handles the loading and translating of messages found within catalogs.
*
* @package Titon\Intl
*/
class MessageLoader {
const string DEFAULT_DOMAIN = 'common';
const string DEFAULT_CATALOG = 'default';
/**
* Loaded catalogs.
*
* @var \Titon\Intl\CatalogMap
*/
protected CatalogMap $catalogs = Map {};
/**
* File readers used for parsing.
*
* @var \Titon\Io\ReaderList
*/
protected ReaderList $readers = Vector {};
/**
* Storage engine used for caching.
*
* @var \Titon\Cache\Storage
*/
protected ?Storage $storage;
/**
* Translator instance.
*
* @var \Titon\Intl\Translator
*/
protected ?Translator $translator;
/**
* Store the reader and optional storage.
*
* @param \Titon\Io\ReaderList $readers
* @param \Titon\Cache\Storage $storage
*/
public function __construct(ReaderList $readers, ?Storage $storage = null) {
$this->readers = $readers;
$this->storage = $storage;
}
/**
* Add a file reader to use for resource parsing.
*
* @param \Titon\Io\Reader $reader
* @return \Titon\Intl\Translator
*/
public function addReader(Reader $reader): this {
$this->readers[] = $reader;
return $this;
}
/**
* Add multiple file readers.
*
* @param \Titon\Io\ReaderList $readers
* @return \Titon\Intl\Translator
*/
public function addReaders(ReaderList $readers): this {
$this->readers->addAll($readers);
return $this;
}
/**
* Return the file readers.
*
* @return \Titon\Io\ReaderList
*/
public function getReaders(): ReaderList {
return $this->readers;
}
/**
* Return the cache storage engine.
*
* @return \Titon\Cache\Storage
*/
public function getStorage(): ?Storage {
return $this->storage;
}
/**
* Return the translator.
*
* @return \Titon\Intl\Translator
*/
public function getTranslator(): Translator {
invariant($this->translator !== null, 'Translator must be defined.');
return $this->translator;
}
/**
* Load and generates a catalog of messages based on the defined domain and catalog.
* The method will attempt to loop through the list of cascading locales,
* loading and merging messages from each catalog resource file.
*
* @param string $domain
* @param string $catalog
* @param mixed $ttl
* @return \Titon\Intl\Catalog
*/
public function loadCatalog(string $domain, string $catalog, mixed $ttl = null): Catalog {
$translator = $this->getTranslator();
$code = $translator->current()?->getCode();
$cacheKey = sprintf('intl.catalog.%s.%s.%s', $domain, $catalog, $code);
if ($code === null) {
throw new UndetectedLocaleException('No locale has been detected. Cannot load messages.');
}
// Return the catalog if it has been loaded
if ($this->catalogs->contains($cacheKey)) {
return $this->catalogs[$cacheKey];
}
$storage = $this->getStorage();
$messages = Map {};
// Check within the cache first
if ($storage && $storage->has($cacheKey)) {
$item = $storage->getItem($cacheKey);
if ($item->isHit()) {
$messages = $item->get();
}
}
// Cycle through each locale and load messages from each catalog
if (!$messages) {
$locales = $translator->cascade()->toVector();
$locales->reverse(); // Reverse order so that parents are loaded first
foreach ($locales as $locale) {
$bundle = $translator->getLocale($locale)->getMessageBundle();
$bundle->addReaders($this->getReaders());
$bundleMessages = $bundle->loadResource($domain, $catalog);
// Merge with the previous messages
if ($bundleMessages instanceof Map && $messages instanceof Map) {
$messages->setAll($bundleMessages);
}
}
// Cache the messages
if ($storage && $messages) {
$storage->save(new Item($cacheKey, $messages, $ttl)); // TODO customize TTL
}
}
invariant($messages instanceof Map, 'Message strings must be a map.');
return $this->catalogs[$cacheKey] = new Catalog($catalog, $domain, $messages);
}
/**
* Set the storage engine to use for catalog caching.
*
* @param \Titon\Cache\Storage $storage
* @return \Titon\Intl\Translator
*/
public function setStorage(Storage $storage): this {
$this->storage = $storage;
return $this;
}
/**
* Set the translator instance.
*
* @param \Titon\Intl\Translator $translator
* @return $this
*/
public function setTranslator(Translator $translator): this {
$this->translator = $translator;
return $this;
}
/**
* Load a message from the defined domain catalog and interpolate parameters using the
* built-in `MessageFormatter`.
*
* @param string $key
* @param \Titon\Intl\ParamList $params
* @return string
*/
public function translate(string $key, ParamList $params = Vector {}): string {
$key = Catalog::parseKey($key);
return (string) MessageFormatter::formatMessage(
(string) $this->getTranslator()->current()?->getCode(),
$this->loadCatalog($key['domain'], $key['catalog'])->getMessage($key['id']),
$params->toArray());
}
}