typed-locales 1.0.15 → 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 +16 -14
- package/package.json +2 -2
|
@@ -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
|
@@ -4,25 +4,26 @@ import { getTranslate } from './index.js';
|
|
|
4
4
|
// Initial translation always should be loaded
|
|
5
5
|
export const initReact = (allTranslations, initialLocale, extraFormatters) => {
|
|
6
6
|
const TranslationContext = createContext(undefined);
|
|
7
|
-
const initialTranslate = getTranslate(allTranslations[initialLocale], initialLocale, extraFormatters);
|
|
8
7
|
const TranslationProvider = ({ children }) => {
|
|
9
8
|
const [locale, setLocale] = useState(initialLocale);
|
|
10
|
-
const [translate, setTranslate] = useState(initialTranslate);
|
|
11
9
|
const [isLoading, setIsLoading] = useState(true);
|
|
12
|
-
const
|
|
10
|
+
const [translate, setTranslate] = useState(() => getTranslate(allTranslations[locale], locale, extraFormatters));
|
|
11
|
+
const setLocaleInternal = async (targetLocale) => {
|
|
13
12
|
try {
|
|
14
13
|
const translationOrLoader = allTranslations[targetLocale];
|
|
15
|
-
let translationData;
|
|
16
14
|
if (typeof translationOrLoader === 'function') {
|
|
15
|
+
console.log('loading translation', targetLocale);
|
|
17
16
|
setIsLoading(true);
|
|
18
|
-
translationData = await translationOrLoader();
|
|
17
|
+
const translationData = await translationOrLoader();
|
|
18
|
+
setTranslate(getTranslate(translationData, targetLocale, extraFormatters));
|
|
19
|
+
setLocale(targetLocale);
|
|
20
|
+
setIsLoading(false);
|
|
19
21
|
}
|
|
20
22
|
else {
|
|
21
|
-
|
|
23
|
+
console.log('sync translation', targetLocale);
|
|
24
|
+
setLocale(targetLocale);
|
|
25
|
+
setTranslate(getTranslate(translationOrLoader, targetLocale, extraFormatters));
|
|
22
26
|
}
|
|
23
|
-
console.log('setting translation to', translationData);
|
|
24
|
-
console.log('translate', getTranslate(translationData, targetLocale, extraFormatters));
|
|
25
|
-
setTranslate(getTranslate(translationData, targetLocale, extraFormatters));
|
|
26
27
|
}
|
|
27
28
|
catch (error) {
|
|
28
29
|
console.error(`Failed to load translations for locale ${String(targetLocale)}:`, error);
|
|
@@ -34,13 +35,14 @@ export const initReact = (allTranslations, initialLocale, extraFormatters) => {
|
|
|
34
35
|
return (_jsx(TranslationContext.Provider, { value: {
|
|
35
36
|
isLoading,
|
|
36
37
|
locale,
|
|
37
|
-
setLocale:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
setLocale: setLocaleInternal,
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
t: (key, ...args) => {
|
|
41
|
+
if (typeof key === 'string') {
|
|
42
|
+
console.trace('incorrect translation', key, args);
|
|
41
43
|
}
|
|
44
|
+
return translate(key, ...args);
|
|
42
45
|
},
|
|
43
|
-
t: translate,
|
|
44
46
|
}, children: children }));
|
|
45
47
|
};
|
|
46
48
|
const useTranslation = () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typed-locales",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "Type safe utilities for translating strings",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "npm-run-all -s -c build:*",
|
|
10
10
|
"build:tsc": "rimraf dist && tsc",
|
|
11
|
-
"build:tsc-alias": "tsc-alias -p tsconfig.json
|
|
11
|
+
"build:tsc-alias": "tsc-alias -p tsconfig.json",
|
|
12
12
|
"dev": "tsx watch src/test.tsx",
|
|
13
13
|
"deploy": "npm run build && npm publish --access public"
|
|
14
14
|
},
|