typed-locales 1.0.16 → 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react-utilities.d.ts.map +1 -1
- package/dist/react-utilities.js +14 -23
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-utilities.d.ts","sourceRoot":"","sources":["../src/react-utilities.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"react-utilities.d.ts","sourceRoot":"","sources":["../src/react-utilities.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+D,MAAM,OAAO,CAAC;AAEpF,OAAO,EAAE,YAAY,EAAE,KAAK,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAAqB,MAAM,SAAS,CAAC;AAEtH,MAAM,WAAW,sBAAsB;IACtC,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;IACrC,CAAC,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;CACnC;AAGD,eAAO,MAAM,SAAS,GACrB,iBAAiB,MAAM,CAAC,OAAO,EAAE,iBAAiB,GAAG,CAAC,MAAM,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,EACxF,eAAe,OAAO,EACtB,iBAAiB,eAAe;wCAGW;QAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;KAAE;;CA4DxE,CAAC"}
|
package/dist/react-utilities.js
CHANGED
|
@@ -6,30 +6,24 @@ export const initReact = (allTranslations, initialLocale, extraFormatters) => {
|
|
|
6
6
|
const TranslationContext = createContext(undefined);
|
|
7
7
|
const TranslationProvider = ({ children }) => {
|
|
8
8
|
const [locale, setLocale] = useState(initialLocale);
|
|
9
|
-
const [translate, setTranslate] = useState(() => {
|
|
10
|
-
console.log('initializing translate', locale);
|
|
11
|
-
return getTranslate(allTranslations[locale], locale, extraFormatters);
|
|
12
|
-
});
|
|
13
9
|
const [isLoading, setIsLoading] = useState(true);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
console.log('isLoading', isLoading);
|
|
17
|
-
const loadTranslation = async (targetLocale) => {
|
|
10
|
+
const [translate, setTranslate] = useState(() => getTranslate(allTranslations[locale], locale, extraFormatters));
|
|
11
|
+
const setLocaleInternal = async (targetLocale) => {
|
|
18
12
|
try {
|
|
19
13
|
const translationOrLoader = allTranslations[targetLocale];
|
|
20
|
-
let translationData;
|
|
21
14
|
if (typeof translationOrLoader === 'function') {
|
|
22
15
|
console.log('loading translation', targetLocale);
|
|
23
16
|
setIsLoading(true);
|
|
24
|
-
translationData = await translationOrLoader();
|
|
17
|
+
const translationData = await translationOrLoader();
|
|
18
|
+
setTranslate(getTranslate(translationData, targetLocale, extraFormatters));
|
|
19
|
+
setLocale(targetLocale);
|
|
20
|
+
setIsLoading(false);
|
|
25
21
|
}
|
|
26
22
|
else {
|
|
27
23
|
console.log('sync translation', targetLocale);
|
|
28
|
-
|
|
24
|
+
setLocale(targetLocale);
|
|
25
|
+
setTranslate(getTranslate(translationOrLoader, targetLocale, extraFormatters));
|
|
29
26
|
}
|
|
30
|
-
console.log('setting translation to', translationData);
|
|
31
|
-
console.log('translate', getTranslate(translationData, targetLocale, extraFormatters));
|
|
32
|
-
setTranslate(getTranslate(translationData, targetLocale, extraFormatters));
|
|
33
27
|
}
|
|
34
28
|
catch (error) {
|
|
35
29
|
console.error(`Failed to load translations for locale ${String(targetLocale)}:`, error);
|
|
@@ -41,16 +35,13 @@ export const initReact = (allTranslations, initialLocale, extraFormatters) => {
|
|
|
41
35
|
return (_jsx(TranslationContext.Provider, { value: {
|
|
42
36
|
isLoading,
|
|
43
37
|
locale,
|
|
44
|
-
setLocale:
|
|
45
|
-
if (newLocale !== locale) {
|
|
46
|
-
setLocale(newLocale);
|
|
47
|
-
await loadTranslation(newLocale);
|
|
48
|
-
}
|
|
49
|
-
},
|
|
38
|
+
setLocale: setLocaleInternal,
|
|
50
39
|
// @ts-ignore
|
|
51
|
-
t: (...args) => {
|
|
52
|
-
|
|
53
|
-
|
|
40
|
+
t: (key, ...args) => {
|
|
41
|
+
if (typeof key === 'string') {
|
|
42
|
+
console.trace('incorrect translation', key, args);
|
|
43
|
+
}
|
|
44
|
+
return translate(key, ...args);
|
|
54
45
|
},
|
|
55
46
|
}, children: children }));
|
|
56
47
|
};
|