-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocale.hh
More file actions
366 lines (311 loc) · 9.16 KB
/
Copy pathLocale.hh
File metadata and controls
366 lines (311 loc) · 9.16 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?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\Common\Bag;
use Titon\Intl\Bag\FormatBag;
use Titon\Intl\Bag\InflectionBag;
use Titon\Intl\Bag\MetadataBag;
use Titon\Intl\Bag\ValidationBag;
use Titon\Io\PathList;
use Titon\Utility\Col;
use \Locale as SystemLocale;
/**
* The `Locale` class manages all aspects of a locale code, like its region specific rules and translation messages.
*
* @package Titon\Intl
*/
class Locale {
/**
* Possible formats for locale codes.
*/
const int FORMAT_1 = 1; // en-us (urls)
const int FORMAT_2 = 2; // en-US
const int FORMAT_3 = 3; // en_US (preferred)
const int FORMAT_4 = 4; // enUS
/**
* Locale code.
*
* @var string
*/
protected string $code;
/**
* Format patterns bag.
*
* @var \Titon\Intl\Bag\FormatBag
*/
protected ?FormatBag $formatBag;
/**
* Inflection rules bag.
*
* @var \Titon\Intl\Bag\InflectionBag
*/
protected ?InflectionBag $inflectionBag;
/**
* Locale bundle.
*
* @var \Titon\Intl\LocaleBundle
*/
protected LocaleBundle $localeBundle;
/**
* Message bundle.
*
* @var \Titon\Intl\MessageBundle
*/
protected MessageBundle $messageBundle;
/**
* Metadata bag.
*
* @var \Titon\Intl\Bag\MetadataBag
*/
protected ?MetadataBag $metaBag;
/**
* Parent locale.
*
* @var \Titon\Intl\Locale
*/
protected ?Locale $parent;
/**
* Validation patterns bag.
*
* @var \Titon\Intl\Bag\ValidationBag
*/
protected ?ValidationBag $validationBag;
/**
* Set locale code and optional bundle.
*
* @param string $code
* @param \Titon\Intl\LocaleBundle $localeBundle
* @param \Titon\Intl\MessageBundle $messageBundle
*/
public function __construct(string $code, ?LocaleBundle $localeBundle = null, ?MessageBundle $messageBundle = null) {
$this->code = $code = self::canonicalize($code);
$this->localeBundle = $localeBundle ?: new LocaleBundle();
$this->messageBundle = $messageBundle ?: new MessageBundle();
// We can infer the parent from the locale code
if (($pos = strpos($code, '_')) !== false) {
$this->parent = LocaleRegistry::factory(substr($code, 0, $pos));
}
}
/**
* Add a locale lookup path.
*
* @param string $path
* @return $this
*/
public function addLocalePath(string $path): this {
$this->getLocaleBundle()->addPath(MessageLoader::DEFAULT_DOMAIN, $path . '/' . $this->getCode());
// Pass it to the parent also
$this->getParentLocale()?->addLocalePath($path);
return $this;
}
/**
* Add multiple locale lookup paths.
*
* @param \Titon\Io\PathList $paths
* @return $this
*/
public function addLocalePaths(PathList $paths): this {
foreach ($paths as $path) {
$this->addLocalePath($path);
}
return $this;
}
/**
* Add a message lookup path.
*
* @param string $domain
* @param string $path
* @return $this
*/
public function addMessagePath(string $domain, string $path): this {
$this->getMessageBundle()->addPath($domain, $path . '/' . $this->getCode());
// Pass it to the parent also
$this->getParentLocale()?->addMessagePath($domain, $path);
return $this;
}
/**
* Add multiple message lookup paths.
*
* @param string $domain
* @param \Titon\Io\PathList $paths
* @return $this
*/
public function addMessagePaths(string $domain, PathList $paths): this {
foreach ($paths as $path) {
$this->addMessagePath($domain, $path);
}
return $this;
}
/**
* Add a resource lookup path for locales and messages.
*
* @param string $domain
* @param string $path
* @return $this
*/
public function addResourcePath(string $domain, string $path): this {
$path = rtrim($path, '/');
$this->addLocalePath(sprintf('%s/locales', $path));
$this->addMessagePath($domain, sprintf('%s/messages', $path));
return $this;
}
/**
* Add multiple resource lookup paths.
*
* @param string $domain
* @param \Titon\Io\PathList $paths
* @return $this
*/
public function addResourcePaths(string $domain, PathList $paths): this {
foreach ($paths as $path) {
$this->addResourcePath($domain, $path);
}
return $this;
}
/**
* Convert a locale code to a specific format.
*
* @param string $code
* @param int $format
* @return string
*/
public static function canonicalize(string $code, int $format = self::FORMAT_3): string {
$parts = explode('-', str_replace('_', '-', strtolower($code)));
$return = $parts[0];
if (array_key_exists(1, $parts)) {
switch ($format) {
case self::FORMAT_1:
$return .= '-' . $parts[1];
break;
case self::FORMAT_2:
$return .= '-' . strtoupper($parts[1]);
break;
case self::FORMAT_3:
$return .= '_' . strtoupper($parts[1]);
break;
case self::FORMAT_4:
$return .= strtoupper($parts[1]);
break;
}
}
return $return;
}
/**
* Takes a map of key-values and returns a correctly ordered and delimited locale ID.
*
* @uses \Locale
*
* @param \Titon\Intl\LocaleTagMap $tags
* @return string
*/
public static function compose(LocaleTagMap $tags): string {
return (string) SystemLocale::composeLocale($tags);
}
/**
* Parses a locale string and returns a map of key-value locale tags.
*
* @uses \Locale
*
* @param string $locale
* @return \Titon\Intl\LocaleTagMap
*/
public static function decompose(string $locale): LocaleTagMap {
return new Map(SystemLocale::parseLocale($locale));
}
/**
* Return the locale code.
*
* @return string
*/
public function getCode(): string {
return $this->code;
}
/**
* Return the format patterns from the locale bundle.
*
* @return \Titon\Intl\Bag\FormatBag
*/
public function getFormatPatterns(): FormatBag {
if ($this->formatBag) {
return $this->formatBag;
}
$bag = $this->getLocaleBundle()->loadResource(MessageLoader::DEFAULT_DOMAIN, 'formats');
if ($parent = $this->getParentLocale()) {
$bag = Col::merge($parent->getFormatPatterns()->all(), $bag);
}
return $this->formatBag = new FormatBag($bag);
}
/**
* Return the inflection rules from the locale bundle.
*
* @return \Titon\Intl\Bag\InflectionBag
*/
public function getInflectionRules(): InflectionBag {
if ($this->inflectionBag) {
return $this->inflectionBag;
}
$bag = $this->getLocaleBundle()->loadResource(MessageLoader::DEFAULT_DOMAIN, 'inflections');
if ($parent = $this->getParentLocale()) {
$bag = Col::merge($parent->getInflectionRules()->all(), $bag);
}
return $this->inflectionBag = new InflectionBag($bag);
}
/**
* Return the locale bundle.
*
* @return \Titon\Intl\LocaleBundle
*/
public function getLocaleBundle(): LocaleBundle {
return $this->localeBundle;
}
/**
* Return the message bundle.
*
* @return \Titon\Intl\MessageBundle
*/
public function getMessageBundle(): MessageBundle {
return $this->messageBundle;
}
/**
* Return the metadata from the locale bundle.
*
* @return \Titon\Intl\Bag\MetadataBag
*/
public function getMetadata(): MetadataBag {
if ($this->metaBag) {
return $this->metaBag;
}
$bag = $this->getLocaleBundle()->loadResource(MessageLoader::DEFAULT_DOMAIN, 'locale');
if ($parent = $this->getParentLocale()) {
$bag = Col::merge($parent->getMetadata()->all(), $bag);
}
return $this->metaBag = new MetadataBag($bag);
}
/**
* Return the parent locale if it exists.
*
* @return \Titon\Intl\Locale
*/
public function getParentLocale(): ?Locale {
return $this->parent;
}
/**
* Return the validation patterns from the locale bundle.
*
* @return \Titon\Intl\Bag\ValidationBag
*/
public function getValidationPatterns(): ValidationBag {
if ($this->validationBag) {
return $this->validationBag;
}
$bag = $this->getLocaleBundle()->loadResource(MessageLoader::DEFAULT_DOMAIN, 'validations');
if ($parent = $this->getParentLocale()) {
$bag = Col::merge($parent->getValidationPatterns()->all(), $bag);
}
return $this->validationBag = new ValidationBag($bag);
}
}