-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT.php
More file actions
324 lines (304 loc) Β· 10.9 KB
/
Copy pathT.php
File metadata and controls
324 lines (304 loc) Β· 10.9 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
<?php
declare(strict_types=1);
namespace SugarCraft\Core\I18n;
/**
* Tiny, zero-dependency translation helper for the SugarCraft monorepo.
*
* Every SugarCraft library shares this single registry so that user-facing
* strings β exception messages, prompt labels, CLI usage banners β can be
* swapped out by locale without bringing in a heavy i18n framework.
*
* ## Mental model
*
* 1. Each library owns a **namespace** (e.g. `'core'`, `'charts'`,
* `'prompt'`) and a **lang directory** containing one PHP file per
* locale: `lang/en.php`, `lang/fr.php`, β¦
* 2. Every lang file `return`s a flat `array<string,string>` keyed by
* sub-key (no namespace prefix). Values may contain `{name}`
* placeholders.
* 3. Libraries register themselves via {@see register()} (idempotent),
* and call sites look strings up via {@see translate()} or its
* short alias {@see t()} using fully-qualified `'<ns>.<key>'` keys.
* 4. Lookup falls back from the active locale β `'en'` β the raw key
* itself, so a missing translation never throws.
*
* ## Example β registering a library
*
* Each library typically ships a small `Lang` helper that wraps this
* registry with its own namespace baked in:
*
* ```php
* // sugar-charts/src/Lang.php
* final class Lang
* {
* public static function t(string $key, array $params = []): string
* {
* T::register('charts', __DIR__ . '/../lang');
* return T::translate('charts.' . $key, $params);
* }
* }
* ```
*
* Call sites then read naturally:
*
* ```php
* throw new \InvalidArgumentException(Lang::t('heatmap.invalid_dimensions'));
* ```
*
* ## Example β switching locale
*
* ```php
* T::setLocale('fr'); // explicit
* T::setLocale(T::detect()); // from $LANG / $LC_ALL
* echo T::t('core.color.invalid_hex', ['hex' => '#zz']);
* ```
*
* ## Placeholder syntax
*
* Values use `{name}` placeholders, replaced from the `$params` array:
*
* ```php
* // lang/en.php
* return ['color.invalid_hex' => 'invalid hex color: {hex}'];
*
* T::t('core.color.invalid_hex', ['hex' => '#zz']);
* // => "invalid hex color: #zz"
* ```
*
* Unmatched placeholders are left intact so that missing context is
* visible rather than silently dropped.
*
* ## Why not symfony/translation?
*
* SugarCraft libraries deliberately keep zero runtime dependencies
* outside `react/event-loop`. Trading ICU MessageFormat / XLIFF tooling
* for a ~150-line helper keeps every downstream lib free to be required
* standalone.
*/
final class T
{
/**
* Map of namespace β lang directory absolute path.
*
* Populated lazily by {@see register()}; consulted by {@see translate()}
* to resolve the file that backs a given key.
*
* @var array<string, string>
*/
private static array $namespaces = [];
/**
* In-memory cache of loaded lang files.
*
* Shape: `$cache[namespace][locale] = ['key.path' => 'translated', β¦]`.
* Each `(namespace, locale)` pair is loaded at most once per process.
*
* @var array<string, array<string, array<string, string>>>
*/
private static array $cache = [];
/**
* The active locale. Defaults to `'en'`; override via {@see setLocale()}
* or pass an explicit `$locale` argument to {@see translate()}.
*/
private static string $locale = 'en';
/**
* Register a library's translation directory.
*
* Calling more than once for the same `$namespace` is a no-op so the
* call is safe to put inline at the top of every lookup helper. The
* directory does not need to exist at registration time β files are
* only opened on first lookup.
*
* @param string $namespace First segment of the translation key (e.g.
* `'core'`, `'charts'`). Must not contain
* a `.` separator.
* @param string $dir Absolute path to the directory containing
* per-locale PHP files (`en.php`, `fr.php`, β¦).
*/
public static function register(string $namespace, string $dir): void
{
if (str_contains($namespace, '.')) {
throw new \InvalidArgumentException(
"i18n namespace must not contain '.': $namespace"
);
}
// First registration wins β later attempts (e.g. shadowed by a
// downstream consumer that wants to override translations) should
// re-register intentionally via overrideNamespace().
self::$namespaces[$namespace] ??= $dir;
}
/**
* Replace the directory backing an already-registered namespace.
*
* Intended for application-level overrides (e.g. an end-user app
* wants to ship its own translations of `charts.*` strings without
* patching the upstream library).
*/
public static function overrideNamespace(string $namespace, string $dir): void
{
self::$namespaces[$namespace] = $dir;
unset(self::$cache[$namespace]);
}
/**
* Translate a fully-qualified key.
*
* The key is split on the **first** `.` β everything before it is the
* namespace, everything after is the lookup key inside that namespace's
* lang file. Keys with no `.` are returned untranslated.
*
* Resolution order:
*
* 1. `lang/$locale.php` (e.g. `fr-fr`) for the namespace
* 2. `lang/<base-language>.php` (e.g. `fr` when locale is `fr-fr`)
* 3. `lang/en.php` for the namespace (universal fallback)
* 4. The raw key (so a missing translation surfaces visibly)
*
* Step 2 means a single `fr.php` file covers `fr-fr`, `fr-ca`,
* `fr-be`, etc. β only add a regional file (e.g. `pt-br.php`) when
* the wording genuinely diverges from the base language.
*
* @param string $key e.g. `'core.color.invalid_hex'`.
* @param array<string, string|int|float> $params Placeholder values for `{name}` substitution.
* @param string|null $locale Override the active locale for this call only.
*/
public static function translate(string $key, array $params = [], ?string $locale = null): string
{
$locale ??= self::$locale;
$dot = strpos($key, '.');
if ($dot === false) {
return self::interpolate($key, $params);
}
$namespace = substr($key, 0, $dot);
$subKey = substr($key, $dot + 1);
$value = self::lookup($namespace, $subKey, $locale);
if ($value === null) {
$dash = strpos($locale, '-');
if ($dash !== false) {
$value = self::lookup($namespace, $subKey, substr($locale, 0, $dash));
}
}
$value ??= self::lookup($namespace, $subKey, 'en') ?? $key;
return self::interpolate($value, $params);
}
/**
* Convenience alias for {@see translate()}.
*
* @param array<string, string|int|float> $params
*/
public static function t(string $key, array $params = [], ?string $locale = null): string
{
return self::translate($key, $params, $locale);
}
/** Set the process-wide active locale (e.g. `'en'`, `'fr'`, `'de'`). */
public static function setLocale(string $locale): void
{
self::$locale = self::normalize($locale);
}
/** Return the process-wide active locale. */
public static function locale(): string
{
return self::$locale;
}
/**
* Detect a sensible locale from the environment.
*
* Reads `$_SERVER['LC_ALL']`, `$_SERVER['LC_MESSAGES']`, then
* `$_SERVER['LANG']`, falling back to `'en'`. Strips encoding
* suffixes (`fr_FR.UTF-8` β `fr`) and lowercases the result.
*
* Useful as `T::setLocale(T::detect())` at app startup.
*/
public static function detect(): string
{
foreach (['LC_ALL', 'LC_MESSAGES', 'LANG'] as $var) {
$raw = $_SERVER[$var] ?? getenv($var) ?: null;
if (is_string($raw) && !in_array($raw, ['', 'C', 'POSIX'], true)) {
return self::normalize($raw);
}
}
return 'en';
}
/**
* Reset all internal state β registered namespaces, cached files, and
* the active locale.
*
* Test-only hook; production code should never need this.
*
* @internal
*/
public static function reset(): void
{
self::$namespaces = [];
self::$cache = [];
self::$locale = 'en';
}
/**
* Look up a key inside a `(namespace, locale)` pair, loading the
* underlying file lazily and returning `null` on miss.
*/
private static function lookup(string $namespace, string $key, string $locale): ?string
{
if (!isset(self::$namespaces[$namespace])) {
return null;
}
if (!isset(self::$cache[$namespace][$locale])) {
self::$cache[$namespace][$locale] = self::load(self::$namespaces[$namespace], $locale);
}
return self::$cache[$namespace][$locale][$key] ?? null;
}
/**
* Read and validate the lang file for a single locale.
*
* @return array<string, string>
*/
private static function load(string $dir, string $locale): array
{
$path = $dir . DIRECTORY_SEPARATOR . $locale . '.php';
if (!is_file($path)) {
return [];
}
/** @psalm-suppress UnresolvableInclude */
$data = require $path;
if (!is_array($data)) {
return [];
}
// Coerce non-string values to strings rather than blowing up β a
// bad lang file shouldn't take down a running program.
$out = [];
foreach ($data as $k => $v) {
if (is_string($k)) {
$out[$k] = is_string($v) ? $v : (string) $v;
}
}
return $out;
}
/**
* Replace `{name}` placeholders in `$value` with values from `$params`.
*
* Unmatched placeholders are left literal so that a missing parameter
* is obvious in the rendered output instead of vanishing.
*
* @param array<string, string|int|float> $params
*/
private static function interpolate(string $value, array $params): string
{
if ($params === [] || !str_contains($value, '{')) {
return $value;
}
$replacements = [];
foreach ($params as $name => $val) {
$replacements['{' . $name . '}'] = (string) $val;
}
return strtr($value, $replacements);
}
/**
* Normalize a locale string from the environment to our canonical
* `lang/<locale>.php` form: lowercase, dashes only, no encoding.
*/
private static function normalize(string $raw): string
{
// Drop encoding suffix: "fr_FR.UTF-8@euro" β "fr_FR"
$raw = preg_replace('/[.@].*$/', '', $raw) ?? $raw;
$raw = strtolower(str_replace('_', '-', $raw));
return $raw === '' ? 'en' : $raw;
}
}