-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI18n.php
181 lines (160 loc) · 4.95 KB
/
I18n.php
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
<?php declare(strict_types=1);
namespace Koded\I18n;
use Koded\Stdlib\Config;
use function array_combine;
use function array_keys;
use function array_map;
use function ini_set;
use function locale_get_default;
use function locale_set_default;
use function str_replace;
use function strtr;
use function vsprintf;
interface I18nFormatter
{
/**
* Message formatter for argument replacement in the message.
*
* @param string $string
* @param array $arguments
* @return string The message with applied arguments (if any)
*/
public function format(string $string, array $arguments): string;
}
final class StrtrFormatter implements I18nFormatter
{
public function format(string $string, array $arguments): string
{
return empty($arguments) ? $string : strtr($string, $arguments);
}
}
final class DefaultFormatter implements I18nFormatter
{
public function format(string $string, array $arguments): string
{
return empty($arguments) ? $string : vsprintf($string, $arguments);
}
}
final class CurlyFormatter implements I18nFormatter
{
public function format(string $string, array $arguments): string
{
if (empty($arguments)) {
return $string;
}
$keys = $vals = [];
foreach ($arguments as $k => $v) {
$keys[] = "{{$k}}";
$vals[] = (string) $v;
}
return str_replace($keys, $vals, $string);
}
}
class I18n
{
public const DEFAULT_LOCALE = 'en_US';
/*
* Default configuration parameters for all catalogs.
* Values are taken by the explicitly set default locale,
* or the first loaded catalog instance.
*/
private static ?string $catalog = null;
private static ?string $formatter = null;
private static ?string $directory = null;
private static ?string $locale = null;
/** @var array<string, I18nCatalog> */
private static array $catalogs = [];
// @codeCoverageIgnoreStart
private function __construct() {}
// @codeCoverageIgnoreEnd
public static function translate(
string $string,
array $arguments = [],
string $locale = ''): string
{
return self::catalog($locale)->translate('messages', $string, $arguments);
}
public static function locale(): string
{
return self::$locale ??= I18nCatalog::normalizeLocale(locale_get_default());
}
/**
* @return array{string, I18nCatalog}
*/
public static function catalogs(): array
{
return self::$catalogs;
}
static public function catalog(string $locale): I18nCatalog
{
empty($locale) and $locale = self::locale();
return self::catalogs()[$locale] ?? self::registerCatalog($locale);
}
/**
* @return array<string, string>
*/
public static function languages(): array
{
return array_combine(
array_keys(self::$catalogs),
array_map(static fn(I18nCatalog $c) => $c->language(), self::$catalogs)
);
}
/**
* @return array{locale:string, catalogs:<string, array{class: string, formatter:string, dir:string, locale:string}>}
*/
public static function info(): array
{
$catalogs = [];
foreach (self::$catalogs as $locale => $instance) {
$catalogs[$locale] = [
'class' => $instance::class,
'formatter' => $instance->formatter()::class,
'dir' => $instance->directory(),
'locale' => $instance->locale(),
];
}
return [
'locale' => self::$locale,
'catalogs' => $catalogs,
];
}
public static function register(
I18nCatalog $catalog,
bool $asDefault = false): void
{
$locale = $catalog->locale();
if ($asDefault || empty(self::$catalogs)) {
self::setDefaultLocale($locale);
self::$directory = $catalog->directory();
self::$formatter = $catalog->formatter()::class;
self::$catalog = $catalog::class;
}
self::$catalogs[$locale] = $catalog;
}
public static function flush(): void
{
self::$catalogs = [];
self::$directory = null;
self::$formatter = null;
self::$catalog = null;
self::$locale = null;
ini_set('intl.default_locale', '');
locale_set_default('');
}
private static function registerCatalog(string $locale): I18nCatalog
{
return self::$catalogs[$locale] = I18nCatalog::new((new Config)->import([
'translation.locale' => $locale,
'translation.dir' => self::$directory,
'translation.formatter' => self::$formatter,
'translation.catalog' => self::$catalog,
]));
}
private static function setDefaultLocale(string $locale): void
{
self::$locale = $locale;
ini_set('intl.default_locale', $locale);
locale_set_default($locale);
}
}