Skip to content

Commit

Permalink
Fix/bug 1691 preserving change language binding (#1720)
Browse files Browse the repository at this point in the history
* test: bug 1691 second attempt, add test for actual use of i18n.changeLanguage with useTranslation

* fix: (pre-cleanup) bug 1691 cherry pick of 168b8f0: use memoization of fixedT function,

bad impl, breaks test that was introduced in parent commit, verifying bug here: #1716 (comment)

* fix: (pre-cleanup) bug 1691 fixing dev issue: regenerate a new t fn everywhere

other than where the bug was occurring, and keep shallow routing working via dependency list of useCallback hook.  Fixes test broken by parent commit.

* style: cleanup debug messages for bug 1691

* build: bug 1691 npm run build
  • Loading branch information
timheilman committed Feb 6, 2024
1 parent ac3f71c commit dc36d69
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
20 changes: 13 additions & 7 deletions react-i18next.js
Expand Up @@ -612,6 +612,12 @@
}, [value, ignore]);
return ref.current;
};
function alwaysNewT(i18n, language, namespace, keyPrefix) {
return i18n.getFixedT(language, namespace, keyPrefix);
}
function useMemoizedT(i18n, language, namespace, keyPrefix) {
return react.useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
}
function useTranslation(ns) {
let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const {
Expand Down Expand Up @@ -650,9 +656,9 @@
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
function getT() {
return i18n.getFixedT(props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
}
const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
const getT = () => memoGetT;
const getNewT = () => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
const [t, setT] = react.useState(getT);
let joinedNS = namespaces.join();
if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
Expand All @@ -667,19 +673,19 @@
if (!ready && !useSuspense) {
if (props.lng) {
loadLanguages(i18n, props.lng, namespaces, () => {
if (isMounted.current) setT(getT);
if (isMounted.current) setT(getNewT);
});
} else {
loadNamespaces(i18n, namespaces, () => {
if (isMounted.current) setT(getT);
if (isMounted.current) setT(getNewT);
});
}
}
if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
setT(getT);
setT(getNewT);
}
function boundReset() {
if (isMounted.current) setT(getT);
if (isMounted.current) setT(getNewT);
}
if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset);
Expand Down

0 comments on commit dc36d69

Please sign in to comment.