typed-locales 1.0.43 → 1.0.45
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 +22 -0
- package/dist/adapters/react.d.ts.map +1 -0
- package/dist/{react.js → adapters/react.js} +23 -10
- package/dist/adapters/zustand.d.ts +12 -0
- package/dist/adapters/zustand.d.ts.map +1 -0
- package/dist/adapters/zustand.js +33 -0
- package/dist/index.d.ts +89 -30
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -3
- package/eslint.config.js +1 -1
- package/package.json +3 -2
- package/dist/react.d.ts +0 -17
- package/dist/react.d.ts.map +0 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getTranslate, createNamespaceTranslate, type ExtraFormatters, type Locales, type TranslationType, type NameSpaces } from '../index.js';
|
|
3
|
+
export interface TranslationContextType {
|
|
4
|
+
isLoading: boolean;
|
|
5
|
+
locale?: Locales;
|
|
6
|
+
setLocale: (locale: Locales) => Promise<Locales>;
|
|
7
|
+
t: ReturnType<typeof getTranslate>;
|
|
8
|
+
}
|
|
9
|
+
export declare const initReact: (allTranslations: Record<Locales, () => Promise<{
|
|
10
|
+
default: TranslationType;
|
|
11
|
+
}>>, extraFormatters: ExtraFormatters, fallbackLocale?: Locales) => {
|
|
12
|
+
TranslationProvider: ({ children }: {
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
15
|
+
useTranslation: {
|
|
16
|
+
(): TranslationContextType;
|
|
17
|
+
<NS extends NameSpaces>(namespace: NS): Omit<TranslationContextType, "t"> & {
|
|
18
|
+
t: ReturnType<typeof createNamespaceTranslate<NS>>;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/adapters/react.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAoE,MAAM,OAAO,CAAC;AAEzF,OAAO,EACN,YAAY,EACZ,wBAAwB,EACxB,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,eAAe,EACpB,KAAK,UAAU,EACf,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;AAGD,eAAO,MAAM,SAAS,GACrB,iBAAiB,MAAM,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC;IAAE,OAAO,EAAE,eAAe,CAAA;CAAE,CAAC,CAAC,EAC7E,iBAAiB,eAAe,EAChC,iBAAiB,OAAO;wCAImB;QAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;KAAE;;YAuE7C,sBAAsB;SACzB,EAAE,SAAS,UAAU,aAAa,EAAE,GAAG,IAAI,CAAC,sBAAsB,EAAE,GAAG,CAAC,GAAG;YAClG,CAAC,EAAE,UAAU,CAAC,OAAO,wBAAwB,CAAC,EAAE,CAAC,CAAC,CAAC;SACnD;;CAmBD,CAAC"}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useCallback, useContext, useState } from 'react';
|
|
3
|
-
import { getTranslate } from '
|
|
2
|
+
import { createContext, useCallback, useContext, useMemo, useState } from 'react';
|
|
3
|
+
import { getTranslate, createNamespaceTranslate } from '../index.js';
|
|
4
4
|
// Initial translation always should be loaded
|
|
5
|
-
export const initReact = (
|
|
5
|
+
export const initReact = (allTranslations, extraFormatters, fallbackLocale) => {
|
|
6
6
|
const TranslationContext = createContext(undefined);
|
|
7
|
-
const initialTranslate = getTranslate(initialTranslation, initialLocale, extraFormatters);
|
|
8
7
|
const TranslationProvider = ({ children }) => {
|
|
9
8
|
const [state, setState] = useState({
|
|
10
9
|
isLoading: false,
|
|
11
|
-
locale:
|
|
12
|
-
translate:
|
|
10
|
+
locale: undefined,
|
|
11
|
+
translate: ((key) => key),
|
|
13
12
|
});
|
|
14
13
|
const setLocale = useCallback(async (targetLocale) => {
|
|
15
14
|
try {
|
|
@@ -22,10 +21,20 @@ export const initReact = (initialTranslation, initialLocale, allTranslations, ex
|
|
|
22
21
|
else {
|
|
23
22
|
translationData = translationOrLoader;
|
|
24
23
|
}
|
|
24
|
+
let fallbackTranslationData;
|
|
25
|
+
if (fallbackLocale) {
|
|
26
|
+
const fallbackTranslationOrLoader = allTranslations[targetLocale];
|
|
27
|
+
if (typeof fallbackTranslationOrLoader === 'function') {
|
|
28
|
+
fallbackTranslationData = await fallbackTranslationOrLoader().then(t => t.default);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
fallbackTranslationData = fallbackTranslationOrLoader;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
25
34
|
setState({
|
|
26
35
|
isLoading: false,
|
|
27
36
|
locale: targetLocale,
|
|
28
|
-
translate: getTranslate(translationData, targetLocale, extraFormatters,
|
|
37
|
+
translate: getTranslate(translationData, targetLocale, extraFormatters, fallbackLocale === targetLocale || !fallbackLocale ? undefined : getTranslate(fallbackTranslationData, fallbackLocale, extraFormatters)),
|
|
29
38
|
});
|
|
30
39
|
}
|
|
31
40
|
catch (error) {
|
|
@@ -41,12 +50,16 @@ export const initReact = (initialTranslation, initialLocale, allTranslations, ex
|
|
|
41
50
|
t: state.translate,
|
|
42
51
|
}, children: children }));
|
|
43
52
|
};
|
|
44
|
-
|
|
53
|
+
function useTranslation(namespace) {
|
|
45
54
|
const context = useContext(TranslationContext);
|
|
46
55
|
if (!context)
|
|
47
56
|
throw new Error('useTranslation must be used within a TranslationProvider');
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
const t = useMemo(() => namespace ? createNamespaceTranslate(namespace, context.t) : context.t, [namespace, context]);
|
|
58
|
+
return {
|
|
59
|
+
...context,
|
|
60
|
+
t,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
50
63
|
return {
|
|
51
64
|
TranslationProvider,
|
|
52
65
|
useTranslation,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ExtraFormatters, getTranslate, type Locales, type TranslationType } from '../index.js';
|
|
2
|
+
interface TranslationState {
|
|
3
|
+
isLoading: boolean;
|
|
4
|
+
locale?: Locales;
|
|
5
|
+
setLocale: (locale: Locales) => Promise<void>;
|
|
6
|
+
t: ReturnType<typeof getTranslate>;
|
|
7
|
+
}
|
|
8
|
+
export declare const initZustand: (allTranslations: Record<Locales, () => Promise<{
|
|
9
|
+
default: TranslationType;
|
|
10
|
+
}>>, extraFormatters: ExtraFormatters, fallbackLocale?: Locales) => import("zustand").UseBoundStore<import("zustand").StoreApi<TranslationState>>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=zustand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zustand.d.ts","sourceRoot":"","sources":["../../src/adapters/zustand.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,eAAe,EACpB,YAAY,EACZ,KAAK,OAAO,EACZ,KAAK,eAAe,EACpB,MAAM,aAAa,CAAC;AAErB,UAAU,gBAAgB;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB,SAAS,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9C,CAAC,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;CACnC;AAED,eAAO,MAAM,WAAW,GACvB,iBAAiB,MAAM,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC;IAAE,OAAO,EAAE,eAAe,CAAA;CAAE,CAAC,CAAC,EAC7E,iBAAiB,eAAe,EAChC,iBAAiB,OAAO,kFAgDxB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { create } from 'zustand';
|
|
2
|
+
import { getTranslate, } from '../index.js';
|
|
3
|
+
export const initZustand = (allTranslations, extraFormatters, fallbackLocale) => {
|
|
4
|
+
const useTranslation = create((set, get) => ({
|
|
5
|
+
isLoading: false,
|
|
6
|
+
locale: undefined,
|
|
7
|
+
setLocale: async (targetLocale) => {
|
|
8
|
+
try {
|
|
9
|
+
if (get().locale === targetLocale) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
set({ isLoading: true });
|
|
13
|
+
const translationData = await allTranslations[targetLocale]().then(t => t.default);
|
|
14
|
+
let fallbackTranslate;
|
|
15
|
+
if (fallbackLocale && fallbackLocale !== targetLocale) {
|
|
16
|
+
const fallbackData = await allTranslations[fallbackLocale]().then(t => t.default);
|
|
17
|
+
fallbackTranslate = getTranslate(fallbackData, fallbackLocale, extraFormatters);
|
|
18
|
+
}
|
|
19
|
+
set({
|
|
20
|
+
isLoading: false,
|
|
21
|
+
locale: targetLocale,
|
|
22
|
+
t: getTranslate(translationData, targetLocale, extraFormatters, fallbackTranslate),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.error(`Failed to load locale ${String(targetLocale)}:`, error);
|
|
27
|
+
set({ isLoading: false });
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
t: ((key) => key),
|
|
31
|
+
}));
|
|
32
|
+
return useTranslation;
|
|
33
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -19,55 +19,58 @@ export interface DefaultOverrides {
|
|
|
19
19
|
export interface Overrides extends DefaultOverrides {
|
|
20
20
|
}
|
|
21
21
|
export type Translations = Overrides['shape'];
|
|
22
|
+
/** Shape of the translation, used to ensure translations match */
|
|
22
23
|
export type TranslationType = DeepResolve<GenerateTranslationType<Translations>>;
|
|
23
24
|
export type Locales = Overrides['locales'];
|
|
24
|
-
|
|
25
|
+
/** All possible translation keys */
|
|
26
|
+
export type PossibleTranslationKeys = DotNestedStringKeys<Translations>;
|
|
27
|
+
/** All existing namespaces */
|
|
28
|
+
export type NameSpaces = DotNestedObjectKeys<Translations>;
|
|
25
29
|
export type ExtraFormatters = Overrides['extraFormatters'];
|
|
30
|
+
/** Base formatters + Custom formatters */
|
|
26
31
|
export type FormatterTypes = keyof ExtraFormatters | keyof typeof formatters;
|
|
27
32
|
export type TranslateFunctionType = ReturnType<typeof getTranslate>;
|
|
33
|
+
/** Get all possible translations for a given namespace */
|
|
34
|
+
export type NamespaceKeys<Namespace extends NameSpaces> = FilterByPrefix<PossibleTranslationKeys, Namespace>;
|
|
28
35
|
type TranslatedMark = {
|
|
29
36
|
translated: true;
|
|
30
37
|
};
|
|
31
38
|
/** Ensure a string is translated */
|
|
32
39
|
export type TranslatedString = string & TranslatedMark;
|
|
33
|
-
export type InterpolationProperties<
|
|
40
|
+
export type InterpolationProperties<Key extends PossibleTranslationKeys, Value extends string = GetValue<Key>> = DeepResolve<IsPlural<Key> extends true ? {
|
|
34
41
|
count: number;
|
|
35
|
-
} &
|
|
36
|
-
|
|
37
|
-
type: any;
|
|
38
|
-
}>> : ExtractPlaceholders<S> extends never ? {} : PlaceholderInfoToObject<ExtractPlaceholdersWithTypes<S>>>;
|
|
42
|
+
} & Omit<ExtractParams<Value>, 'count'> : ExtractParams<Value>>;
|
|
43
|
+
type ExtractParams<Value extends string> = Value extends `${infer _}{${infer _}}${infer _}` ? PlaceholderInfoToObject<ExtractPlaceholders<Value>> : {};
|
|
39
44
|
declare const pluralSufixes: readonly ["_none", "_one", "_other"];
|
|
40
45
|
type PluralSuffix = (typeof pluralSufixes)[number];
|
|
41
|
-
type
|
|
46
|
+
type FilterByPrefix<T, Prefix extends string> = T extends `${Prefix}.${infer Rest}` ? Rest : never;
|
|
47
|
+
type RemovePluralSuffix<T extends string> = T extends `${infer Base}_none` ? Base : T extends `${infer Base}_one` ? Base : T extends `${infer Base}_other` ? Base : T;
|
|
42
48
|
type PluralKeys<Base extends string> = `${Base}${PluralSuffix}`;
|
|
43
|
-
type
|
|
44
|
-
[K in keyof T]: K extends string ? T[K] extends Record<string, any> ?
|
|
45
|
-
}[keyof T];
|
|
49
|
+
type DotNestedStringKeys<T, Prefix extends string = ''> = T extends object ? {
|
|
50
|
+
[K in keyof T]: K extends string ? T[K] extends Record<string, any> ? DotNestedStringKeys<T[K], `${Prefix}${K}.`> : `${Prefix}${RemovePluralSuffix<K>}` : never;
|
|
51
|
+
}[keyof T] : never;
|
|
52
|
+
type DotNestedObjectKeys<T, Prefix extends string = ''> = T extends object ? {
|
|
53
|
+
[K in keyof T]: K extends string ? T[K] extends Record<string, any> ? `${Prefix}${K}` | DotNestedObjectKeys<T[K], `${Prefix}${K}.`> : never : never;
|
|
54
|
+
}[keyof T] : never;
|
|
46
55
|
export type GenerateTranslationType<T> = {
|
|
47
|
-
[K in keyof T as IsPlural<RemovePluralSuffix<K & string
|
|
56
|
+
[K in keyof T as IsPlural<RemovePluralSuffix<K & string> & PossibleTranslationKeys> extends true ? never : K]: T[K] extends object ? GenerateTranslationType<T[K]> : string;
|
|
48
57
|
} & {
|
|
49
|
-
[K in keyof T as IsPlural<RemovePluralSuffix<K & string
|
|
58
|
+
[K in keyof T as IsPlural<RemovePluralSuffix<K & string> & PossibleTranslationKeys> extends true ? K | PluralKeys<RemovePluralSuffix<K & string>> : never]?: T[K] extends object ? GenerateTranslationType<T[K]> : string;
|
|
50
59
|
};
|
|
51
|
-
type
|
|
60
|
+
type ExtractPlaceholders<T extends string, Acc = never> = T extends `${infer Start}{${infer P}}${infer Rest}` ? Start extends `${infer _}{${infer _}` ? Acc : ExtractPlaceholders<Rest, Acc | ParsePlaceholder<P>> : Acc;
|
|
61
|
+
type ParsePlaceholder<P extends string> = P extends `${infer Name}:${infer Type}|${infer _}` ? {
|
|
52
62
|
name: Name;
|
|
53
|
-
type: TypeMap[Type];
|
|
54
|
-
} : {
|
|
55
|
-
name: Name;
|
|
56
|
-
type: ValueType;
|
|
57
|
-
} : T extends `${infer Name}:${infer Type}` ? Type extends keyof TypeMap ? {
|
|
63
|
+
type: Type extends keyof TypeMap ? TypeMap[Type] : ValueType;
|
|
64
|
+
} : P extends `${infer Name}:${infer Type}` ? {
|
|
58
65
|
name: Name;
|
|
59
|
-
type: TypeMap[Type];
|
|
60
|
-
} : {
|
|
61
|
-
name: Name;
|
|
62
|
-
type: ValueType;
|
|
63
|
-
} : T extends `${infer Name}|${infer _Rest}` ? {
|
|
66
|
+
type: Type extends keyof TypeMap ? TypeMap[Type] : ValueType;
|
|
67
|
+
} : P extends `${infer Name}|${infer _}` ? {
|
|
64
68
|
name: Name;
|
|
65
69
|
type: ValueType;
|
|
66
70
|
} : {
|
|
67
|
-
name:
|
|
71
|
+
name: P;
|
|
68
72
|
type: ValueType;
|
|
69
73
|
};
|
|
70
|
-
type ExtractPlaceholdersWithTypes<T extends string, Acc = never> = T extends `${infer _Start}{${infer Placeholder}}${infer Rest}` ? ExtractPlaceholdersWithTypes<Rest, Acc | ExtractPlaceholderInfo<Placeholder>> : Acc;
|
|
71
74
|
type PlaceholderInfoToObject<T> = {
|
|
72
75
|
[K in T extends {
|
|
73
76
|
name: infer N extends string;
|
|
@@ -77,16 +80,72 @@ type PlaceholderInfoToObject<T> = {
|
|
|
77
80
|
type: infer Type;
|
|
78
81
|
} ? Type : never;
|
|
79
82
|
};
|
|
80
|
-
type ExtractPlaceholders<T extends string> = T extends `${infer _Start}{${infer Placeholder}}${infer Rest}` ? (Placeholder extends `${infer Name}:${infer _TypeAndFormatters}` ? Name : Placeholder extends `${infer Name}|${infer _Formatters}` ? Name : Placeholder) | ExtractPlaceholders<Rest> : never;
|
|
81
83
|
type HasPluralKeys<T, Path extends string> = Path extends `${infer K}.${infer Rest}` ? K extends keyof T ? HasPluralKeys<T[K], Rest> : false : PluralKeys<Path> & keyof T extends never ? false : true;
|
|
82
84
|
type IsPlural<Path extends string> = HasPluralKeys<Translations, Path>;
|
|
83
|
-
type InternalGetValue<T, Path extends string> = Path extends `${infer K}.${infer Rest}` ? K extends keyof T ? InternalGetValue<T[K], Rest> :
|
|
84
|
-
type GetValue<
|
|
85
|
+
type InternalGetValue<T, Path extends string> = Path extends `${infer K}.${infer Rest}` ? K extends keyof T ? InternalGetValue<T[K], Rest> : string : Path extends keyof T ? T[Path] : T[PluralKeys<Path> & keyof T] extends infer V ? unknown extends V ? string : Exclude<V, undefined> : string;
|
|
86
|
+
type GetValue<S extends PossibleTranslationKeys> = Exclude<InternalGetValue<Translations, S>, undefined>;
|
|
85
87
|
export type DeepResolve<T> = T extends (...args: any[]) => any ? T : T extends object ? {
|
|
86
88
|
-readonly [K in keyof T]: DeepResolve<T[K]>;
|
|
87
89
|
} : T;
|
|
88
|
-
export declare const getTranslate: (translations: TranslationType, locale: Locales, extraFormatters: ExtraFormatters, baseTranslate?: (...props: any) => string) => <Key extends PossibleTranslationKeys>(key: Key, ...arguments_: InterpolationProperties<
|
|
89
|
-
export {
|
|
90
|
+
export declare const getTranslate: (translations: TranslationType, locale: Locales, extraFormatters: ExtraFormatters, baseTranslate?: (...props: any) => string) => <Key extends PossibleTranslationKeys>(key: Key, ...arguments_: InterpolationProperties<Key> extends Record<string, never> ? [] : Key extends PossibleTranslationKeys ? [params: InterpolationProperties<Key>] : []) => GetValue<Key> & TranslatedMark;
|
|
91
|
+
export declare const createNamespaceTranslate: <NameSpace extends NameSpaces>(namespace: NameSpace, translateFn: TranslateFunctionType) => <Key extends NamespaceKeys<NameSpace>>(key: Key, ...args: InterpolationProperties<`${NameSpace}.${Key}` & PossibleTranslationKeys> extends Record<string, never> ? [] : `${NameSpace}.${Key}` & PossibleTranslationKeys extends PossibleTranslationKeys ? [params: InterpolationProperties<`${NameSpace}.${Key}` & PossibleTranslationKeys>] : []) => (Exclude<InternalGetValue<{
|
|
92
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
93
|
+
readonly nested: {
|
|
94
|
+
readonly deep: {
|
|
95
|
+
readonly hello: "Hello";
|
|
96
|
+
readonly again: "Nested again {value:number|number}";
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
readonly test2_one: "Plural one";
|
|
100
|
+
readonly test2_other: "Plural: {count}";
|
|
101
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
102
|
+
}, `${NameSpace}.${Key}` & "test">, undefined> | Exclude<InternalGetValue<{
|
|
103
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
104
|
+
readonly nested: {
|
|
105
|
+
readonly deep: {
|
|
106
|
+
readonly hello: "Hello";
|
|
107
|
+
readonly again: "Nested again {value:number|number}";
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
readonly test2_one: "Plural one";
|
|
111
|
+
readonly test2_other: "Plural: {count}";
|
|
112
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
113
|
+
}, `${NameSpace}.${Key}` & "customFormatter">, undefined> | Exclude<InternalGetValue<{
|
|
114
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
115
|
+
readonly nested: {
|
|
116
|
+
readonly deep: {
|
|
117
|
+
readonly hello: "Hello";
|
|
118
|
+
readonly again: "Nested again {value:number|number}";
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
readonly test2_one: "Plural one";
|
|
122
|
+
readonly test2_other: "Plural: {count}";
|
|
123
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
124
|
+
}, `${NameSpace}.${Key}` & "nested.deep.hello">, undefined> | Exclude<InternalGetValue<{
|
|
125
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
126
|
+
readonly nested: {
|
|
127
|
+
readonly deep: {
|
|
128
|
+
readonly hello: "Hello";
|
|
129
|
+
readonly again: "Nested again {value:number|number}";
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
readonly test2_one: "Plural one";
|
|
133
|
+
readonly test2_other: "Plural: {count}";
|
|
134
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
135
|
+
}, `${NameSpace}.${Key}` & "nested.deep.again">, undefined> | Exclude<InternalGetValue<{
|
|
136
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
137
|
+
readonly nested: {
|
|
138
|
+
readonly deep: {
|
|
139
|
+
readonly hello: "Hello";
|
|
140
|
+
readonly again: "Nested again {value:number|number}";
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
readonly test2_one: "Plural one";
|
|
144
|
+
readonly test2_other: "Plural: {count}";
|
|
145
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
146
|
+
}, `${NameSpace}.${Key}` & "test2">, undefined>) & TranslatedMark;
|
|
147
|
+
export { initReact } from './adapters/react.js';
|
|
148
|
+
export { initZustand } from './adapters/zustand.js';
|
|
90
149
|
export { type Formatter, default as defaultFormatters } from './formatters.js';
|
|
91
150
|
export { type ValidateTranslation, type EnsureValidTranslation, } from './validation.js';
|
|
92
151
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,UAAU,EAAE,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG7D,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC;AAGpE,MAAM,MAAM,OAAO,GAAG;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,GAAG,EAAE,CAAC;IACb,IAAI,EAAE,IAAI,CAAC;IACX,SAAS,EAAE,SAAS,CAAC;IACr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"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
1
2
|
import baseFormatters from './formatters.js';
|
|
2
|
-
// TODO add more
|
|
3
3
|
const pluralSufixes = ['_none', '_one', '_other'];
|
|
4
|
-
//
|
|
4
|
+
// Runtime implementation remains the same
|
|
5
5
|
export const getTranslate = (translations, locale, extraFormatters, baseTranslate) => {
|
|
6
6
|
const formatters = { ...baseFormatters, ...extraFormatters };
|
|
7
7
|
function translate(key, ...arguments_) {
|
|
@@ -82,5 +82,12 @@ export const getTranslate = (translations, locale, extraFormatters, baseTranslat
|
|
|
82
82
|
}
|
|
83
83
|
return translate;
|
|
84
84
|
};
|
|
85
|
-
export
|
|
85
|
+
export const createNamespaceTranslate = (namespace, translateFn) => {
|
|
86
|
+
return (key, ...args) => {
|
|
87
|
+
const fullKey = `${namespace}.${key}`;
|
|
88
|
+
return translateFn(fullKey, ...args);
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
export { initReact } from './adapters/react.js';
|
|
92
|
+
export { initZustand } from './adapters/zustand.js';
|
|
86
93
|
export { default as defaultFormatters } from './formatters.js';
|
package/eslint.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typed-locales",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.45",
|
|
4
4
|
"description": "Type safe utilities for translating strings",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"react": "^19.1.0",
|
|
20
20
|
"rimraf": "^6.0.1",
|
|
21
21
|
"tsx": "^4.19.4",
|
|
22
|
-
"typescript": "^5.8.3"
|
|
22
|
+
"typescript": "^5.8.3",
|
|
23
|
+
"zustand": "^5.0.5"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@eslint/js": "^9.28.0",
|
package/dist/react.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { getTranslate, type ExtraFormatters, type Locales, type TranslationType } from './index.js';
|
|
3
|
-
export interface TranslationContextType {
|
|
4
|
-
isLoading: boolean;
|
|
5
|
-
locale: Locales;
|
|
6
|
-
setLocale: (locale: Locales) => Promise<Locales>;
|
|
7
|
-
t: ReturnType<typeof getTranslate>;
|
|
8
|
-
}
|
|
9
|
-
export declare const initReact: (initialTranslation: TranslationType, initialLocale: Locales, allTranslations: Record<Locales, (() => Promise<{
|
|
10
|
-
default: TranslationType;
|
|
11
|
-
}>) | TranslationType>, extraFormatters: ExtraFormatters) => {
|
|
12
|
-
TranslationProvider: ({ children }: {
|
|
13
|
-
children: React.ReactNode;
|
|
14
|
-
}) => import("react/jsx-runtime").JSX.Element;
|
|
15
|
-
useTranslation: () => TranslationContextType;
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=react.d.ts.map
|
package/dist/react.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA2D,MAAM,OAAO,CAAC;AAEhF,OAAO,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,KAAK,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AAEpG,MAAM,WAAW,sBAAsB;IACtC,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IACjD,CAAC,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;CACnC;AAGD,eAAO,MAAM,SAAS,GACrB,oBAAoB,eAAe,EACnC,eAAe,OAAO,EACtB,iBAAiB,MAAM,CAAC,OAAO,EAAE,CAAC,MAAM,OAAO,CAAC;IAAC,OAAO,EAAE,eAAe,CAAA;CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,EAC/F,iBAAiB,eAAe;wCAKW;QAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;KAAE;;CAkExE,CAAC"}
|