Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: i18next/react-i18next
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v14.0.2
Choose a base ref
...
head repository: i18next/react-i18next
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v14.0.3
Choose a head ref
  • 3 commits
  • 6 files changed
  • 1 contributor

Commits on Feb 5, 2024

  1. Revert "Fix/bug 1691 make returned t function identical upon second e…

    …ffect run in strict mode (#1716)"
    
    This reverts commit f8a4e19.
    adrai committed Feb 5, 2024
    Copy the full SHA
    8df58e0 View commit details
  2. revert last version

    adrai committed Feb 5, 2024
    Copy the full SHA
    66e8ac8 View commit details
  3. 14.0.3

    adrai committed Feb 5, 2024
    Copy the full SHA
    80c8f61 View commit details
Showing with 22 additions and 50 deletions.
  1. +4 −0 CHANGELOG.md
  2. +2 −2 package-lock.json
  3. +1 −1 package.json
  4. +4 −11 react-i18next.js
  5. +1 −1 react-i18next.min.js
  6. +10 −35 src/useTranslation.js
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 14.0.3

- revert changes done in v14.0.2 since it breaks normal language change render updates

### 14.0.2

- Fix/bug [1691](https://github.com/i18next/react-i18next/issues/1691) make returned t function identical upon second effect run in strict mode [1716](https://github.com/i18next/react-i18next/pull/1716)
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-i18next",
"version": "14.0.2",
"version": "14.0.3",
"description": "Internationalization for react done right. Using the i18next i18n ecosystem.",
"main": "dist/commonjs/index.js",
"types": "./index.d.mts",
15 changes: 4 additions & 11 deletions react-i18next.js
Original file line number Diff line number Diff line change
@@ -612,12 +612,6 @@
}, [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 {
@@ -656,8 +650,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));
const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
const getT = () => memoGetT;
function getT() {
return i18n.getFixedT(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}`;
@@ -676,9 +671,7 @@
});
} else {
loadNamespaces(i18n, namespaces, () => {
if (isMounted.current) {
setT(() => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix));
}
if (isMounted.current) setT(getT);
});
}
}
2 changes: 1 addition & 1 deletion react-i18next.min.js

Large diffs are not rendered by default.

45 changes: 10 additions & 35 deletions src/useTranslation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useContext, useRef, useCallback } from 'react';
import { useState, useEffect, useContext, useRef } from 'react';
import { getI18n, getDefaults, ReportNamespaces, I18nContext } from './context.js';
import { warnOnce, loadNamespaces, loadLanguages, hasLoadedNamespace } from './utils.js';

@@ -10,19 +10,6 @@ const usePrevious = (value, ignore) => {
return ref.current;
};

function alwaysNewT(i18n, language, namespace, keyPrefix) {
return i18n.getFixedT(language, namespace, keyPrefix);
}

function useMemoizedT(i18n, language, namespace, keyPrefix) {
return useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [
i18n,
language,
namespace,
keyPrefix,
]);
}

export function useTranslation(ns, props = {}) {
// assert we have the needed i18nInstance
const { i18n: i18nFromProps } = props;
@@ -68,16 +55,14 @@ export function useTranslation(ns, props = {}) {
(i18n.isInitialized || i18n.initializedStoreOnce) &&
namespaces.every((n) => hasLoadedNamespace(n, i18n, i18nOptions));

// binding t function to namespace (acts also as rerender trigger *when* args have changed)
const memoGetT = useMemoizedT(
i18n,
props.lng || null,
i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0],
keyPrefix,
);
// using useState with a function expects an initializer, not the function itself:
const getT = () => memoGetT;

// binding t function to namespace (acts also as rerender trigger)
function getT() {
return i18n.getFixedT(
props.lng || null,
i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0],
keyPrefix,
);
}
const [t, setT] = useState(getT);

let joinedNS = namespaces.join();
@@ -98,17 +83,7 @@ export function useTranslation(ns, props = {}) {
});
} else {
loadNamespaces(i18n, namespaces, () => {
if (isMounted.current) {
// despite that no memoization arguments changed, supply always new T to trigger rerender
setT(() =>
alwaysNewT(
i18n,
props.lng || null,
i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0],
keyPrefix,
),
);
}
if (isMounted.current) setT(getT);
});
}
}