typed-locales 1.0.52 → 1.0.53
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
CHANGED
|
@@ -6,10 +6,17 @@ export interface TranslationContextType {
|
|
|
6
6
|
setLocale: (locale: Locales) => Promise<Locales>;
|
|
7
7
|
t: ReturnType<typeof getTranslate>;
|
|
8
8
|
}
|
|
9
|
+
type ExtraTranslation = {
|
|
10
|
+
[locale in Locales]?: string;
|
|
11
|
+
} & {
|
|
12
|
+
key: string;
|
|
13
|
+
};
|
|
14
|
+
type ExtraTranslations = ExtraTranslation[];
|
|
9
15
|
export declare const initReact: (allTranslations: Record<Locales, (() => Promise<{
|
|
10
16
|
default: TranslationType;
|
|
11
|
-
}>) | TranslationType>, extraFormatters: ExtraFormatters, defaultLocale: Locales, fallbackLocale?: Locales, extraTranslations?:
|
|
17
|
+
}>) | TranslationType>, extraFormatters: ExtraFormatters, defaultLocale: Locales, fallbackLocale?: Locales, extraTranslations?: ExtraTranslations) => ({ children }: {
|
|
12
18
|
children: React.ReactNode;
|
|
13
19
|
}) => import("react/jsx-runtime").JSX.Element;
|
|
14
20
|
export declare const useTranslation: () => TranslationContextType;
|
|
21
|
+
export {};
|
|
15
22
|
//# sourceMappingURL=react.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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;AAMD,KAAK,gBAAgB,GAAG;KACtB,MAAM,IAAI,OAAO,CAAC,CAAC,EAAE,MAAM;CAC5B,GAAG;IACH,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,KAAK,iBAAiB,GAAG,gBAAgB,EAAE,CAAC;AA4B5C,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,iBAAiB,oBASM;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,4CAsFxE,CAAC;AAEF,eAAO,MAAM,cAAc,8BAM1B,CAAC"}
|
package/dist/adapters/react.js
CHANGED
|
@@ -2,9 +2,10 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { createContext, useCallback, useContext, useState, } from 'react';
|
|
3
3
|
import { getTranslate, } from '../index.js';
|
|
4
4
|
const TranslationContext = createContext(undefined);
|
|
5
|
-
const addExtraTranslations = (translation, extraTranslations) => {
|
|
5
|
+
const addExtraTranslations = (translation, extraTranslations, locale) => {
|
|
6
6
|
let current = translation;
|
|
7
|
-
for (const
|
|
7
|
+
for (const extraTranslation of extraTranslations) {
|
|
8
|
+
const { key, ...locales } = extraTranslation;
|
|
8
9
|
current = translation;
|
|
9
10
|
const parts = key.split('.');
|
|
10
11
|
const lastPart = parts.pop();
|
|
@@ -14,8 +15,8 @@ const addExtraTranslations = (translation, extraTranslations) => {
|
|
|
14
15
|
}
|
|
15
16
|
current = current[part];
|
|
16
17
|
}
|
|
17
|
-
if (lastPart) {
|
|
18
|
-
current[lastPart] =
|
|
18
|
+
if (lastPart && locales[locale] !== undefined) {
|
|
19
|
+
current[lastPart] = locales[locale];
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
return translation;
|
|
@@ -30,7 +31,7 @@ export const initReact = (allTranslations, extraFormatters, defaultLocale, fallb
|
|
|
30
31
|
const [state, setState] = useState({
|
|
31
32
|
isLoading: false,
|
|
32
33
|
locale: defaultLocale,
|
|
33
|
-
translate: getTranslate(addExtraTranslations(defaultTranslations, extraTranslations
|
|
34
|
+
translate: getTranslate(addExtraTranslations(defaultTranslations, extraTranslations ?? [], defaultLocale), defaultLocale, extraFormatters),
|
|
34
35
|
});
|
|
35
36
|
const setLocale = useCallback(async (targetLocale) => {
|
|
36
37
|
try {
|
|
@@ -56,7 +57,7 @@ export const initReact = (allTranslations, extraFormatters, defaultLocale, fallb
|
|
|
56
57
|
setState({
|
|
57
58
|
isLoading: false,
|
|
58
59
|
locale: targetLocale,
|
|
59
|
-
translate: getTranslate(addExtraTranslations(translationData, extraTranslations
|
|
60
|
+
translate: getTranslate(addExtraTranslations(translationData, extraTranslations ?? [], targetLocale), targetLocale, extraFormatters, fallbackLocale === targetLocale || !fallbackLocale
|
|
60
61
|
? undefined
|
|
61
62
|
: getTranslate(fallbackTranslationData, fallbackLocale, extraFormatters)),
|
|
62
63
|
});
|