mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
fix: rerender i18n in host components on lang change (#6224)
This commit is contained in:
parent
e4506be3e8
commit
04a8c22f39
14 changed files with 88 additions and 72 deletions
16
src/i18n.ts
16
src/i18n.ts
|
@ -1,6 +1,8 @@
|
|||
import fallbackLangData from "./locales/en.json";
|
||||
import percentages from "./locales/percentages.json";
|
||||
import { ENV } from "./constants";
|
||||
import { jotaiScope, jotaiStore } from "./jotai";
|
||||
import { atom, useAtomValue } from "jotai";
|
||||
|
||||
const COMPLETION_THRESHOLD = 85;
|
||||
|
||||
|
@ -99,6 +101,8 @@ export const setLanguage = async (lang: Language) => {
|
|||
currentLangData = fallbackLangData;
|
||||
}
|
||||
}
|
||||
|
||||
jotaiStore.set(editorLangCodeAtom, lang.code);
|
||||
};
|
||||
|
||||
export const getLanguage = () => currentLang;
|
||||
|
@ -143,3 +147,15 @@ export const t = (
|
|||
}
|
||||
return translation;
|
||||
};
|
||||
|
||||
/** @private atom used solely to rerender components using `useI18n` hook */
|
||||
const editorLangCodeAtom = atom(defaultLang.code);
|
||||
|
||||
// Should be used in components that fall under these cases:
|
||||
// - component is rendered as an <Excalidraw> child
|
||||
// - component is rendered internally by <Excalidraw>, but the component
|
||||
// is memoized w/o being updated on `langCode`, `AppState`, or `UIAppState`
|
||||
export const useI18n = () => {
|
||||
const langCode = useAtomValue(editorLangCodeAtom, jotaiScope);
|
||||
return { t, langCode };
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue