typed-locales 1.0.49 → 1.0.50
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/adapters/react.d.ts +6 -7
- package/dist/adapters/react.d.ts.map +1 -1
- package/dist/adapters/react.js +35 -17
- package/package.json +35 -35
package/dist/adapters/react.d.ts
CHANGED
|
@@ -6,12 +6,11 @@ export interface TranslationContextType {
|
|
|
6
6
|
setLocale: (locale: Locales) => Promise<Locales>;
|
|
7
7
|
t: ReturnType<typeof getTranslate>;
|
|
8
8
|
}
|
|
9
|
-
export declare const initReact: (allTranslations: Record<Locales, () => Promise<{
|
|
9
|
+
export declare const initReact: (allTranslations: Record<Locales, (() => Promise<{
|
|
10
10
|
default: TranslationType;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
11
|
+
}>) | TranslationType>, extraFormatters: ExtraFormatters, defaultLocale: Locales, fallbackLocale?: Locales, extraTranslations?: Record<Locales, Record<string, string>>) => ({ children }: {
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
declare const useTranslation: () => TranslationContextType;
|
|
15
|
+
export default useTranslation;
|
|
17
16
|
//# sourceMappingURL=react.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/adapters/react.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/adapters/react.tsx"],"names":[],"mappings":"AAAA,OAAO,KAKN,MAAM,OAAO,CAAC;AAEf,OAAO,EACN,YAAY,EACZ,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,eAAe,EACpB,MAAM,aAAa,CAAC;AAErB,MAAM,WAAW,sBAAsB;IACtC,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IACjD,CAAC,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;CACnC;AA2BD,eAAO,MAAM,SAAS,GACrB,iBAAiB,MAAM,CACtB,OAAO,EACP,CAAC,MAAM,OAAO,CAAC;IAAE,OAAO,EAAE,eAAe,CAAA;CAAE,CAAC,CAAC,GAAG,eAAe,CAC/D,EACD,iBAAiB,eAAe,EAChC,eAAe,OAAO,EACtB,iBAAiB,OAAO,EACxB,oBAAoB,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,oBAShB;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,4CAqFxE,CAAC;AAEF,QAAA,MAAM,cAAc,8BAMnB,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
package/dist/adapters/react.js
CHANGED
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useCallback, useContext, useState } from 'react';
|
|
2
|
+
import { createContext, useCallback, useContext, useState, } from 'react';
|
|
3
3
|
import { getTranslate, } from '../index.js';
|
|
4
|
+
const TranslationContext = createContext(undefined);
|
|
5
|
+
const addExtraTranslations = (translation, extraTranslations) => {
|
|
6
|
+
let current = translation;
|
|
7
|
+
for (const key in extraTranslations) {
|
|
8
|
+
current = translation;
|
|
9
|
+
const parts = key.split('.');
|
|
10
|
+
const lastPart = parts.pop();
|
|
11
|
+
for (const part of parts) {
|
|
12
|
+
current = current[part] ?? {};
|
|
13
|
+
}
|
|
14
|
+
if (lastPart) {
|
|
15
|
+
current[lastPart] = extraTranslations[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return translation;
|
|
19
|
+
};
|
|
4
20
|
// Initial translation always should be loaded
|
|
5
|
-
export const initReact = (allTranslations, extraFormatters, fallbackLocale) => {
|
|
6
|
-
const
|
|
21
|
+
export const initReact = (allTranslations, extraFormatters, defaultLocale, fallbackLocale, extraTranslations) => {
|
|
22
|
+
const defaultTranslations = allTranslations[defaultLocale];
|
|
23
|
+
if (typeof defaultTranslations === 'function') {
|
|
24
|
+
throw new Error('Default locale in all translations object cant be a promise');
|
|
25
|
+
}
|
|
7
26
|
const TranslationProvider = ({ children }) => {
|
|
8
27
|
const [state, setState] = useState({
|
|
9
28
|
isLoading: false,
|
|
10
|
-
locale:
|
|
11
|
-
translate: ((
|
|
29
|
+
locale: defaultLocale,
|
|
30
|
+
translate: getTranslate(addExtraTranslations(defaultTranslations, extraTranslations?.[defaultLocale] ?? {}), defaultLocale, extraFormatters),
|
|
12
31
|
});
|
|
13
32
|
const setLocale = useCallback(async (targetLocale) => {
|
|
14
33
|
try {
|
|
@@ -34,7 +53,9 @@ export const initReact = (allTranslations, extraFormatters, fallbackLocale) => {
|
|
|
34
53
|
setState({
|
|
35
54
|
isLoading: false,
|
|
36
55
|
locale: targetLocale,
|
|
37
|
-
translate: getTranslate(translationData, targetLocale, extraFormatters, fallbackLocale === targetLocale || !fallbackLocale
|
|
56
|
+
translate: getTranslate(addExtraTranslations(translationData, extraTranslations?.[targetLocale] ?? {}), targetLocale, extraFormatters, fallbackLocale === targetLocale || !fallbackLocale
|
|
57
|
+
? undefined
|
|
58
|
+
: getTranslate(fallbackTranslationData, fallbackLocale, extraFormatters)),
|
|
38
59
|
});
|
|
39
60
|
}
|
|
40
61
|
catch (error) {
|
|
@@ -50,15 +71,12 @@ export const initReact = (allTranslations, extraFormatters, fallbackLocale) => {
|
|
|
50
71
|
t: state.translate,
|
|
51
72
|
}, children: children }));
|
|
52
73
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
TranslationProvider,
|
|
62
|
-
useTranslation,
|
|
63
|
-
};
|
|
74
|
+
return TranslationProvider;
|
|
75
|
+
};
|
|
76
|
+
const useTranslation = () => {
|
|
77
|
+
const context = useContext(TranslationContext);
|
|
78
|
+
if (!context)
|
|
79
|
+
throw new Error('useTranslation must be used within a TranslationProvider');
|
|
80
|
+
return context;
|
|
64
81
|
};
|
|
82
|
+
export default useTranslation;
|
package/package.json
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
2
|
+
"name": "typed-locales",
|
|
3
|
+
"version": "1.0.50",
|
|
4
|
+
"description": "Type safe utilities for translating strings",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "rimraf dist && tsc",
|
|
10
|
+
"dev": "tsx watch src/test.tsx",
|
|
11
|
+
"deploy": "npm run build && npm publish --access public",
|
|
12
|
+
"test": "tsx ./src/tests/test.ts"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [],
|
|
15
|
+
"author": "",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"packageManager": "pnpm@10.6.2",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"react": "^19.1.0",
|
|
20
|
+
"rimraf": "^6.0.1",
|
|
21
|
+
"tsx": "^4.19.4",
|
|
22
|
+
"typescript": "^5.8.3",
|
|
23
|
+
"zustand": "^5.0.5"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@eslint/js": "^9.28.0",
|
|
27
|
+
"@eslint/markdown": "^6.4.0",
|
|
28
|
+
"@types/react": "^19.1.5",
|
|
29
|
+
"eslint": "^9.28.0",
|
|
30
|
+
"eslint-config-prettier": "^10.1.5",
|
|
31
|
+
"eslint-plugin-prettier": "^5.4.1",
|
|
32
|
+
"globals": "^16.2.0",
|
|
33
|
+
"prettier": "^3.5.3",
|
|
34
|
+
"typescript-eslint": "^8.33.0"
|
|
35
|
+
}
|
|
36
|
+
}
|