react-native-i18njs 0.0.3 → 1.0.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/index.js CHANGED
@@ -1,588 +1 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var i18nJs = require('i18n-js');
6
- var RNLocalize = require('react-native-localize');
7
- var reactNative = require('react-native');
8
- var React = require('react');
9
-
10
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
-
12
- function _interopNamespace(e) {
13
- if (e && e.__esModule) return e;
14
- var n = Object.create(null);
15
- if (e) {
16
- Object.keys(e).forEach(function (k) {
17
- if (k !== 'default') {
18
- var d = Object.getOwnPropertyDescriptor(e, k);
19
- Object.defineProperty(n, k, d.get ? d : {
20
- enumerable: true,
21
- get: function () { return e[k]; }
22
- });
23
- }
24
- });
25
- }
26
- n.default = e;
27
- return Object.freeze(n);
28
- }
29
-
30
- var RNLocalize__namespace = /*#__PURE__*/_interopNamespace(RNLocalize);
31
- var React__default = /*#__PURE__*/_interopDefault(React);
32
-
33
- var __defProp = Object.defineProperty;
34
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
35
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
36
- var RTL_SCRIPTS = /* @__PURE__ */ new Set(["Arab", "Hebr", "Thaa", "Syrc", "Nkoo", "Adlm"]);
37
- var RTL_LANGUAGE_CODES = /* @__PURE__ */ new Set([
38
- "ar",
39
- // Arabic
40
- "fa",
41
- // Persian
42
- "he",
43
- // Hebrew
44
- "iw",
45
- // Hebrew (legacy)
46
- "ur",
47
- // Urdu
48
- "ps",
49
- // Pashto
50
- "dv",
51
- // Divehi
52
- "ku",
53
- // Kurdish
54
- "ug",
55
- // Uyghur
56
- "yi",
57
- // Yiddish
58
- "sd"
59
- // Sindhi
60
- ]);
61
- var DefaultI18nEngine = class {
62
- constructor() {
63
- __publicField(this, "i18n");
64
- __publicField(this, "listeners");
65
- __publicField(this, "onLocaleChange");
66
- __publicField(this, "fallbackLocales");
67
- __publicField(this, "missingBehavior");
68
- __publicField(this, "onMissingKey");
69
- __publicField(this, "enableFallback");
70
- __publicField(this, "followSystem");
71
- __publicField(this, "localeSource");
72
- __publicField(this, "initialized");
73
- __publicField(this, "version");
74
- __publicField(this, "readyPromise");
75
- __publicField(this, "resolveReady");
76
- // --- 性能缓存 ---
77
- /** Intl 格式化器缓存,key = locale + JSON(options) */
78
- __publicField(this, "numberFormatCache", /* @__PURE__ */ new Map());
79
- __publicField(this, "dateFormatCache", /* @__PURE__ */ new Map());
80
- /** locale chain 缓存,locale 或 fallbackLocales 变化时失效 */
81
- __publicField(this, "localeChainCache", /* @__PURE__ */ new Map());
82
- this.i18n = new i18nJs.I18n();
83
- this.listeners = /* @__PURE__ */ new Set();
84
- this.i18n.enableFallback = false;
85
- this.missingBehavior = "key";
86
- this.enableFallback = true;
87
- this.followSystem = true;
88
- this.localeSource = "system";
89
- this.initialized = false;
90
- this.version = 0;
91
- this.readyPromise = new Promise((resolve) => {
92
- this.resolveReady = resolve;
93
- });
94
- }
95
- init(translations, options = {}) {
96
- var _a;
97
- const {
98
- defaultLocale = "en",
99
- enableFallback = true,
100
- onLocaleChange,
101
- fallbackLocales,
102
- followSystem = true,
103
- missingBehavior = "key",
104
- onMissingKey
105
- } = options;
106
- this.i18n.store(this.normalizeTranslations(translations));
107
- this.i18n.defaultLocale = this.normalizeLocaleTag(defaultLocale);
108
- this.i18n.locale = this.normalizeLocaleTag(defaultLocale);
109
- this.enableFallback = enableFallback;
110
- this.onLocaleChange = onLocaleChange;
111
- this.fallbackLocales = fallbackLocales;
112
- this.followSystem = followSystem;
113
- this.localeSource = "system";
114
- this.missingBehavior = missingBehavior;
115
- this.onMissingKey = onMissingKey;
116
- this.invalidateCaches();
117
- if (this.followSystem) this.updateLocale();
118
- this.version += 1;
119
- this.notifyListeners({ type: "translations", version: this.version });
120
- if (!this.initialized) {
121
- this.initialized = true;
122
- (_a = this.resolveReady) == null ? void 0 : _a.call(this);
123
- this.resolveReady = void 0;
124
- }
125
- }
126
- loadTranslations(translations) {
127
- this.i18n.store(this.normalizeTranslations(translations));
128
- this.version += 1;
129
- this.notifyListeners({ type: "translations", version: this.version });
130
- }
131
- updateLocale() {
132
- if (!this.followSystem) return;
133
- if (this.localeSource === "user") return;
134
- let locales = [];
135
- try {
136
- locales = RNLocalize__namespace.getLocales();
137
- } catch {
138
- return;
139
- }
140
- if (!locales || locales.length === 0) return;
141
- const systemLocale = this.normalizeLocaleTag(locales[0].languageTag);
142
- if (!systemLocale) return;
143
- this.setLocaleFromSystem(systemLocale);
144
- }
145
- setLocale(locale) {
146
- this.localeSource = "user";
147
- this.applyLocale(locale);
148
- }
149
- getLocale() {
150
- return this.i18n.locale;
151
- }
152
- resetToSystem() {
153
- this.localeSource = "system";
154
- this.updateLocale();
155
- }
156
- t(scope, options) {
157
- var _a, _b, _c;
158
- if (typeof scope !== "string") {
159
- return (_a = this.normalizeTranslateResult(this.i18n.t(scope, options))) != null ? _a : "";
160
- }
161
- const key = scope;
162
- const currentLocale = this.i18n.locale;
163
- const chain = this.enableFallback ? this.getLocaleChain(currentLocale) : [currentLocale];
164
- for (const locale of chain) {
165
- if (this.hasTranslation(locale, key)) {
166
- const translated = this.normalizeTranslateResult(
167
- this.translateAtLocale(locale, scope, options)
168
- );
169
- if (translated !== void 0) return translated;
170
- }
171
- }
172
- (_b = this.onMissingKey) == null ? void 0 : _b.call(this, key, currentLocale);
173
- if (options && Object.prototype.hasOwnProperty.call(options, "defaultValue")) {
174
- return (_c = this.normalizeTranslateResult(
175
- this.translateAtLocale(currentLocale, scope, options)
176
- )) != null ? _c : "";
177
- }
178
- if (this.missingBehavior === "empty") return "";
179
- if (this.missingBehavior === "throw") {
180
- throw new Error(`Missing translation: ${currentLocale}.${key}`);
181
- }
182
- return key;
183
- }
184
- formatNumber(n, options) {
185
- if (typeof Intl === "undefined" || typeof Intl.NumberFormat !== "function") {
186
- return String(n);
187
- }
188
- try {
189
- return this.getCachedNumberFormat(this.i18n.locale, options).format(n);
190
- } catch {
191
- return String(n);
192
- }
193
- }
194
- formatCurrency(n, currency, options) {
195
- if (typeof Intl === "undefined" || typeof Intl.NumberFormat !== "function") {
196
- return `${n} ${currency}`;
197
- }
198
- try {
199
- return this.getCachedNumberFormat(this.i18n.locale, {
200
- ...options,
201
- style: "currency",
202
- currency
203
- }).format(n);
204
- } catch {
205
- return `${n} ${currency}`;
206
- }
207
- }
208
- formatDate(date, options) {
209
- const d = typeof date === "number" ? new Date(date) : date;
210
- if (Number.isNaN(d.getTime())) return "";
211
- if (typeof Intl === "undefined" || typeof Intl.DateTimeFormat !== "function") {
212
- return d.toISOString();
213
- }
214
- try {
215
- return this.getCachedDateFormat(this.i18n.locale, options).format(d);
216
- } catch {
217
- return d.toISOString();
218
- }
219
- }
220
- subscribe(listener) {
221
- this.listeners.add(listener);
222
- return () => {
223
- this.listeners.delete(listener);
224
- };
225
- }
226
- isRTL() {
227
- return reactNative.I18nManager.isRTL;
228
- }
229
- ready() {
230
- if (this.initialized) return Promise.resolve();
231
- return this.readyPromise;
232
- }
233
- isReady() {
234
- return this.initialized;
235
- }
236
- // ─── 缓存管理 ────────────────────────────────────────────
237
- invalidateCaches() {
238
- this.numberFormatCache.clear();
239
- this.dateFormatCache.clear();
240
- this.localeChainCache.clear();
241
- }
242
- getCachedNumberFormat(locale, options) {
243
- const cacheKey = locale + (options ? JSON.stringify(options) : "");
244
- let fmt = this.numberFormatCache.get(cacheKey);
245
- if (!fmt) {
246
- fmt = new Intl.NumberFormat(locale, options);
247
- this.numberFormatCache.set(cacheKey, fmt);
248
- }
249
- return fmt;
250
- }
251
- getCachedDateFormat(locale, options) {
252
- const cacheKey = locale + (options ? JSON.stringify(options) : "");
253
- let fmt = this.dateFormatCache.get(cacheKey);
254
- if (!fmt) {
255
- fmt = new Intl.DateTimeFormat(locale, options);
256
- this.dateFormatCache.set(cacheKey, fmt);
257
- }
258
- return fmt;
259
- }
260
- // ─── 私有方法 ────────────────────────────────────────────
261
- handleRTL(locale) {
262
- var _a, _b;
263
- const isRTL2 = this.isRTLLocale(locale);
264
- if (typeof ((_a = reactNative.I18nManager) == null ? void 0 : _a.allowRTL) !== "function" || typeof ((_b = reactNative.I18nManager) == null ? void 0 : _b.forceRTL) !== "function") {
265
- return;
266
- }
267
- if (reactNative.I18nManager.isRTL !== isRTL2) {
268
- reactNative.I18nManager.allowRTL(isRTL2);
269
- reactNative.I18nManager.forceRTL(isRTL2);
270
- }
271
- }
272
- notifyListeners(change) {
273
- this.listeners.forEach((listener) => listener(this.i18n.locale, change));
274
- }
275
- setLocaleFromSystem(locale) {
276
- this.localeSource = "system";
277
- this.applyLocale(locale);
278
- }
279
- applyLocale(locale) {
280
- var _a;
281
- const normalizedLocale = this.normalizeLocaleTag(locale);
282
- if (this.i18n.locale !== normalizedLocale) {
283
- this.i18n.locale = normalizedLocale;
284
- this.invalidateCaches();
285
- this.handleRTL(normalizedLocale);
286
- this.version += 1;
287
- this.notifyListeners({ type: "locale", version: this.version });
288
- (_a = this.onLocaleChange) == null ? void 0 : _a.call(this, normalizedLocale);
289
- }
290
- }
291
- translateAtLocale(locale, scope, options) {
292
- const prevLocale = this.i18n.locale;
293
- this.i18n.locale = locale;
294
- try {
295
- return this.i18n.t(scope, options);
296
- } finally {
297
- this.i18n.locale = prevLocale;
298
- }
299
- }
300
- /** 将 locale 及其自动降级(如 en-US → en)追加到 chain 中 */
301
- pushWithDegradation(chain, locale) {
302
- const normalized = this.normalizeLocaleTag(locale);
303
- chain.push(normalized);
304
- const parts = normalized.split("-").filter(Boolean);
305
- for (let i = parts.length - 1; i >= 1; i -= 1) {
306
- chain.push(parts.slice(0, i).join("-"));
307
- }
308
- }
309
- getLocaleChain(locale) {
310
- const cached = this.localeChainCache.get(locale);
311
- if (cached) return cached;
312
- const chain = [];
313
- this.pushWithDegradation(chain, locale);
314
- const normalizedLocale = this.normalizeLocaleTag(locale);
315
- const extra = this.fallbackLocales ? typeof this.fallbackLocales === "function" ? this.fallbackLocales(normalizedLocale) : this.fallbackLocales : [];
316
- for (const l of extra) {
317
- this.pushWithDegradation(chain, l);
318
- }
319
- if (this.i18n.defaultLocale) {
320
- this.pushWithDegradation(chain, this.i18n.defaultLocale);
321
- }
322
- const seen = /* @__PURE__ */ new Set();
323
- const result = chain.filter((l) => {
324
- if (!l || seen.has(l)) return false;
325
- seen.add(l);
326
- return true;
327
- });
328
- this.localeChainCache.set(locale, result);
329
- return result;
330
- }
331
- hasTranslation(locale, key) {
332
- var _a;
333
- const table = (_a = this.i18n.translations) == null ? void 0 : _a[locale];
334
- if (!table || typeof table !== "object") return false;
335
- if (!key.includes(".") && Object.prototype.hasOwnProperty.call(table, key)) {
336
- const direct = table[key];
337
- return direct !== null && direct !== void 0;
338
- }
339
- const parts = key.split(".").filter(Boolean);
340
- let node = table;
341
- for (const part of parts) {
342
- if (!node || typeof node !== "object" || !(part in node)) return false;
343
- node = node[part];
344
- }
345
- return node !== null && node !== void 0;
346
- }
347
- normalizeTranslateResult(value) {
348
- if (typeof value === "string") return value;
349
- if (typeof value === "number") return String(value);
350
- return void 0;
351
- }
352
- normalizeLocaleTag(tag) {
353
- return tag.replace(/_/g, "-");
354
- }
355
- normalizeTranslations(translations) {
356
- const normalized = {};
357
- for (const key of Object.keys(translations)) {
358
- normalized[this.normalizeLocaleTag(key)] = translations[key];
359
- }
360
- return normalized;
361
- }
362
- isRTLLocale(locale) {
363
- var _a;
364
- const normalized = this.normalizeLocaleTag(locale);
365
- const parts = normalized.split("-").filter(Boolean);
366
- const languageCode = (_a = parts[0]) == null ? void 0 : _a.toLowerCase();
367
- if (!languageCode) return false;
368
- const scriptSubtag = parts.find((part) => /^[A-Za-z]{4}$/.test(part));
369
- if (scriptSubtag) {
370
- const script = scriptSubtag[0].toUpperCase() + scriptSubtag.slice(1).toLowerCase();
371
- return RTL_SCRIPTS.has(script);
372
- }
373
- return RTL_LANGUAGE_CODES.has(languageCode);
374
- }
375
- };
376
- var i18nService = new DefaultI18nEngine();
377
- var DEFAULT_I18N_CONTEXT = {
378
- locale: i18nService.getLocale(),
379
- setLocale: (locale) => i18nService.setLocale(locale),
380
- t: (scope, options) => i18nService.t(scope, options),
381
- formatNumber: (n, o) => i18nService.formatNumber(n, o),
382
- formatCurrency: (n, c, o) => i18nService.formatCurrency(n, c, o),
383
- formatDate: (d, o) => i18nService.formatDate(d, o)
384
- };
385
- var I18nContext = React.createContext(DEFAULT_I18N_CONTEXT);
386
- var I18nProvider = ({ children, readyGate, fallback }) => {
387
- const [locale, setLocaleState] = React.useState(i18nService.getLocale());
388
- const [version, setVersion] = React.useState(0);
389
- const [ready, setReady] = React.useState(i18nService.isReady());
390
- React.useEffect(() => {
391
- const unsubscribe = i18nService.subscribe((newLocale, change) => {
392
- setLocaleState(newLocale);
393
- setVersion((prev) => {
394
- var _a;
395
- return (_a = change == null ? void 0 : change.version) != null ? _a : prev + 1;
396
- });
397
- });
398
- return () => unsubscribe();
399
- }, []);
400
- React.useEffect(() => {
401
- var _a, _b;
402
- const handler = (state) => {
403
- if (state === "active") {
404
- i18nService.updateLocale();
405
- }
406
- };
407
- const subscription = (_b = (_a = reactNative.AppState) == null ? void 0 : _a.addEventListener) == null ? void 0 : _b.call(_a, "change", handler);
408
- return () => {
409
- var _a2, _b2;
410
- if (typeof (subscription == null ? void 0 : subscription.remove) === "function") {
411
- subscription.remove();
412
- return;
413
- }
414
- (_b2 = (_a2 = reactNative.AppState) == null ? void 0 : _a2.removeEventListener) == null ? void 0 : _b2.call(_a2, "change", handler);
415
- };
416
- }, []);
417
- React.useEffect(() => {
418
- if (!readyGate) return;
419
- let cancelled = false;
420
- i18nService.ready().then(() => {
421
- if (!cancelled) setReady(true);
422
- });
423
- return () => {
424
- cancelled = true;
425
- };
426
- }, [readyGate]);
427
- const value = React.useMemo(() => ({
428
- locale,
429
- setLocale: (l) => i18nService.setLocale(l),
430
- t: (scope, options) => i18nService.t(scope, options),
431
- formatNumber: (n, o) => i18nService.formatNumber(n, o),
432
- formatCurrency: (n, c, o) => i18nService.formatCurrency(n, c, o),
433
- formatDate: (d, o) => i18nService.formatDate(d, o)
434
- }), [locale, version]);
435
- if (readyGate && !ready) return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, fallback != null ? fallback : null);
436
- return /* @__PURE__ */ React__default.default.createElement(I18nContext.Provider, { value }, children);
437
- };
438
- function withI18n(Component) {
439
- return function WithI18n(props) {
440
- return /* @__PURE__ */ React__default.default.createElement(I18nContext.Consumer, null, (context) => {
441
- if (context === DEFAULT_I18N_CONTEXT) {
442
- throw new Error("withI18n must be used within an I18nProvider");
443
- }
444
- return /* @__PURE__ */ React__default.default.createElement(Component, { ...props, ...context });
445
- });
446
- };
447
- }
448
- function useI18n() {
449
- const context = React.useContext(I18nContext);
450
- if (context === DEFAULT_I18N_CONTEXT) {
451
- throw new Error("useI18n must be used within an I18nProvider");
452
- }
453
- return React.useMemo(() => {
454
- const t2 = (scope, options) => {
455
- return context.t(scope, options);
456
- };
457
- return {
458
- ...context,
459
- t: t2
460
- };
461
- }, [context]);
462
- }
463
- var escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
464
- var TAG_REGEX = /<(\/?)([A-Za-z][\w-]*)(?:\s[^>]*?)?(\/?)>/g;
465
- var parseString = (input) => {
466
- var _a;
467
- const root = { type: "tag", name: "root", children: [] };
468
- const stack = [root];
469
- let lastIndex = 0;
470
- for (const match of input.matchAll(TAG_REGEX)) {
471
- const fullMatch = match[0];
472
- const isClosing = match[1] === "/";
473
- const tagName = match[2];
474
- const isSelfClosing = match[3] === "/" || fullMatch.endsWith("/>");
475
- const index = (_a = match.index) != null ? _a : 0;
476
- if (index > lastIndex) {
477
- const text = input.slice(lastIndex, index);
478
- if (text) stack[stack.length - 1].children.push({ type: "text", content: text });
479
- }
480
- if (isClosing) {
481
- if (stack.length > 1 && stack[stack.length - 1].name === tagName) {
482
- stack.pop();
483
- } else {
484
- stack[stack.length - 1].children.push({ type: "text", content: fullMatch });
485
- }
486
- } else if (isSelfClosing) {
487
- stack[stack.length - 1].children.push({ type: "tag", name: tagName, children: [] });
488
- } else {
489
- const node = { type: "tag", name: tagName, children: [] };
490
- stack[stack.length - 1].children.push(node);
491
- stack.push(node);
492
- }
493
- lastIndex = index + fullMatch.length;
494
- }
495
- if (lastIndex < input.length) {
496
- stack[stack.length - 1].children.push({ type: "text", content: input.slice(lastIndex) });
497
- }
498
- return root.children;
499
- };
500
- var renderAST = (nodes, keyPrefix, components, placeholderToElement) => {
501
- return nodes.map((node, index) => {
502
- const key = `${keyPrefix}-${index}`;
503
- if (node.type === "text") {
504
- const placeholders = Object.keys(placeholderToElement);
505
- if (placeholders.length === 0) return node.content;
506
- const pattern = new RegExp(`(${placeholders.map(escapeRegExp).join("|")})`, "g");
507
- const parts = node.content.split(pattern);
508
- if (parts.length === 1) return node.content;
509
- return /* @__PURE__ */ React__default.default.createElement(React.Fragment, { key }, parts.map((part, i) => {
510
- if (placeholderToElement[part]) {
511
- return React.cloneElement(placeholderToElement[part], { key: `${key}-${i}` });
512
- }
513
- return part;
514
- }));
515
- } else if (node.type === "tag") {
516
- const Component = components == null ? void 0 : components[node.name];
517
- const children = renderAST(node.children, key, components, placeholderToElement);
518
- if (React.isValidElement(Component)) {
519
- return React.cloneElement(Component, { key }, children.length > 0 ? children : void 0);
520
- }
521
- return /* @__PURE__ */ React__default.default.createElement(React.Fragment, { key }, children);
522
- }
523
- return null;
524
- });
525
- };
526
- var Trans = ({ i18nKey, values, components, ...props }) => {
527
- const { t: t2 } = useI18n();
528
- const { stringValues, placeholderToElement } = React.useMemo(() => {
529
- const sv = {};
530
- const pte = {};
531
- if (values) {
532
- for (const key of Object.keys(values)) {
533
- const value = values[key];
534
- if (React.isValidElement(value)) {
535
- const placeholder = `__ELEMENT_${key}__`;
536
- pte[placeholder] = value;
537
- sv[key] = placeholder;
538
- } else {
539
- sv[key] = value == null ? "" : String(value);
540
- }
541
- }
542
- }
543
- return { stringValues: sv, placeholderToElement: pte };
544
- }, [values]);
545
- const translatedText = t2(i18nKey, stringValues);
546
- const ast = React.useMemo(() => parseString(translatedText), [translatedText]);
547
- const children = React.useMemo(
548
- () => renderAST(ast, "trans", components, placeholderToElement),
549
- [ast, components, placeholderToElement]
550
- );
551
- return /* @__PURE__ */ React__default.default.createElement(reactNative.Text, { ...props }, children);
552
- };
553
-
554
- // src/index.ts
555
- var initI18n = (translations, options) => i18nService.init(translations, options);
556
- var loadTranslations = (translations) => i18nService.loadTranslations(translations);
557
- var setLocale = (locale) => i18nService.setLocale(locale);
558
- var getLocale = () => i18nService.getLocale();
559
- var t = (scope, options) => i18nService.t(scope, options);
560
- var formatNumber = (n, options) => i18nService.formatNumber(n, options);
561
- var formatCurrency = (n, currency, options) => i18nService.formatCurrency(n, currency, options);
562
- var formatDate = (date, options) => i18nService.formatDate(date, options);
563
- var subscribe = (listener) => i18nService.subscribe(listener);
564
- var isRTL = () => i18nService.isRTL();
565
- var resetToSystem = () => i18nService.resetToSystem();
566
- var readyI18n = () => i18nService.ready();
567
- var isI18nReady = () => i18nService.isReady();
568
- var index_default = i18nService;
569
-
570
- exports.I18nContext = I18nContext;
571
- exports.I18nProvider = I18nProvider;
572
- exports.Trans = Trans;
573
- exports.default = index_default;
574
- exports.formatCurrency = formatCurrency;
575
- exports.formatDate = formatDate;
576
- exports.formatNumber = formatNumber;
577
- exports.getLocale = getLocale;
578
- exports.initI18n = initI18n;
579
- exports.isI18nReady = isI18nReady;
580
- exports.isRTL = isRTL;
581
- exports.loadTranslations = loadTranslations;
582
- exports.readyI18n = readyI18n;
583
- exports.resetToSystem = resetToSystem;
584
- exports.setLocale = setLocale;
585
- exports.subscribe = subscribe;
586
- exports.t = t;
587
- exports.useI18n = useI18n;
588
- exports.withI18n = withI18n;
1
+ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});var i18nJs=require('i18n-js'),R=require('react-native-localize'),reactNative=require('react-native'),T=require('react');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var R__namespace=/*#__PURE__*/_interopNamespace(R);var T__default=/*#__PURE__*/_interopDefault(T);var E=Object.defineProperty;var M=(n,t,e)=>t in n?E(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e;var h=(n,t,e)=>M(n,typeof t!="symbol"?t+"":t,e);var K=new Set(["ar","fa","he","iw","ur","ps","dv","ku","ug","yi","sd"]),_=new Set(["Arab","Hebr","Thaa","Syrc","Nkoo","Adlm"]),d=n=>n.replace(/_/g,"-"),x=class{constructor(){h(this,"i",new i18nJs.I18n);h(this,"ls",new Set);h(this,"cb");h(this,"fb");h(this,"mb","key");h(this,"mk");h(this,"ef",true);h(this,"fs",true);h(this,"sr","system");h(this,"ok",false);h(this,"v",0);h(this,"rp");h(this,"rr");h(this,"nc",new Map);h(this,"dc",new Map);h(this,"lc",new Map);this.i.enableFallback=false,this.rp=new Promise(t=>{this.rr=t;});}init(t,e={}){var a;let{defaultLocale:r="en",enableFallback:s=true,onLocaleChange:l,fallbackLocales:o,followSystem:p=true,missingBehavior:f="key",onMissingKey:m}=e;this.i.store(this.nt(t)),this.i.defaultLocale=this.i.locale=d(r),this.ef=s,this.cb=l,this.fb=o,this.fs=p,this.sr="system",this.mb=f,this.mk=m,this.cl(),p&&this.updateLocale(),this.em("translations"),this.ok||(this.ok=true,(a=this.rr)==null||a.call(this),this.rr=void 0);}loadTranslations(t){this.i.store(this.nt(t)),this.em("translations");}updateLocale(){var t,e;if(!(!this.fs||this.sr==="user"))try{let r=(e=(t=R__namespace.getLocales())==null?void 0:t[0])==null?void 0:e.languageTag;r&&(this.sr="system",this.ap(r));}catch{}}setLocale(t){this.sr="user",this.ap(t);}getLocale(){return this.i.locale}resetToSystem(){this.sr="system",this.updateLocale();}t(t,e){var l,o,p;if(typeof t!="string")return (l=this.nr(this.i.t(t,e)))!=null?l:"";let r=this.i.locale,s=this.ef?this.gc(r):[r];for(let f of s)if(this.ht(f,t)){let m=this.nr(this.ta(f,t,e));if(m!==void 0)return m}if((o=this.mk)==null||o.call(this,t,r),e&&Object.prototype.hasOwnProperty.call(e,"defaultValue"))return (p=this.nr(this.ta(r,t,e)))!=null?p:"";if(this.mb==="empty")return "";if(this.mb==="throw")throw new Error(`Missing translation: ${r}.${t}`);return t}formatNumber(t,e){if(typeof Intl=="undefined"||!Intl.NumberFormat)return String(t);try{return this.gn(this.i.locale,e).format(t)}catch{return String(t)}}formatCurrency(t,e,r){if(typeof Intl=="undefined"||!Intl.NumberFormat)return `${t} ${e}`;try{return this.gn(this.i.locale,{...r,style:"currency",currency:e}).format(t)}catch{return `${t} ${e}`}}formatDate(t,e){let r=typeof t=="number"?new Date(t):t;if(Number.isNaN(r.getTime()))return "";if(typeof Intl=="undefined"||!Intl.DateTimeFormat)return r.toISOString();try{return this.gd(this.i.locale,e).format(r)}catch{return r.toISOString()}}subscribe(t){return this.ls.add(t),()=>{this.ls.delete(t);}}isRTL(){return reactNative.I18nManager.isRTL}ready(){return this.ok?Promise.resolve():this.rp}isReady(){return this.ok}cl(){this.nc.clear(),this.dc.clear(),this.lc.clear();}gn(t,e){let r=t+(e?JSON.stringify(e):""),s=this.nc.get(r);return s||(s=new Intl.NumberFormat(t,e),this.nc.set(r,s)),s}gd(t,e){let r=t+(e?JSON.stringify(e):""),s=this.dc.get(r);return s||(s=new Intl.DateTimeFormat(t,e),this.dc.set(r,s)),s}em(t){this.v++,this.ls.forEach(e=>e(this.i.locale,{type:t,version:this.v}));}ap(t){var r;let e=d(t);this.i.locale!==e&&(this.i.locale=e,this.cl(),this.hr(e),this.em("locale"),(r=this.cb)==null||r.call(this,e));}hr(t){var p;let e=d(t).split("-").filter(Boolean),r=(p=e[0])==null?void 0:p.toLowerCase();if(!r)return;let s=e.find(f=>/^[A-Za-z]{4}$/.test(f)),l=s?_.has(s[0].toUpperCase()+s.slice(1).toLowerCase()):K.has(r),o=reactNative.I18nManager;typeof(o==null?void 0:o.allowRTL)=="function"&&typeof(o==null?void 0:o.forceRTL)=="function"&&reactNative.I18nManager.isRTL!==l&&(o.allowRTL(l),o.forceRTL(l));}ta(t,e,r){let s=this.i.locale;this.i.locale=t;try{return this.i.t(e,r)}finally{this.i.locale=s;}}dg(t,e){let r=d(e);t.push(r);let s=r.split("-").filter(Boolean);for(let l=s.length-1;l>=1;l--)t.push(s.slice(0,l).join("-"));}gc(t){let e=this.lc.get(t);if(e)return e;let r=[];this.dg(r,t);let s=this.fb?typeof this.fb=="function"?this.fb(d(t)):this.fb:[];for(let p of s)this.dg(r,p);this.i.defaultLocale&&this.dg(r,this.i.defaultLocale);let l=new Set,o=r.filter(p=>p&&!l.has(p)&&(l.add(p),true));return this.lc.set(t,o),o}ht(t,e){var l;let r=(l=this.i.translations)==null?void 0:l[t];if(!r||typeof r!="object")return false;if(!e.includes(".")&&Object.prototype.hasOwnProperty.call(r,e))return r[e]!=null;let s=r;for(let o of e.split(".").filter(Boolean)){if(!s||typeof s!="object"||!(o in s))return false;s=s[o];}return s!=null}nr(t){return typeof t=="string"?t:typeof t=="number"?String(t):void 0}nt(t){let e={};for(let r of Object.keys(t))e[d(r)]=t[r];return e}},i=new x;var N={locale:i.getLocale(),setLocale:n=>i.setLocale(n),t:(n,t)=>i.t(n,t),formatNumber:(n,t)=>i.formatNumber(n,t),formatCurrency:(n,t,e)=>i.formatCurrency(n,t,e),formatDate:(n,t)=>i.formatDate(n,t)},b=T.createContext(N),V=({children:n,readyGate:t,fallback:e})=>{let[r,s]=T.useState(i.getLocale()),[l,o]=T.useState(0),[p,f]=T.useState(i.isReady());T.useEffect(()=>i.subscribe((a,c)=>{s(a),o(u=>{var g;return (g=c==null?void 0:c.version)!=null?g:u+1});}),[]),T.useEffect(()=>{var u,g;let a=y=>{y==="active"&&i.updateLocale();},c=(g=(u=reactNative.AppState)==null?void 0:u.addEventListener)==null?void 0:g.call(u,"change",a);return ()=>{var y,C;typeof(c==null?void 0:c.remove)=="function"?c.remove():(C=(y=reactNative.AppState)==null?void 0:y.removeEventListener)==null||C.call(y,"change",a);}},[]),T.useEffect(()=>{if(!t)return;let a=false;return i.ready().then(()=>{a||f(true);}),()=>{a=true;}},[t]);let m=T.useMemo(()=>({locale:r,setLocale:a=>i.setLocale(a),t:(a,c)=>i.t(a,c),formatNumber:(a,c)=>i.formatNumber(a,c),formatCurrency:(a,c,u)=>i.formatCurrency(a,c,u),formatDate:(a,c)=>i.formatDate(a,c)}),[r,l]);return t&&!p?T__default.default.createElement(T__default.default.Fragment,null,e!=null?e:null):T__default.default.createElement(b.Provider,{value:m},n)};function w(){let n=T.useContext(b);if(n===N)throw new Error("useI18n must be used within an I18nProvider");return T.useMemo(()=>({...n,t:((t,e)=>n.t(t,e))}),[n])}function J(n){return t=>T__default.default.createElement(b.Consumer,null,e=>{if(e===N)throw new Error("withI18n must be used within an I18nProvider");return T__default.default.createElement(n,{...t,...e})})}var H=/<(\/?)([A-Za-z][\w-]*)(?:\s[^>]*?)?(\/?)>/g,U=n=>n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),W=n=>{var s;let t={t:"g",n:"",ch:[]},e=[t],r=0;for(let l of n.matchAll(H)){let[o,p,f,m]=l,a=(s=l.index)!=null?s:0;if(a>r){let c=n.slice(r,a);c&&e[e.length-1].ch.push({t:"x",c});}if(p==="/")e.length>1&&e[e.length-1].n===f?e.pop():e[e.length-1].ch.push({t:"x",c:o});else if(m==="/"||o.endsWith("/>"))e[e.length-1].ch.push({t:"g",n:f,ch:[]});else {let c={t:"g",n:f,ch:[]};e[e.length-1].ch.push(c),e.push(c);}r=a+o.length;}return r<n.length&&e[e.length-1].ch.push({t:"x",c:n.slice(r)}),t.ch},j=(n,t,e,r={})=>n.map((s,l)=>{let o=`${t}-${l}`;if(s.t==="x"){let m=Object.keys(r);if(!m.length)return s.c;let a=s.c.split(new RegExp(`(${m.map(U).join("|")})`,"g"));return a.length===1?s.c:T__default.default.createElement(T.Fragment,{key:o},a.map((c,u)=>r[c]?T.cloneElement(r[c],{key:`${o}-${u}`}):c))}let p=e==null?void 0:e[s.n],f=j(s.ch,o,e,r);return T.isValidElement(p)?T.cloneElement(p,{key:o},f.length?f:void 0):T__default.default.createElement(T.Fragment,{key:o},f)}),q=({i18nKey:n,values:t,components:e,...r})=>{let{t:s}=w(),{sv:l,pe:o}=T.useMemo(()=>{let a={},c={};if(t)for(let u of Object.keys(t)){let g=t[u];if(T.isValidElement(g)){let y=`__E_${u}__`;c[y]=g,a[u]=y;}else a[u]=g==null?"":String(g);}return {sv:a,pe:c}},[t]),p=s(n,l),f=T.useMemo(()=>W(p),[p]),m=T.useMemo(()=>j(f,"tr",e,o),[f,e,o]);return T__default.default.createElement(reactNative.Text,{...r},m)};var bt=(n,t)=>i.init(n,t),vt=n=>i.loadTranslations(n),xt=n=>i.setLocale(n),It=()=>i.getLocale(),Lt=(n,t)=>i.t(n,t),Nt=(n,t)=>i.formatNumber(n,t),wt=(n,t,e)=>i.formatCurrency(n,t,e),Ot=(n,t)=>i.formatDate(n,t),kt=n=>i.subscribe(n),Ct=()=>i.isRTL(),Rt=()=>i.resetToSystem(),St=()=>i.ready(),Ft=()=>i.isReady(),Pt=i;exports.I18nContext=b;exports.I18nProvider=V;exports.Trans=q;exports.default=Pt;exports.formatCurrency=wt;exports.formatDate=Ot;exports.formatNumber=Nt;exports.getLocale=It;exports.initI18n=bt;exports.isI18nReady=Ft;exports.isRTL=Ct;exports.loadTranslations=vt;exports.readyI18n=St;exports.resetToSystem=Rt;exports.setLocale=xt;exports.subscribe=kt;exports.t=Lt;exports.useI18n=w;exports.withI18n=J;