-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseLocalizedText.ts
More file actions
30 lines (28 loc) Β· 1.07 KB
/
Copy pathuseLocalizedText.ts
File metadata and controls
30 lines (28 loc) Β· 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { normalizeSupportedLanguageCode } from "../i18n";
import { resolveLocalizedText, type LocalizedText } from "../lib/localizedText";
/**
* Resolver for plugin-authored {@link LocalizedText} values, bound to
* the active app language.
*
* Goes through `useTranslation` rather than reading `i18n.language`
* directly so the consuming component re-subscribes to i18next's
* `languageChanged` event β a language switch re-renders the plugin
* rows just like it re-renders `t()` output around them.
*
* The code is normalised first (`fr-FR` β `fr`, `kr` β `ko`) so the
* lookup sees one of the app's 17 canonical codes, matching what a
* plugin author is told to key their manifest on.
*/
export function useLocalizedText() {
const { i18n } = useTranslation();
const lang = normalizeSupportedLanguageCode(
i18n.resolvedLanguage ?? i18n.language,
);
return useCallback(
(value: LocalizedText | null | undefined) =>
resolveLocalizedText(value, lang),
[lang],
);
}