-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocaleRegistry.hh
52 lines (43 loc) · 1.16 KB
/
LocaleRegistry.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
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Intl;
/**
* The registry manages the instantiation of `Locale` objects to reduce the overhead of resource loading.
*
* @package Titon\Intl
*/
class LocaleRegistry {
/**
* A mapping of registered locales.
*
* @var \Titon\Intl\LocaleMap
*/
protected static LocaleMap $locales = Map {};
/**
* Instantiate or return a `Locale` with the given locale code.
*
* @param string $code
* @return \Titon\Intl\Locale
*/
public static function factory(string $code): Locale {
$code = Locale::canonicalize($code);
if (static::$locales->contains($code)) {
return static::$locales[$code];
}
return static::set(new Locale($code));
}
/**
* Set a `Locale` manually.
*
* @param \Titon\Intl\Locale $locale
* @return \Titon\Intl\Locale
*/
public static function set(Locale $locale): Locale {
static::$locales[$locale->getCode()] = $locale;
return $locale;
}
}