typed-locales 1.0.17 → 1.0.19
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 +27 -14
- 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,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;;
|
|
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;;CA6ExE,CAAC"}
|
package/dist/react-utilities.js
CHANGED
|
@@ -5,43 +5,56 @@ import { getTranslate } from './index.js';
|
|
|
5
5
|
export const initReact = (allTranslations, initialLocale, extraFormatters) => {
|
|
6
6
|
const TranslationContext = createContext(undefined);
|
|
7
7
|
const TranslationProvider = ({ children }) => {
|
|
8
|
-
const [
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const [state, setState] = useState({
|
|
9
|
+
locale: initialLocale,
|
|
10
|
+
translate: getTranslate(allTranslations[initialLocale], initialLocale, extraFormatters),
|
|
11
|
+
isLoading: true,
|
|
12
|
+
});
|
|
11
13
|
const setLocaleInternal = async (targetLocale) => {
|
|
12
14
|
try {
|
|
13
15
|
const translationOrLoader = allTranslations[targetLocale];
|
|
14
16
|
if (typeof translationOrLoader === 'function') {
|
|
15
17
|
console.log('loading translation', targetLocale);
|
|
16
|
-
|
|
18
|
+
setState({
|
|
19
|
+
...state,
|
|
20
|
+
isLoading: true,
|
|
21
|
+
});
|
|
17
22
|
const translationData = await translationOrLoader();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
setState({
|
|
24
|
+
locale: targetLocale,
|
|
25
|
+
translate: getTranslate(translationData, targetLocale, extraFormatters),
|
|
26
|
+
isLoading: false,
|
|
27
|
+
});
|
|
21
28
|
}
|
|
22
29
|
else {
|
|
23
30
|
console.log('sync translation', targetLocale);
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
setState({
|
|
32
|
+
locale: targetLocale,
|
|
33
|
+
translate: getTranslate(translationOrLoader, targetLocale, extraFormatters),
|
|
34
|
+
isLoading: false,
|
|
35
|
+
});
|
|
26
36
|
}
|
|
27
37
|
}
|
|
28
38
|
catch (error) {
|
|
29
39
|
console.error(`Failed to load translations for locale ${String(targetLocale)}:`, error);
|
|
30
40
|
}
|
|
31
41
|
finally {
|
|
32
|
-
|
|
42
|
+
setState({
|
|
43
|
+
...state,
|
|
44
|
+
isLoading: false,
|
|
45
|
+
});
|
|
33
46
|
}
|
|
34
47
|
};
|
|
35
48
|
return (_jsx(TranslationContext.Provider, { value: {
|
|
36
|
-
isLoading,
|
|
37
|
-
locale,
|
|
49
|
+
isLoading: state.isLoading,
|
|
50
|
+
locale: state.locale,
|
|
38
51
|
setLocale: setLocaleInternal,
|
|
39
52
|
// @ts-ignore
|
|
40
53
|
t: (key, ...args) => {
|
|
41
|
-
if (typeof key
|
|
54
|
+
if (typeof key !== 'string') {
|
|
42
55
|
console.trace('incorrect translation', key, args);
|
|
43
56
|
}
|
|
44
|
-
return translate(key, ...args);
|
|
57
|
+
return state.translate(key, ...args);
|
|
45
58
|
},
|
|
46
59
|
}, children: children }));
|
|
47
60
|
};
|