typed-locales 1.0.42 → 1.0.44
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/README.md +1 -0
- package/dist/index.d.ts +82 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/react.d.ts +7 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +9 -5
- package/docs/helper-types.md +34 -0
- package/eslint.config.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -105,6 +105,7 @@ translate('items', { count: 5 }); // "5 elementos"
|
|
|
105
105
|
- [Formatters Guide](./docs/formatters.md) - Built-in and custom formatters
|
|
106
106
|
- [Validation System](./docs/validation.md) - Compile-time validation features
|
|
107
107
|
- [VS Code Setup](./docs/vscode-setup.md) - IDE integration guide
|
|
108
|
+
- [Helper Types](./docs/vscode-setup.md) - Helper types for advanced use
|
|
108
109
|
|
|
109
110
|
## 🛠️ TypeScript Configuration
|
|
110
111
|
|
package/dist/index.d.ts
CHANGED
|
@@ -19,18 +19,40 @@ 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<S extends NameSpaces> = FilterByPrefix<PossibleTranslationKeys, S>;
|
|
35
|
+
type TranslatedMark = {
|
|
36
|
+
translated: true;
|
|
37
|
+
};
|
|
38
|
+
/** Ensure a string is translated */
|
|
39
|
+
export type TranslatedString = string & TranslatedMark;
|
|
40
|
+
export type InterpolationProperties<Key extends PossibleTranslationKeys, Value extends string = GetValue<Key>, IsKeyPlural = IsPlural<Value>> = DeepResolve<IsKeyPlural extends true ? {
|
|
41
|
+
count: number;
|
|
42
|
+
} & PlaceholderInfoToObject<Exclude<ExtractPlaceholdersWithTypes<Value>, {
|
|
43
|
+
name: 'count';
|
|
44
|
+
type: any;
|
|
45
|
+
}>> : ExtractPlaceholders<Value> extends never ? {} : PlaceholderInfoToObject<ExtractPlaceholdersWithTypes<Value>>>;
|
|
28
46
|
declare const pluralSufixes: readonly ["_none", "_one", "_other"];
|
|
29
47
|
type PluralSuffix = (typeof pluralSufixes)[number];
|
|
48
|
+
type FilterByPrefix<T, Prefix extends NameSpaces> = T extends `${Prefix}.${infer Rest}` ? Rest : never;
|
|
30
49
|
type RemovePluralSuffix<T extends string> = T extends `${infer Base}${PluralSuffix}` ? Base : T;
|
|
31
50
|
type PluralKeys<Base extends string> = `${Base}${PluralSuffix}`;
|
|
32
|
-
type
|
|
33
|
-
[K in keyof T]: K extends string ? T[K] extends Record<string, any> ? `${K}.${
|
|
51
|
+
type DotNestedStringKeys<T> = {
|
|
52
|
+
[K in keyof T]: K extends string ? T[K] extends Record<string, any> ? `${K}.${DotNestedStringKeys<T[K]>}` : RemovePluralSuffix<K> : never;
|
|
53
|
+
}[keyof T];
|
|
54
|
+
type DotNestedObjectKeys<T> = {
|
|
55
|
+
[K in keyof T]: K extends string ? T[K] extends Record<string, any> ? K | `${K}.${DotNestedObjectKeys<T[K]>}` : never : never;
|
|
34
56
|
}[keyof T];
|
|
35
57
|
export type GenerateTranslationType<T> = {
|
|
36
58
|
[K in keyof T as IsPlural<RemovePluralSuffix<K & string>> extends true ? never : K]: T[K] extends object ? GenerateTranslationType<T[K]> : string;
|
|
@@ -71,16 +93,66 @@ type HasPluralKeys<T, Path extends string> = Path extends `${infer K}.${infer Re
|
|
|
71
93
|
type IsPlural<Path extends string> = HasPluralKeys<Translations, Path>;
|
|
72
94
|
type InternalGetValue<T, Path extends string> = Path extends `${infer K}.${infer Rest}` ? K extends keyof T ? InternalGetValue<T[K], Rest> : never : Path extends keyof T ? T[Path] : T[PluralKeys<Path> & keyof T];
|
|
73
95
|
type GetValue<Path extends string> = Exclude<InternalGetValue<Translations, Path>, undefined>;
|
|
74
|
-
type InterpolationProperties<S extends string, IsKeyPlural = IsPlural<S>> = DeepResolve<IsKeyPlural extends true ? {
|
|
75
|
-
count: number;
|
|
76
|
-
} & PlaceholderInfoToObject<Exclude<ExtractPlaceholdersWithTypes<S>, {
|
|
77
|
-
name: 'count';
|
|
78
|
-
type: any;
|
|
79
|
-
}>> : ExtractPlaceholders<S> extends never ? {} : PlaceholderInfoToObject<ExtractPlaceholdersWithTypes<S>>>;
|
|
80
96
|
export type DeepResolve<T> = T extends (...args: any[]) => any ? T : T extends object ? {
|
|
81
97
|
-readonly [K in keyof T]: DeepResolve<T[K]>;
|
|
82
98
|
} : T;
|
|
83
|
-
export declare const getTranslate: (translations: TranslationType, locale: Locales, extraFormatters: ExtraFormatters, baseTranslate?: (...props: any) => string) => <Key extends PossibleTranslationKeys>(key: Key, ...arguments_: InterpolationProperties<
|
|
99
|
+
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;
|
|
100
|
+
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<{
|
|
101
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
102
|
+
readonly nested: {
|
|
103
|
+
readonly deep: {
|
|
104
|
+
readonly hello: "Hello";
|
|
105
|
+
readonly again: "Nested again {value:number|number}";
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
readonly test2_one: "Plural one";
|
|
109
|
+
readonly test2_other: "Plural: {count}";
|
|
110
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
111
|
+
}, `${NameSpace}.${Key}` & "test">, undefined> | Exclude<InternalGetValue<{
|
|
112
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
113
|
+
readonly nested: {
|
|
114
|
+
readonly deep: {
|
|
115
|
+
readonly hello: "Hello";
|
|
116
|
+
readonly again: "Nested again {value:number|number}";
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
readonly test2_one: "Plural one";
|
|
120
|
+
readonly test2_other: "Plural: {count}";
|
|
121
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
122
|
+
}, `${NameSpace}.${Key}` & "customFormatter">, undefined> | Exclude<InternalGetValue<{
|
|
123
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
124
|
+
readonly nested: {
|
|
125
|
+
readonly deep: {
|
|
126
|
+
readonly hello: "Hello";
|
|
127
|
+
readonly again: "Nested again {value:number|number}";
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
readonly test2_one: "Plural one";
|
|
131
|
+
readonly test2_other: "Plural: {count}";
|
|
132
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
133
|
+
}, `${NameSpace}.${Key}` & "test2">, undefined> | Exclude<InternalGetValue<{
|
|
134
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
135
|
+
readonly nested: {
|
|
136
|
+
readonly deep: {
|
|
137
|
+
readonly hello: "Hello";
|
|
138
|
+
readonly again: "Nested again {value:number|number}";
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
readonly test2_one: "Plural one";
|
|
142
|
+
readonly test2_other: "Plural: {count}";
|
|
143
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
144
|
+
}, `${NameSpace}.${Key}` & "nested.deep.hello">, undefined> | Exclude<InternalGetValue<{
|
|
145
|
+
readonly test: "Hello {who:string|capitalize}!";
|
|
146
|
+
readonly nested: {
|
|
147
|
+
readonly deep: {
|
|
148
|
+
readonly hello: "Hello";
|
|
149
|
+
readonly again: "Nested again {value:number|number}";
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
readonly test2_one: "Plural one";
|
|
153
|
+
readonly test2_other: "Plural: {count}";
|
|
154
|
+
readonly customFormatter: "Test formatter {data|customFormatter}";
|
|
155
|
+
}, `${NameSpace}.${Key}` & "nested.deep.again">, undefined>) & TranslatedMark;
|
|
84
156
|
export { initReact } from './react.js';
|
|
85
157
|
export { type Formatter, default as defaultFormatters } from './formatters.js';
|
|
86
158
|
export { type ValidateTranslation, type EnsureValidTranslation, } from './validation.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,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,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C/C,SAAS,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GAC5B,EAAE,GACF,GAAG,SAAS,IAAI,GAAG,EAAE,GACpB,uBAAuB,SAAS,uBAAuB,GACvD,CACA,MAAM,EAAE,uBAAuB,CAC9B,GAAG,SAAS,IAAI,GAAG,EAAE,GAAG,uBAAuB,CAC/C,CACD,GACA,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6EAMP,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,KAAK,SAAS,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE/E,OAAO,EACN,KAAK,mBAAmB,EACxB,KAAK,sBAAsB,GAC3B,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -82,5 +82,11 @@ export const getTranslate = (translations, locale, extraFormatters, baseTranslat
|
|
|
82
82
|
}
|
|
83
83
|
return translate;
|
|
84
84
|
};
|
|
85
|
+
export const createNamespaceTranslate = (namespace, translateFn) => {
|
|
86
|
+
return (key, ...args) => {
|
|
87
|
+
const fullKey = `${namespace}.${key}`;
|
|
88
|
+
return translateFn(fullKey, ...args);
|
|
89
|
+
};
|
|
90
|
+
};
|
|
85
91
|
export { initReact } from './react.js';
|
|
86
92
|
export { default as defaultFormatters } from './formatters.js';
|
package/dist/react.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { getTranslate, type ExtraFormatters, type Locales, type TranslationType } from './index.js';
|
|
2
|
+
import { getTranslate, createNamespaceTranslate, type ExtraFormatters, type Locales, type TranslationType, type NameSpaces } from './index.js';
|
|
3
3
|
export interface TranslationContextType {
|
|
4
4
|
isLoading: boolean;
|
|
5
5
|
locale: Locales;
|
|
@@ -12,6 +12,11 @@ export declare const initReact: (initialTranslation: TranslationType, initialLoc
|
|
|
12
12
|
TranslationProvider: ({ children }: {
|
|
13
13
|
children: React.ReactNode;
|
|
14
14
|
}) => import("react/jsx-runtime").JSX.Element;
|
|
15
|
-
useTranslation:
|
|
15
|
+
useTranslation: {
|
|
16
|
+
(): TranslationContextType;
|
|
17
|
+
<NS extends NameSpaces>(namespace: NS): Omit<TranslationContextType, "t"> & {
|
|
18
|
+
t: ReturnType<typeof createNamespaceTranslate<NS>>;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
16
21
|
};
|
|
17
22
|
//# sourceMappingURL=react.d.ts.map
|
package/dist/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/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,YAAY,CAAC;AAEpB,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;IAAE,OAAO,EAAE,eAAe,CAAA;CAAE,CAAC,CAAC,GAAG,eAAe,CAAC,EACjG,iBAAiB,eAAe;wCAKW;QAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;KAAE;;YAwD7C,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"}
|
package/dist/react.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useCallback, useContext, useState } from 'react';
|
|
3
|
-
import { getTranslate } from './index.js';
|
|
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
5
|
export const initReact = (initialTranslation, initialLocale, allTranslations, extraFormatters) => {
|
|
6
6
|
const TranslationContext = createContext(undefined);
|
|
@@ -41,12 +41,16 @@ export const initReact = (initialTranslation, initialLocale, allTranslations, ex
|
|
|
41
41
|
t: state.translate,
|
|
42
42
|
}, children: children }));
|
|
43
43
|
};
|
|
44
|
-
|
|
44
|
+
function useTranslation(namespace) {
|
|
45
45
|
const context = useContext(TranslationContext);
|
|
46
46
|
if (!context)
|
|
47
47
|
throw new Error('useTranslation must be used within a TranslationProvider');
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
const t = useMemo(() => namespace ? createNamespaceTranslate(namespace, context.t) : context.t, [namespace, context]);
|
|
49
|
+
return {
|
|
50
|
+
...context,
|
|
51
|
+
t,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
50
54
|
return {
|
|
51
55
|
TranslationProvider,
|
|
52
56
|
useTranslation,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
|
|
2
|
+
# Helper Types
|
|
3
|
+
Sometimes you may want to recieve a translation key as a function parameter, get the parameters of a translation as a type, or ensure a function parameter is translated, for this 3 helper types are exposed.
|
|
4
|
+
|
|
5
|
+
## PossibleTranslationKeys
|
|
6
|
+
Ensure a string is a valid translation key
|
|
7
|
+
```ts
|
|
8
|
+
const formatLabel = (key: PossibleTranslationKeys) => t(key);
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## InterpolationProperties
|
|
12
|
+
Get the properties of a given translation key
|
|
13
|
+
```ts
|
|
14
|
+
const formatLabel = (props: InterpolationProperties<'labelWithProps'>) => t('labelWithProps', props);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## TranslatedString
|
|
18
|
+
Ensure a recieved string is translated
|
|
19
|
+
```ts
|
|
20
|
+
const print = (string_: TranslatedString) => console.log(string_);
|
|
21
|
+
|
|
22
|
+
print(t('hello')); // This is ok
|
|
23
|
+
print('hello'); // This show an error
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This is usefull for react components, for example
|
|
27
|
+
```tsx
|
|
28
|
+
const Label: React.FC<{
|
|
29
|
+
label: TranslatedString
|
|
30
|
+
}> = ({label}) => (<label>{label}</label>);
|
|
31
|
+
|
|
32
|
+
const test = <Label label={t('hello')}} /> // This is ok
|
|
33
|
+
const test2 = <Label label="hello" /> // This shows an error
|
|
34
|
+
```
|
package/eslint.config.js
CHANGED