Skip to content

Commit 9d9f91a

Browse files
fix: recompute LL after calling setLocale (#722)
Co-authored-by: Hofer Ivan <ivan.hofer@outlook.com>
1 parent a5cd2e3 commit 9d9f91a

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = [
3838
{
3939
name: 'adapter-react',
4040
path: 'react/index.min.mjs',
41-
limit: '1562 b',
41+
limit: '1602 b',
4242
ignore: ['react'],
4343
},
4444
{

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
# Version 5
44

5+
## 5.26.1 (2023-08-14)
6+
7+
Fix:
8+
- recompute `LL` object after calling `setLocale` in adapter-react [#721](https://github.com/ivanhofer/typesafe-i18n/issues/721)
9+
510
## 5.26.0 (2023-08-01)
611

712
Feat:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ Apart from that there can be a small overhead depending on which utilities and w
241241

242242
There also exists a useful wrapper for some frameworks:
243243
- [`typesafe-i18n` angular-service](https://github.com/ivanhofer/typesafe-i18n/tree/main/packages/adapter-angular): 1230 bytes gzipped
244-
- [`typesafe-i18n` react-context](https://github.com/ivanhofer/typesafe-i18n/tree/main/packages/adapter-react): 1562 bytes gzipped
244+
- [`typesafe-i18n` react-context](https://github.com/ivanhofer/typesafe-i18n/tree/main/packages/adapter-react): 1602 bytes gzipped
245245
- [`typesafe-i18n` solid-context](https://github.com/ivanhofer/typesafe-i18n/tree/main/packages/adapter-solid): 1403 bytes gzipped
246246
- [`typesafe-i18n` svelte-store](https://github.com/ivanhofer/typesafe-i18n/tree/main/packages/adapter-svelte): 1342 bytes gzipped
247247
- [`typesafe-i18n` vue-plugin](https://github.com/ivanhofer/typesafe-i18n/tree/main/packages/adapter-vue): 1256 bytes gzipped

packages/adapter-react/src/index.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,23 @@ export const initI18nReact = <
5050
const context = React.createContext({} as I18nContextType<L, T, TF>)
5151

5252
const component: React.FunctionComponent<TypesafeI18nProps<L>> = (props) => {
53-
const [locale, setLocale] = React.useState<L>(props.locale)
53+
const [locale, _setLocale] = React.useState<L>(props.locale)
54+
const [LL, setLL] = React.useState<TF>(() =>
55+
!locale ? getFallbackProxy<TF>() : i18nObject<L, T, TF, F>(locale, translations[locale], formatters[locale]),
56+
)
57+
58+
const setLocale = React.useCallback((newLocale: L) => {
59+
_setLocale(newLocale)
60+
setLL(() => i18nObject<L, T, TF, F>(newLocale, translations[newLocale], formatters[newLocale]))
61+
}, [])
5462

5563
const ctx = React.useMemo<I18nContextType<L, T, TF>>(
5664
() => ({
5765
setLocale,
5866
locale,
59-
LL: !locale
60-
? getFallbackProxy<TF>()
61-
: i18nObject<L, T, TF, F>(locale, translations[locale], formatters[locale]),
67+
LL,
6268
}),
63-
[setLocale, locale, translations, formatters],
69+
[setLocale, locale, LL],
6470
)
6571

6672
return <context.Provider value={ctx}>{props.children}</context.Provider>

0 commit comments

Comments
 (0)