react-i18next 16.2.0 → 16.2.2

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.
@@ -50,15 +50,23 @@ export const useTranslation = (ns, props = {}) => {
50
50
 
51
51
  i18n?.reportNamespaces?.addUsedNamespaces?.(namespaces);
52
52
 
53
+ const revisionRef = useRef(0);
53
54
  const subscribe = useCallback(
54
55
  (callback) => {
55
56
  if (!i18n) return dummySubscribe;
56
57
  const { bindI18n, bindI18nStore } = i18nOptions;
57
- if (bindI18n) i18n.on(bindI18n, callback);
58
- if (bindI18nStore) i18n.store.on(bindI18nStore, callback);
58
+
59
+ const wrappedCallback = () => {
60
+ revisionRef.current += 1;
61
+ callback();
62
+ };
63
+
64
+ if (bindI18n) i18n.on(bindI18n, wrappedCallback);
65
+ if (bindI18nStore) i18n.store.on(bindI18nStore, wrappedCallback);
59
66
  return () => {
60
- if (bindI18n) bindI18n.split(' ').forEach((e) => i18n.off(e, callback));
61
- if (bindI18nStore) bindI18nStore.split(' ').forEach((e) => i18n.store.off(e, callback));
67
+ if (bindI18n) bindI18n.split(' ').forEach((e) => i18n.off(e, wrappedCallback));
68
+ if (bindI18nStore)
69
+ bindI18nStore.split(' ').forEach((e) => i18n.store.off(e, wrappedCallback));
62
70
  };
63
71
  },
64
72
  [i18n, i18nOptions],
@@ -72,25 +80,32 @@ export const useTranslation = (ns, props = {}) => {
72
80
  const calculatedReady =
73
81
  !!(i18n.isInitialized || i18n.initializedStoreOnce) &&
74
82
  namespaces.every((n) => hasLoadedNamespace(n, i18n, i18nOptions));
75
- const calculatedT = i18n.getFixedT(
76
- props.lng || i18n.language,
77
- i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0],
78
- keyPrefix,
79
- );
83
+ const currentLng = props.lng || i18n.language;
84
+ const currentRevision = revisionRef.current;
85
+
80
86
  const lastSnapshot = snapshotRef.current;
81
87
  if (
82
88
  lastSnapshot &&
83
89
  lastSnapshot.ready === calculatedReady &&
84
- lastSnapshot.lng === (props.lng || i18n.language) &&
85
- lastSnapshot.keyPrefix === keyPrefix
90
+ lastSnapshot.lng === currentLng &&
91
+ lastSnapshot.keyPrefix === keyPrefix &&
92
+ lastSnapshot.revision === currentRevision // Check revision
86
93
  ) {
87
94
  return lastSnapshot;
88
95
  }
96
+
97
+ const calculatedT = i18n.getFixedT(
98
+ currentLng,
99
+ i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0],
100
+ keyPrefix,
101
+ );
102
+
89
103
  const newSnapshot = {
90
104
  t: calculatedT,
91
105
  ready: calculatedReady,
92
- lng: props.lng || i18n.language,
106
+ lng: currentLng,
93
107
  keyPrefix,
108
+ revision: currentRevision, // Store revision
94
109
  };
95
110
  snapshotRef.current = newSnapshot;
96
111
  return newSnapshot;