@ham2k/extension-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/i18n.js ADDED
@@ -0,0 +1,43 @@
1
+ import i18next from "i18next";
2
+ function createCachedTranslator(resources) {
3
+ let cachedLocale;
4
+ let cachedT;
5
+ return (ctx) => {
6
+ const locale = ctx.locale ?? "en";
7
+ if (!cachedT || cachedLocale !== locale) {
8
+ cachedLocale = locale;
9
+ cachedT = createTranslator(resources, locale);
10
+ }
11
+ return cachedT;
12
+ };
13
+ }
14
+ function createTranslator(resources, currentLocale) {
15
+ const instance = i18next.createInstance();
16
+ instance.init({
17
+ lng: currentLocale,
18
+ fallbackLng: "en",
19
+ resources,
20
+ // Explicit because the engine has no Intl. i18next's PluralResolver
21
+ // probes for Intl.PluralRules, does not find it under QuickJS-NG, and logs
22
+ // "Your environment seems not to be Intl API compatible" through the host
23
+ // log on EVERY translator build — and createCachedTranslator builds one per
24
+ // extension per locale, so that is live noise on every platform.
25
+ //
26
+ // Declaring v3 makes the supported plural shape a decision rather than a
27
+ // silent fallback. It is inert for correctness today: no catalog uses a
28
+ // plural-suffixed key in either shape, and no call
29
+ // site passes `count` to t() — the one place that needs a plural spells it
30
+ // out with a ternary (core/scoring/src/scoring.ts). If a catalog ever does
31
+ // want plurals, v3's `key`/`key_plural` pair is what will work here.
32
+ compatibilityJSON: "v3",
33
+ interpolation: {
34
+ escapeValue: false
35
+ // Not in a browser/HTML environment
36
+ }
37
+ });
38
+ return (key, options) => instance.t(key, options);
39
+ }
40
+ export {
41
+ createCachedTranslator,
42
+ createTranslator
43
+ };