typed-locales 1.0.9 → 1.0.10
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/index.d.ts +21 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/src/react.d.ts +15 -0
- package/dist/src/react.d.ts.map +1 -0
- package/dist/src/react.js +3 -3
- package/dist/src/tests/config.d.ts +16 -0
- package/dist/src/tests/config.d.ts.map +1 -0
- package/dist/src/tests/config.js +2 -4
- package/dist/src/tests/translations/en.d.ts.map +1 -1
- package/dist/src/tests/translations/es.d.ts.map +1 -1
- package/dist/src/validation.d.ts +6 -6
- package/dist/src/validation.d.ts.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import baseFormatters from './src/formatters';
|
|
2
2
|
import type { Formatter } from './src/formatters';
|
|
3
3
|
type ValueType = null | number | string | undefined | object;
|
|
4
|
-
export
|
|
4
|
+
export interface DefaultOverrides {
|
|
5
|
+
shape: any;
|
|
6
|
+
extraFormatters?: Record<string, Formatter>;
|
|
7
|
+
locales: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Overrides extends DefaultOverrides {
|
|
10
|
+
}
|
|
11
|
+
export type Translations = Overrides['shape'];
|
|
12
|
+
export type OtherTranslations = DeepStringify<Translations>;
|
|
13
|
+
export type TranslationSchema = GenerateTranslationType<Translations>;
|
|
14
|
+
export type Locales = Overrides['locales'];
|
|
15
|
+
export type BaseFormatters = typeof baseFormatters;
|
|
16
|
+
export type ExtraFormatters = Overrides['extraFormatters'];
|
|
17
|
+
export type Formatters = BaseFormatters & ExtraFormatters;
|
|
18
|
+
export type FormattersKeys = keyof Formatters;
|
|
19
|
+
export type PossibleTranslationKeys = DotNestedLeafKeys<Translations>;
|
|
5
20
|
export type InternalDeepStringify<T> = {
|
|
6
21
|
[K in keyof T]: T[K] extends string ? string : T[K] extends object ? InternalDeepStringify<T[K]> : never;
|
|
7
22
|
};
|
|
@@ -21,12 +36,13 @@ type DotNestedLeafKeys<T> = {
|
|
|
21
36
|
}[keyof T];
|
|
22
37
|
type GenerateStringFromProperties<T extends Record<string, any>> = T extends Record<string, never> ? string : `${string}{${keyof T & string}${string}` | `${string}{${keyof T & string}|${string}}${string}`;
|
|
23
38
|
export type GenerateTranslationType<T> = {
|
|
24
|
-
-readonly [K in keyof T]: T[K] extends object ? Simplify<GenerateTranslationType<T[K]>> : GenerateStringFromProperties<InterpolationProperties<RemovePluralSuffix<T[K] & string>, IsPlural<
|
|
39
|
+
-readonly [K in keyof T]: T[K] extends object ? Simplify<GenerateTranslationType<T[K]>> : GenerateStringFromProperties<InterpolationProperties<RemovePluralSuffix<T[K] & string>, IsPlural<RemovePluralSuffix<K & string>>>>;
|
|
25
40
|
};
|
|
26
41
|
type ExtractPlaceholders<T extends string> = T extends `${infer _Start}{${infer Placeholder}}${infer Rest}` ? (Placeholder extends `${infer Name}|${infer _Formatters}` ? Name : Placeholder) | ExtractPlaceholders<Rest> : never;
|
|
27
42
|
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;
|
|
28
|
-
type IsPlural<
|
|
29
|
-
type
|
|
43
|
+
type IsPlural<Path extends string> = HasPluralKeys<Translations, Path>;
|
|
44
|
+
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];
|
|
45
|
+
type GetValue<Path extends string> = InternalGetValue<Translations, Path>;
|
|
30
46
|
type InterpolationProperties<S extends string, IsPlural extends boolean> = IsPlural extends true ? {
|
|
31
47
|
count: number;
|
|
32
48
|
} & {
|
|
@@ -41,7 +57,7 @@ type DeepOmitNever<T> = {
|
|
|
41
57
|
[K in keyof T as T[K] extends never ? never : T[K] extends Record<string, any> ? DeepOmitNever<T[K]> extends never ? never : K : K]: T[K] extends Record<string, any> ? DeepOmitNever<T[K]> : T[K];
|
|
42
58
|
} extends infer U ? keyof U extends never ? never : U : never;
|
|
43
59
|
export type Simplify<T> = InternalSimplify<DeepOmitNever<T>>;
|
|
44
|
-
export declare const getTranslate:
|
|
60
|
+
export declare const getTranslate: (translations: Translations, locale: Locales, extraFormatters: ExtraFormatters) => <Key extends PossibleTranslationKeys>(key: Key, ...arguments_: InterpolationProperties<GetValue<Key>, IsPlural<Key>> extends Record<string, never> ? [] : [params: Simplify<InterpolationProperties<GetValue<Key> & string, IsPlural<Key>>>]) => GetValue<Key>;
|
|
45
61
|
export { initReact } from './src/react';
|
|
46
62
|
export { type Formatter, default as defaultFormatters } from './src/formatters';
|
|
47
63
|
export { type ValidateTranslation, type EnsureValidTranslation } from './src/validation';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAGlD,KAAK,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC;AAE7D,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAGlD,KAAK,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC;AAE7D,MAAM,WAAW,gBAAgB;IAChC,KAAK,EAAE,GAAG,CAAC;IACX,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAC5C,O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YAAY,EAAE,QAAQ,OAAO,EAAE,iBAAiB,eAAe,MAQxG,GAAG,SAAS,uBAAuB,OAE7B,GAAG,iBACO,uBAAuB,CACrC,QAAQ,CAAC,GAAG,CAAC,EACb,QAAQ,CAAC,GAAG,CAAC,CACb,SAAS,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GAC5B,EAAE,GACF,CACD,MAAM,EAAE,QAAQ,CACf,uBAAuB,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,MAAM,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAC9D,CACD,KACD,QAAQ,CAAC,GAAG,CAgFf,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EAAE,KAAK,SAAS,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAEhF,OAAO,EAAE,KAAK,mBAAmB,EAAE,KAAK,sBAAsB,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import baseFormatters from './src/formatters';
|
|
2
|
+
;
|
|
3
|
+
;
|
|
2
4
|
// Given a translations object returns a function that can be used to translate keys
|
|
3
5
|
export const getTranslate = (translations, locale, extraFormatters) => {
|
|
4
6
|
const formatters = { ...baseFormatters, ...extraFormatters };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getTranslate, type Locales, type ExtraFormatters, type OtherTranslations } from '../index';
|
|
3
|
+
export interface TranslationContextType {
|
|
4
|
+
isLoading: boolean;
|
|
5
|
+
locale: Locales;
|
|
6
|
+
setLocale: (locale: Locales) => void;
|
|
7
|
+
t: ReturnType<typeof getTranslate>;
|
|
8
|
+
}
|
|
9
|
+
export declare const initReact: (allTranslations: Record<Locales, OtherTranslations | (() => Promise<OtherTranslations>)>, initialLocale: Locales, extraFormatters: ExtraFormatters) => {
|
|
10
|
+
TranslationProvider: ({ children }: {
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
useTranslation: () => TranslationContextType;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/react.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA8C,MAAM,OAAO,CAAC;AAEnE,OAAO,EAAE,YAAY,EAAE,KAAK,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAAqB,MAAM,UAAU,CAAC;AAEvH,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;;CA0DxE,CAAC"}
|
package/dist/src/react.js
CHANGED
|
@@ -2,15 +2,15 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { createContext, useContext, useState } from 'react';
|
|
3
3
|
import { getTranslate } from '../index';
|
|
4
4
|
// Initial translation always should be loaded
|
|
5
|
-
export const initReact = (
|
|
5
|
+
export const initReact = (allTranslations, initialLocale, extraFormatters) => {
|
|
6
6
|
const TranslationContext = createContext(undefined);
|
|
7
7
|
const TranslationProvider = ({ children }) => {
|
|
8
8
|
const [locale, setLocale] = useState(initialLocale);
|
|
9
|
-
const [translate, setTranslate] = useState(() => getTranslate(
|
|
9
|
+
const [translate, setTranslate] = useState(() => getTranslate(allTranslations[locale], locale, extraFormatters));
|
|
10
10
|
const [isLoading, setIsLoading] = useState(true);
|
|
11
11
|
const loadTranslation = async (targetLocale) => {
|
|
12
12
|
try {
|
|
13
|
-
const translationOrLoader =
|
|
13
|
+
const translationOrLoader = allTranslations[targetLocale];
|
|
14
14
|
let translationData;
|
|
15
15
|
if (typeof translationOrLoader === 'function') {
|
|
16
16
|
setIsLoading(true);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import en from './translations/en';
|
|
2
|
+
declare const customFormatters: {
|
|
3
|
+
readonly myCustomFormatter: () => string;
|
|
4
|
+
};
|
|
5
|
+
export declare const useTranslation: () => import("../react").TranslationContextType, TranslationProvider: ({ children }: {
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
declare module '../../index' {
|
|
9
|
+
interface Overrides {
|
|
10
|
+
shape: typeof en;
|
|
11
|
+
extraFormatters: typeof customFormatters;
|
|
12
|
+
locales: 'en' | 'es';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/tests/config.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAInC,QAAA,MAAM,gBAAgB;;CAEwB,CAAC;AAE/C,eAAO,MAAQ,cAAc,mDAAE,mBAAmB;;6CAAgD,CAAC;AAEnG,OAAO,QAAQ,aAAa,CAAC;IAC5B,UAAU,SAAS;QAClB,KAAK,EAAE,OAAO,EAAE,CAAC;QACjB,eAAe,EAAE,OAAO,gBAAgB,CAAC;QACzC,OAAO,EAAE,IAAI,GAAG,IAAI,CAAC;KACrB;CACD"}
|
package/dist/src/tests/config.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { initReact } from '../react';
|
|
2
2
|
import en from './translations/en';
|
|
3
|
+
import es from './translations/es';
|
|
3
4
|
const customFormatters = {
|
|
4
5
|
myCustomFormatter: () => 'Hello im custom',
|
|
5
6
|
};
|
|
6
|
-
export const { useTranslation, TranslationProvider } = initReact({
|
|
7
|
-
en,
|
|
8
|
-
es: () => import('./translations/es').then(m => m.default),
|
|
9
|
-
}, 'en', customFormatters);
|
|
7
|
+
export const { useTranslation, TranslationProvider } = initReact({ en, es }, 'en', customFormatters);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"en.d.ts","sourceRoot":"","sources":["../../../../src/tests/translations/en.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"en.d.ts","sourceRoot":"","sources":["../../../../src/tests/translations/en.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BE,CAAC;AAEX,eAAe,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"es.d.ts","sourceRoot":"","sources":["../../../../src/tests/translations/es.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"es.d.ts","sourceRoot":"","sources":["../../../../src/tests/translations/es.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;CA6B8B,CAAC;AAEvC,eAAe,EAAE,CAAC"}
|
package/dist/src/validation.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FormattersKeys, RemoveReadonlyDeep } from "../index";
|
|
2
2
|
type ErrorMessage<Value extends string, T extends string> = `You are using an invalid formatter: ${T} in: "${Value}"`;
|
|
3
3
|
type ExtractFormatter<T extends string> = T extends `${string}{${string}|${infer F}}${string}` ? F : never;
|
|
4
4
|
type CountOpenBraces<T extends string, Count extends readonly unknown[] = []> = T extends `${infer First}${infer Rest}` ? First extends '{' ? CountOpenBraces<Rest, [...Count, unknown]> : CountOpenBraces<Rest, Count> : Count['length'];
|
|
5
5
|
type CountCloseBraces<T extends string, Count extends readonly unknown[] = []> = T extends `${infer First}${infer Rest}` ? First extends '}' ? CountCloseBraces<Rest, [...Count, unknown]> : CountCloseBraces<Rest, Count> : Count['length'];
|
|
6
6
|
type BalancedBraces<T extends string> = CountOpenBraces<T> extends CountCloseBraces<T> ? never : `Brackets are not balanced in: "${T}"`;
|
|
7
|
-
type ValidateFormatter<T extends string
|
|
8
|
-
type InternalValidateTranslation<T,
|
|
9
|
-
[K in keyof T]: InternalValidateTranslation<T[K],
|
|
10
|
-
} : T extends string ? ValidateFormatter<T
|
|
11
|
-
export type ValidateTranslation<T
|
|
7
|
+
type ValidateFormatter<T extends string> = ExtractFormatter<T> extends never ? BalancedBraces<T> : ExtractFormatter<T> extends FormattersKeys ? BalancedBraces<T> : ErrorMessage<T, ExtractFormatter<T>>;
|
|
8
|
+
type InternalValidateTranslation<T, KeyPath extends string = ''> = T extends Record<string, any> ? {
|
|
9
|
+
[K in keyof T]: InternalValidateTranslation<T[K], KeyPath extends '' ? K & string : `${KeyPath}.${K & string}`>;
|
|
10
|
+
} : T extends string ? ValidateFormatter<T> : T;
|
|
11
|
+
export type ValidateTranslation<T> = RemoveReadonlyDeep<InternalValidateTranslation<T>>;
|
|
12
12
|
export type EnsureValidTranslation<T extends never> = T;
|
|
13
13
|
export {};
|
|
14
14
|
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnE,KAAK,YAAY,CAAC,KAAK,SAAS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CAAC,CAAC,GACjB,YAAY,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC;AAGxC,KAAK,2BAA2B,CAAC,CAAC,EAAE,OAAO,SAAS,MAAM,GAAG,EAAE,IAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC7F;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,2BAA2B,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,SAAS,EAAE,GAAG,CAAC,GAAG,MAAM,GAAG,GAAG,OAAO,IAAI,CAAC,GAAG,MAAM,EAAE,CAAC;CAAE,GACnH,CAAC,SAAS,MAAM,GAChB,iBAAiB,CAAC,CAAC,CAAC,GACpB,CAAC,CAAC;AAEL,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,kBAAkB,CAAC,2BAA2B,CAAC,CAAC,CAAC,CAAC,CAAC;AAExF,MAAM,MAAM,sBAAsB,CAAC,CAAC,SAAS,KAAK,IAAI,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typed-locales",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "Type safe utilities for translating strings",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "rimraf dist && tsc",
|
|
10
|
-
"dev": "tsx watch src/test.tsx"
|
|
10
|
+
"dev": "tsx watch src/test.tsx",
|
|
11
|
+
"deploy": "npm run build && npm publish --access public"
|
|
11
12
|
},
|
|
12
13
|
"keywords": [],
|
|
13
14
|
"author": "",
|