react-i18next 16.1.6 → 16.2.1

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/react-i18next.js CHANGED
@@ -475,7 +475,12 @@
475
475
  };
476
476
  if (key == null) return false;
477
477
  const resolved = this.resolve(key, opt);
478
- return resolved?.res !== undefined;
478
+ if (resolved?.res === undefined) return false;
479
+ const isObject = shouldHandleAsObject(resolved.res);
480
+ if (opt.returnObjects === false && isObject) {
481
+ return false;
482
+ }
483
+ return true;
479
484
  }
480
485
  extractFromKey(key, opt) {
481
486
  let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
@@ -3270,15 +3275,102 @@
3270
3275
  }
3271
3276
  IcuTrans.displayName = 'IcuTrans';
3272
3277
 
3273
- const usePrevious = (value, ignore) => {
3274
- const ref = React.useRef();
3275
- React.useEffect(() => {
3276
- ref.current = value;
3277
- }, [value, ignore]);
3278
- return ref.current;
3278
+ var shim = {exports: {}};
3279
+
3280
+ var useSyncExternalStoreShim_development = {};
3281
+
3282
+ /**
3283
+ * @license React
3284
+ * use-sync-external-store-shim.development.js
3285
+ *
3286
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3287
+ *
3288
+ * This source code is licensed under the MIT license found in the
3289
+ * LICENSE file in the root directory of this source tree.
3290
+ */
3291
+
3292
+ (function () {
3293
+ function is(x, y) {
3294
+ return x === y && (0 !== x || 1 / x === 1 / y) || x !== x && y !== y;
3295
+ }
3296
+ function useSyncExternalStore$2(subscribe, getSnapshot) {
3297
+ didWarnOld18Alpha || void 0 === React$1.startTransition || (didWarnOld18Alpha = true, console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));
3298
+ var value = getSnapshot();
3299
+ if (!didWarnUncachedGetSnapshot) {
3300
+ var cachedValue = getSnapshot();
3301
+ objectIs(value, cachedValue) || (console.error("The result of getSnapshot should be cached to avoid an infinite loop"), didWarnUncachedGetSnapshot = true);
3302
+ }
3303
+ cachedValue = useState({
3304
+ inst: {
3305
+ value: value,
3306
+ getSnapshot: getSnapshot
3307
+ }
3308
+ });
3309
+ var inst = cachedValue[0].inst,
3310
+ forceUpdate = cachedValue[1];
3311
+ useLayoutEffect(function () {
3312
+ inst.value = value;
3313
+ inst.getSnapshot = getSnapshot;
3314
+ checkIfSnapshotChanged(inst) && forceUpdate({
3315
+ inst: inst
3316
+ });
3317
+ }, [subscribe, value, getSnapshot]);
3318
+ useEffect(function () {
3319
+ checkIfSnapshotChanged(inst) && forceUpdate({
3320
+ inst: inst
3321
+ });
3322
+ return subscribe(function () {
3323
+ checkIfSnapshotChanged(inst) && forceUpdate({
3324
+ inst: inst
3325
+ });
3326
+ });
3327
+ }, [subscribe]);
3328
+ useDebugValue(value);
3329
+ return value;
3330
+ }
3331
+ function checkIfSnapshotChanged(inst) {
3332
+ var latestGetSnapshot = inst.getSnapshot;
3333
+ inst = inst.value;
3334
+ try {
3335
+ var nextValue = latestGetSnapshot();
3336
+ return !objectIs(inst, nextValue);
3337
+ } catch (error) {
3338
+ return true;
3339
+ }
3340
+ }
3341
+ function useSyncExternalStore$1(subscribe, getSnapshot) {
3342
+ return getSnapshot();
3343
+ }
3344
+ "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ && "function" === typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
3345
+ var React$1 = React,
3346
+ objectIs = "function" === typeof Object.is ? Object.is : is,
3347
+ useState = React$1.useState,
3348
+ useEffect = React$1.useEffect,
3349
+ useLayoutEffect = React$1.useLayoutEffect,
3350
+ useDebugValue = React$1.useDebugValue,
3351
+ didWarnOld18Alpha = false,
3352
+ didWarnUncachedGetSnapshot = false,
3353
+ shim = "undefined" === typeof window || "undefined" === typeof window.document || "undefined" === typeof window.document.createElement ? useSyncExternalStore$1 : useSyncExternalStore$2;
3354
+ useSyncExternalStoreShim_development.useSyncExternalStore = void 0 !== React$1.useSyncExternalStore ? React$1.useSyncExternalStore : shim;
3355
+ "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ && "function" === typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
3356
+ })();
3357
+
3358
+ {
3359
+ shim.exports = useSyncExternalStoreShim_development;
3360
+ }
3361
+
3362
+ var shimExports = shim.exports;
3363
+
3364
+ const notReadyT = (k, optsOrDefaultValue) => {
3365
+ if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
3366
+ if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue;
3367
+ return Array.isArray(k) ? k[k.length - 1] : k;
3368
+ };
3369
+ const notReadySnapshot = {
3370
+ t: notReadyT,
3371
+ ready: false
3279
3372
  };
3280
- const alwaysNewT = (i18n, language, namespace, keyPrefix) => i18n.getFixedT(language, namespace, keyPrefix);
3281
- const useMemoizedT = (i18n, language, namespace, keyPrefix) => React.useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
3373
+ const dummySubscribe = () => () => {};
3282
3374
  const useTranslation = (ns, props = {}) => {
3283
3375
  const {
3284
3376
  i18n: i18nFromProps
@@ -3291,88 +3383,96 @@
3291
3383
  if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
3292
3384
  if (!i18n) {
3293
3385
  warnOnce(i18n, 'NO_I18NEXT_INSTANCE', 'useTranslation: You will need to pass in an i18next instance by using initReactI18next');
3294
- const notReadyT = (k, optsOrDefaultValue) => {
3295
- if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
3296
- if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue;
3297
- return Array.isArray(k) ? k[k.length - 1] : k;
3298
- };
3299
- const retNotReady = [notReadyT, {}, false];
3300
- retNotReady.t = notReadyT;
3301
- retNotReady.i18n = {};
3302
- retNotReady.ready = false;
3303
- return retNotReady;
3304
- }
3305
- if (i18n.options.react?.wait) warnOnce(i18n, 'DEPRECATED_OPTION', 'useTranslation: It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
3306
- const i18nOptions = {
3386
+ }
3387
+ const i18nOptions = React.useMemo(() => ({
3307
3388
  ...getDefaults(),
3308
- ...i18n.options.react,
3389
+ ...i18n?.options?.react,
3309
3390
  ...props
3310
- };
3391
+ }), [i18n, props]);
3311
3392
  const {
3312
3393
  useSuspense,
3313
3394
  keyPrefix
3314
3395
  } = i18nOptions;
3315
- let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
3316
- namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
3317
- i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
3318
- const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
3319
- const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
3320
- const getT = () => memoGetT;
3321
- const getNewT = () => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
3322
- const [t, setT] = React.useState(getT);
3323
- let joinedNS = namespaces.join();
3324
- if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
3325
- const previousJoinedNS = usePrevious(joinedNS);
3326
- const isMounted = React.useRef(true);
3327
- React.useEffect(() => {
3396
+ const namespaces = React.useMemo(() => {
3397
+ const nsOrContext = ns || defaultNSFromContext || i18n?.options?.defaultNS;
3398
+ return isString(nsOrContext) ? [nsOrContext] : nsOrContext || ['translation'];
3399
+ }, [ns, defaultNSFromContext, i18n]);
3400
+ i18n?.reportNamespaces?.addUsedNamespaces?.(namespaces);
3401
+ const revisionRef = React.useRef(0);
3402
+ const subscribe = React.useCallback(callback => {
3403
+ if (!i18n) return dummySubscribe;
3328
3404
  const {
3329
3405
  bindI18n,
3330
3406
  bindI18nStore
3331
3407
  } = i18nOptions;
3332
- isMounted.current = true;
3333
- if (!ready && !useSuspense) {
3334
- if (props.lng) {
3335
- loadLanguages(i18n, props.lng, namespaces, () => {
3336
- if (isMounted.current) setT(getNewT);
3337
- });
3338
- } else {
3339
- loadNamespaces(i18n, namespaces, () => {
3340
- if (isMounted.current) setT(getNewT);
3341
- });
3342
- }
3343
- }
3344
- if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
3345
- setT(getNewT);
3346
- }
3347
- const boundReset = () => {
3348
- if (isMounted.current) setT(getNewT);
3408
+ const wrappedCallback = () => {
3409
+ revisionRef.current += 1;
3410
+ callback();
3349
3411
  };
3350
- if (bindI18n) i18n?.on(bindI18n, boundReset);
3351
- if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
3412
+ if (bindI18n) i18n.on(bindI18n, wrappedCallback);
3413
+ if (bindI18nStore) i18n.store.on(bindI18nStore, wrappedCallback);
3352
3414
  return () => {
3353
- isMounted.current = false;
3354
- if (i18n && bindI18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset));
3355
- if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
3415
+ if (bindI18n) bindI18n.split(' ').forEach(e => i18n.off(e, wrappedCallback));
3416
+ if (bindI18nStore) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, wrappedCallback));
3417
+ };
3418
+ }, [i18n, i18nOptions]);
3419
+ const snapshotRef = React.useRef();
3420
+ const getSnapshot = React.useCallback(() => {
3421
+ if (!i18n) {
3422
+ return notReadySnapshot;
3423
+ }
3424
+ const calculatedReady = !!(i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
3425
+ const currentLng = props.lng || i18n.language;
3426
+ const currentRevision = revisionRef.current;
3427
+ const lastSnapshot = snapshotRef.current;
3428
+ if (lastSnapshot && lastSnapshot.ready === calculatedReady && lastSnapshot.lng === currentLng && lastSnapshot.keyPrefix === keyPrefix && lastSnapshot.revision === currentRevision) {
3429
+ return lastSnapshot;
3430
+ }
3431
+ const calculatedT = i18n.getFixedT(currentLng, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
3432
+ const newSnapshot = {
3433
+ t: calculatedT,
3434
+ ready: calculatedReady,
3435
+ lng: currentLng,
3436
+ keyPrefix,
3437
+ revision: currentRevision
3356
3438
  };
3357
- }, [i18n, joinedNS]);
3439
+ snapshotRef.current = newSnapshot;
3440
+ return newSnapshot;
3441
+ }, [i18n, namespaces, keyPrefix, i18nOptions, props.lng]);
3442
+ const [loadCount, setLoadCount] = React.useState(0);
3443
+ const {
3444
+ t,
3445
+ ready
3446
+ } = shimExports.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
3358
3447
  React.useEffect(() => {
3359
- if (isMounted.current && ready) {
3360
- setT(getT);
3361
- }
3362
- }, [i18n, keyPrefix, ready]);
3363
- const ret = [t, i18n, ready];
3364
- ret.t = t;
3365
- ret.i18n = i18n;
3366
- ret.ready = ready;
3367
- if (ready) return ret;
3368
- if (!ready && !useSuspense) return ret;
3369
- throw new Promise(resolve => {
3370
- if (props.lng) {
3371
- loadLanguages(i18n, props.lng, namespaces, () => resolve());
3372
- } else {
3373
- loadNamespaces(i18n, namespaces, () => resolve());
3448
+ if (i18n && !ready && !useSuspense) {
3449
+ const onLoaded = () => setLoadCount(c => c + 1);
3450
+ if (props.lng) {
3451
+ loadLanguages(i18n, props.lng, namespaces, onLoaded);
3452
+ } else {
3453
+ loadNamespaces(i18n, namespaces, onLoaded);
3454
+ }
3374
3455
  }
3375
- });
3456
+ }, [i18n, props.lng, namespaces, ready, useSuspense, loadCount]);
3457
+ const finalI18n = i18n || {};
3458
+ const ret = React.useMemo(() => {
3459
+ const arr = [t, finalI18n, ready];
3460
+ arr.t = t;
3461
+ arr.i18n = finalI18n;
3462
+ arr.ready = ready;
3463
+ return arr;
3464
+ }, [t, finalI18n, ready]);
3465
+ if (i18n && useSuspense && !ready) {
3466
+ throw new Promise(resolve => {
3467
+ const onLoaded = () => resolve();
3468
+ if (props.lng) {
3469
+ loadLanguages(i18n, props.lng, namespaces, onLoaded);
3470
+ } else {
3471
+ loadNamespaces(i18n, namespaces, onLoaded);
3472
+ }
3473
+ });
3474
+ }
3475
+ return ret;
3376
3476
  };
3377
3477
 
3378
3478
  const withTranslation = (ns, options = {}) => function Extend(WrappedComponent) {