typed-locales 1.0.25 → 1.0.27

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/TODO CHANGED
@@ -1,3 +1,3 @@
1
1
  Fallback language
2
2
  Allow using some of the plural in a language but not in the other (both need to use at least one way)
3
- Force both keys being used for translations
3
+ Add an option that dosnt force you to define all plural types
package/dist/index.d.ts CHANGED
@@ -1,64 +1,46 @@
1
- import baseFormatters from './formatters.js';
2
- import type { Formatter } from './formatters.js';
1
+ import formatters, { type Formatter } from "./src/formatters";
3
2
  type ValueType = null | number | string | undefined | object;
4
3
  export interface DefaultOverrides {
5
- shape: Record<string, any>;
4
+ shape: object;
6
5
  extraFormatters?: Record<string, Formatter>;
7
6
  locales: string;
7
+ validateFormatters: true;
8
8
  }
9
9
  export interface Overrides extends DefaultOverrides {
10
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;
11
+ export type Translations = Overrides["shape"];
12
+ export type TranslationType = DeepResolve<GenerateTranslationType<Translations>>;
13
+ export type Locales = Overrides["locales"];
19
14
  export type PossibleTranslationKeys = DotNestedLeafKeys<Translations>;
20
- export type InternalDeepStringify<T> = {
21
- [K in keyof T]: T[K] extends string ? string : T[K] extends object ? InternalDeepStringify<T[K]> : never;
22
- };
23
- export type DeepStringify<T> = RemoveReadonlyDeep<InternalDeepStringify<T>>;
24
- export type InternalRemoveReadonlyDeep<T> = {
25
- -readonly [K in keyof T]: T[K] extends object ? InternalRemoveReadonlyDeep<T[K]> : T[K];
26
- };
27
- export type RemoveReadonlyDeep<T> = Simplify<InternalRemoveReadonlyDeep<T>>;
28
- type PluralSuffix = '_none' | '_one' | '_other';
29
- export type TranslationType = {
30
- [key: string]: string | TranslationType;
31
- };
15
+ export type ExtraFormatters = Overrides['extraFormatters'];
16
+ export type FormatterTypes = keyof ExtraFormatters | keyof typeof formatters;
17
+ declare const pluralSufixes: readonly ["_none", "_one", "_other"];
18
+ type PluralSuffix = (typeof pluralSufixes)[number];
32
19
  type RemovePluralSuffix<T extends string> = T extends `${infer Base}${PluralSuffix}` ? Base : T;
33
20
  type PluralKeys<Base extends string> = `${Base}${PluralSuffix}`;
34
21
  type DotNestedLeafKeys<T> = {
35
22
  [K in keyof T]: K extends string ? T[K] extends Record<string, any> ? `${K}.${DotNestedLeafKeys<T[K]>}` : RemovePluralSuffix<K> : never;
36
23
  }[keyof T];
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}`;
38
24
  export type GenerateTranslationType<T> = {
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
+ [K in keyof T as IsPlural<RemovePluralSuffix<K & string>> extends true ? K | PluralKeys<RemovePluralSuffix<K & string>> : K]: IsPlural<RemovePluralSuffix<K & string>> extends true ? (T[K] extends object ? GenerateTranslationType<T[K]> : string) | undefined : T[K] extends object ? GenerateTranslationType<T[K]> : string;
40
26
  };
41
27
  type ExtractPlaceholders<T extends string> = T extends `${infer _Start}{${infer Placeholder}}${infer Rest}` ? (Placeholder extends `${infer Name}|${infer _Formatters}` ? Name : Placeholder) | ExtractPlaceholders<Rest> : never;
42
28
  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;
43
29
  type IsPlural<Path extends string> = HasPluralKeys<Translations, Path>;
44
30
  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
31
  type GetValue<Path extends string> = InternalGetValue<Translations, Path>;
46
- type InterpolationProperties<S extends string, IsPlural extends boolean> = IsPlural extends true ? {
32
+ type InterpolationProperties<S extends string, IsPlural extends boolean, forceCount extends boolean> = IsPlural extends true ? (forceCount extends true ? {
47
33
  count: number;
48
- } & {
49
- [K in Exclude<ExtractPlaceholders<S>, 'count'>]: ValueType;
34
+ } : {}) & {
35
+ [K in Exclude<ExtractPlaceholders<S>, "count">]: ValueType;
50
36
  } : ExtractPlaceholders<S> extends never ? {} : {
51
37
  [K in ExtractPlaceholders<S>]: ValueType;
52
38
  };
53
- type InternalSimplify<T> = {
54
- [K in keyof T]: InternalSimplify<T[K]>;
55
- } & {};
56
- type DeepOmitNever<T> = {
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];
58
- } extends infer U ? keyof U extends never ? never : U : never;
59
- export type Simplify<T> = InternalSimplify<DeepOmitNever<T>>;
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>;
61
- export { initReact } from './react-utilities.js';
62
- export { type Formatter, default as defaultFormatters } from './formatters.js';
63
- export { type ValidateTranslation, type EnsureValidTranslation } from './validation.js';
39
+ export type DeepResolve<T> = T extends (...args: any[]) => any ? T : T extends object ? {
40
+ -readonly [K in keyof T]: DeepResolve<T[K]>;
41
+ } : T;
42
+ export declare const getTranslate: (translations: TranslationType, locale: Locales, extraFormatters: ExtraFormatters) => <Key extends PossibleTranslationKeys>(key: Key, ...arguments_: InterpolationProperties<GetValue<Key>, IsPlural<Key>, true> extends Record<string, never> ? [] : Key extends PossibleTranslationKeys ? [params: InterpolationProperties<GetValue<Key> & string, IsPlural<Key>, true>] : []) => GetValue<Key>;
43
+ export { initReact } from "./src/react";
44
+ export { type Formatter, default as defaultFormatters } from "./src/formatters";
45
+ export { type ValidateTranslation, type EnsureValidTranslation, } from "./src/validation";
64
46
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,cAAc,CAAC;AAC1C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAG9C,KAAK,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC;AAE7D,MAAM,WAAW,gBAAgB;IAChC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAC5C,OAAO,EAAE,MAAM,CAAC;CAChB;AACD,MAAM,WAAW,SAAW,SAAQ,gBAAgB;CAAG;AACvD,MAAM,MAAM,YAAY,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;AAC9C,MAAM,MAAM,iBAAiB,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC;AAC5D,MAAM,MAAM,iBAAiB,GAAG,uBAAuB,CAAC,YAAY,CAAC,CAAC;AACtE,MAAM,MAAM,OAAO,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC;AAC3C,MAAM,MAAM,cAAc,GAAG,OAAO,cAAc,CAAC;AACnD,MAAM,MAAM,eAAe,GAAG,SAAS,CAAC,iBAAiB,CAAC,CAAC;AAC3D,MAAM,MAAM,UAAU,GAAG,cAAc,GAAG,eAAe,CAAC;AAC1D,MAAM,MAAM,cAAc,GAAG,M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CAAC;AAEL,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,GAC3D,CAAC,GACD,CAAC,SAAS,MAAM,GACf;IAAE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GAC/C,CAAC,CAAC;AAGN,eAAO,MAAM,YAAY,GACxB,cAAc,eAAe,EAC7B,QAAQ,OAAO,EACf,iBAAiB,eAAe,MAQb,GAAG,SAAS,uBAAuB,OAChD,GAAG,iBACO,uBAAuB,CACrC,QAAQ,CAAC,GAAG,CAAC,EACb,QAAQ,CAAC,GAAG,CAAC,EACb,IAAI,CACJ,SAAS,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GAC5B,EAAE,GACF,GAAG,SAAS,uBAAuB,GAClC,CACA,MAAM,EAAE,uBAAuB,CAC7B,QAAQ,CAAC,GAAG,CAAC,GAAG,MAAM,EACtB,QAAQ,CAAC,GAAG,CAAC,EACb,IAAI,CACJ,CACF,GACA,EAAE,KACJ,QAAQ,CAAC,GAAG,CAoFf,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EAAE,KAAK,SAAS,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAEhF,OAAO,EACN,KAAK,mBAAmB,EACxB,KAAK,sBAAsB,GAC3B,MAAM,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import baseFormatters from './formatters.js';
2
- ;
3
- ;
1
+ import baseFormatters from "./src/formatters";
2
+ // TODO add more
3
+ const pluralSufixes = ["_none", "_one", "_other"];
4
4
  // Given a translations object returns a function that can be used to translate keys
5
5
  export const getTranslate = (translations, locale, extraFormatters) => {
6
6
  const formatters = { ...baseFormatters, ...extraFormatters };
@@ -9,54 +9,58 @@ export const getTranslate = (translations, locale, extraFormatters) => {
9
9
  * Supports nested keys, substitutions and plurals
10
10
  */
11
11
  function translate(key, ...arguments_) {
12
- const parts = key.split('.');
12
+ const parts = key.split(".");
13
+ const parameters = arguments_[0];
13
14
  let current = translations;
15
+ let value = key;
16
+ let isPlural = false;
17
+ let lastPart = "";
14
18
  for (const part of parts) {
15
19
  if (current && current[part]) {
20
+ if (typeof current[part] === "string") {
21
+ value = current[part];
22
+ break;
23
+ }
16
24
  current = current[part];
17
25
  }
18
26
  else {
19
- break;
27
+ lastPart = parts.at(-1);
28
+ isPlural = pluralSufixes.some((sufix) => current[lastPart + sufix] !== undefined);
29
+ if (!isPlural) {
30
+ console.error(`Translation key "${key}" not found`);
31
+ return value;
32
+ }
20
33
  }
21
34
  }
22
- // eslint-disable-next-line sonarjs/different-types-comparison
23
- if (current === undefined) {
24
- console.error(`Translation key "${key}" not found`);
25
- return key;
26
- }
27
- if (typeof current === 'object') {
28
- // If its an object being returned check if its a plural key
29
- const isPlural = current[`${parts.at(-1)}_none`] !== undefined ||
30
- current[`${parts.at(-1)}_one`] !== undefined ||
31
- current[`${parts.at(-1)}_other`] !== undefined;
32
- if (isPlural) {
33
- if (!arguments_[0] || !Object.hasOwn(arguments_[0], 'count')) {
34
- console.error(`Missing count value for plural key "${key}"`);
35
- return key;
36
- }
37
- // @ts-expect-error
38
- const count = Number(arguments_[0].count);
39
- if (!count) {
40
- current = current[`${parts.at(-1)}_none`];
41
- }
42
- else if (count === 1) {
43
- current = current[`${parts.at(-1)}_one`];
44
- }
45
- else {
46
- current = current[`${parts.at(-1)}_other`];
47
- }
35
+ // Handle plural keys
36
+ if (isPlural) {
37
+ if (typeof parameters?.count === 'undefined') {
38
+ console.error(`Missing count value for plural key "${key}"`);
39
+ return key;
40
+ }
41
+ const count = Number(parameters.count);
42
+ const none = current[`${lastPart}_none`];
43
+ const one = current[`${lastPart}_one`];
44
+ const other = current[`${lastPart}_other`];
45
+ if (!count && typeof none === "string") {
46
+ value = none;
47
+ }
48
+ else if (count === 1 && typeof one === "string") {
49
+ value = one;
50
+ }
51
+ else if (typeof other === "string") {
52
+ value = other;
48
53
  }
49
54
  else {
50
- console.error(`Incomplete translation key "${key}"`);
55
+ console.warn(`'Missing other translation for: ${key} with count ${count}`);
51
56
  return key;
52
57
  }
53
58
  }
54
- let value = String(current);
55
- const parameters = arguments_[0];
56
59
  if (parameters) {
57
60
  for (const [parameter, value_] of Object.entries(parameters)) {
58
- value = value.replaceAll(new RegExp(`{${parameter}(\\|[a-z|]+)?}`, 'g'), (match, formatters_) => {
59
- const parsedFormatters = (formatters_?.split('|').filter(Boolean) ?? []);
61
+ value = value.replaceAll(new RegExp(`{${parameter}(\\|[a-z|]+)?}`, "g"), (match, formatters_) => {
62
+ const parsedFormatters = (formatters_?.split("|").filter(Boolean) ??
63
+ []);
60
64
  let formattedValue = String(value_);
61
65
  for (const formatter of parsedFormatters) {
62
66
  if (!formatters[formatter]) {
@@ -73,5 +77,5 @@ export const getTranslate = (translations, locale, extraFormatters) => {
73
77
  }
74
78
  return translate;
75
79
  };
76
- export { initReact } from './react-utilities.js';
77
- export { default as defaultFormatters } from './formatters.js';
80
+ export { initReact } from "./src/react";
81
+ export { default as defaultFormatters } from "./src/formatters";
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../../src/formatters.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;AAElE,QAAA,MAAM,UAAU;gCACI,MAAM;gCACN,MAAM;iCACL,MAAM;;8BAET,MAAM;6BACP,MAAM;2BACR,MAAM;CACyB,CAAC;AAE/C,eAAe,UAAU,CAAC"}
@@ -1,15 +1,15 @@
1
1
  import React from 'react';
2
- import { getTranslate, type Locales, type ExtraFormatters, type OtherTranslations } from './index.js';
2
+ import { getTranslate, type ExtraFormatters, type Locales, type TranslationType } from '../index';
3
3
  export interface TranslationContextType {
4
4
  isLoading: boolean;
5
5
  locale: Locales;
6
6
  setLocale: (locale: Locales) => void;
7
7
  t: ReturnType<typeof getTranslate>;
8
8
  }
9
- export declare const initReact: (allTranslations: Record<Locales, OtherTranslations | (() => Promise<OtherTranslations>)>, initialLocale: Locales, extraFormatters: ExtraFormatters) => {
9
+ export declare const initReact: (initialTranslation: TranslationType, initialLocale: Locales, allTranslations: Record<Locales, TranslationType | (() => Promise<TranslationType>)>, extraFormatters: ExtraFormatters) => {
10
10
  TranslationProvider: ({ children }: {
11
11
  children: React.ReactNode;
12
12
  }) => import("react/jsx-runtime").JSX.Element;
13
13
  useTranslation: () => TranslationContextType;
14
14
  };
15
- //# sourceMappingURL=react-utilities.d.ts.map
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,eAAe,EAAkB,KAAK,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,UAAU,CAAC;AAElH,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,oBAAoB,eAAe,EACnC,eAAe,OAAO,EACtB,iBAAiB,MAAM,CAAC,OAAO,EAAE,eAAe,GAAG,CAAC,MAAM,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,EACpF,iBAAiB,eAAe;wCAGW;QAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;KAAE;;CA0DxE,CAAC"}
@@ -0,0 +1,53 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useState } from 'react';
3
+ import { getTranslate } from '../index';
4
+ // Initial translation always should be loaded
5
+ export const initReact = (initialTranslation, initialLocale, allTranslations, extraFormatters) => {
6
+ const TranslationContext = createContext(undefined);
7
+ const TranslationProvider = ({ children }) => {
8
+ const [locale, setLocale] = useState(initialLocale);
9
+ const [translate, setTranslate] = useState(() => getTranslate(initialTranslation, locale, extraFormatters));
10
+ const [isLoading, setIsLoading] = useState(true);
11
+ const loadTranslation = async (targetLocale) => {
12
+ try {
13
+ const translationOrLoader = allTranslations[targetLocale];
14
+ let translationData;
15
+ if (typeof translationOrLoader === 'function') {
16
+ setIsLoading(true);
17
+ translationData = await translationOrLoader();
18
+ }
19
+ else {
20
+ translationData = translationOrLoader;
21
+ }
22
+ setTranslate(getTranslate(translationData, targetLocale, extraFormatters));
23
+ }
24
+ catch (error) {
25
+ console.error(`Failed to load translations for locale ${String(targetLocale)}:`, error);
26
+ }
27
+ finally {
28
+ setIsLoading(false);
29
+ }
30
+ };
31
+ return (_jsx(TranslationContext.Provider, { value: {
32
+ isLoading,
33
+ locale,
34
+ setLocale: async (newLocale) => {
35
+ if (newLocale !== locale) {
36
+ setLocale(newLocale);
37
+ await loadTranslation(newLocale);
38
+ }
39
+ },
40
+ t: translate,
41
+ }, children: children }));
42
+ };
43
+ const useTranslation = () => {
44
+ const context = useContext(TranslationContext);
45
+ if (!context)
46
+ throw new Error('useTranslation must be used within a TranslationProvider');
47
+ return context;
48
+ };
49
+ return {
50
+ TranslationProvider,
51
+ useTranslation
52
+ };
53
+ };
@@ -0,0 +1,13 @@
1
+ import en from './translations/en';
2
+ declare const customFormatters: {
3
+ readonly myCustomFormatter: () => string;
4
+ };
5
+ declare module '../../index' {
6
+ interface Overrides {
7
+ shape: typeof en;
8
+ locales: 'en' | 'es';
9
+ extraFormatters: typeof customFormatters;
10
+ }
11
+ }
12
+ export {};
13
+ //# sourceMappingURL=test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../../src/tests/test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAInC,QAAA,MAAM,gBAAgB;;CAEZ,CAAC;AAEX,OAAO,QAAQ,aAAa,CAAC;IAC5B,UAAU,SAAS;QAClB,KAAK,EAAE,OAAO,EAAE,CAAC;QACjB,OAAO,EAAE,IAAI,GAAG,IAAI,CAAC;QACrB,eAAe,EAAE,OAAO,gBAAgB,CAAA;KACxC;CACD"}
@@ -0,0 +1,19 @@
1
+ import es from './translations/es';
2
+ import { getTranslate } from '../..';
3
+ const customFormatters = {
4
+ myCustomFormatter: () => 'Hello im custom',
5
+ };
6
+ const translate = getTranslate(es, 'en', customFormatters);
7
+ const result = {
8
+ test: translate('test'),
9
+ nested: {
10
+ test: translate('nested.test'),
11
+ deep: {
12
+ again: translate('nested.deep.again'),
13
+ },
14
+ },
15
+ test2_none: translate('test2', { count: 0, value: 'test' }),
16
+ test2_one: translate('test2', { count: 1, value: 'test' }),
17
+ test2_other: translate('test2', { count: 69, value: 'test' }),
18
+ };
19
+ console.log(result);
@@ -0,0 +1,13 @@
1
+ declare const en: {
2
+ readonly test: "Regular translation";
3
+ readonly nested: {
4
+ readonly test: "Nested";
5
+ readonly deep: {
6
+ readonly again: "Nested again";
7
+ };
8
+ };
9
+ readonly test2_one: "Plural one {value}";
10
+ readonly test2_other: "Plural other {value}";
11
+ };
12
+ export default en;
13
+ //# sourceMappingURL=en.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"en.d.ts","sourceRoot":"","sources":["../../../../src/tests/translations/en.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,EAAE;;;;;;;;;;CAUE,CAAC;AAEX,eAAe,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ let test;
2
+ void test;
3
+ const en = {
4
+ test: 'Regular translation',
5
+ nested: {
6
+ test: 'Nested',
7
+ deep: {
8
+ again: 'Nested again',
9
+ },
10
+ },
11
+ test2_one: 'Plural one {value}',
12
+ test2_other: 'Plural other {value}'
13
+ };
14
+ export default en;
@@ -0,0 +1,14 @@
1
+ declare const es: {
2
+ readonly test: "Regular translation";
3
+ readonly nested: {
4
+ readonly test: "Nested";
5
+ readonly deep: {
6
+ readonly again: "Nested again";
7
+ };
8
+ };
9
+ readonly test2_none: "Plural none (not in en)";
10
+ readonly test2_one: undefined;
11
+ readonly test2_other: "Plural other {count|uppercase}";
12
+ };
13
+ export default es;
14
+ //# sourceMappingURL=es.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"es.d.ts","sourceRoot":"","sources":["../../../../src/tests/translations/es.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,EAAE;;;;;;;;;;;CAW4B,CAAC;AAErC,eAAe,EAAE,CAAC"}
@@ -0,0 +1,15 @@
1
+ let test;
2
+ void test;
3
+ const es = {
4
+ test: 'Regular translation',
5
+ nested: {
6
+ test: 'Nested',
7
+ deep: {
8
+ again: 'Nested again',
9
+ },
10
+ },
11
+ test2_none: 'Plural none (not in en)',
12
+ test2_one: undefined, // Key not used
13
+ test2_other: 'Plural other {count|uppercase}'
14
+ };
15
+ export default es;
@@ -1,14 +1,28 @@
1
- import type { FormattersKeys, RemoveReadonlyDeep } from "./index.js";
1
+ import type { FormatterTypes } 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> = ExtractFormatter<T> extends never ? BalancedBraces<T> : ExtractFormatter<T> extends FormattersKeys ? BalancedBraces<T> : ErrorMessage<T, ExtractFormatter<T>>;
7
+ type ValidateFormatter<T extends string> = ExtractFormatter<T> extends never ? BalancedBraces<T> : ExtractFormatter<T> extends FormatterTypes ? BalancedBraces<T> : ErrorMessage<T, ExtractFormatter<T>>;
8
8
  type InternalValidateTranslation<T, KeyPath extends string = ''> = T extends Record<string, any> ? {
9
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
- export type EnsureValidTranslation<T extends never> = T;
10
+ } : T extends string ? ValidateFormatter<T> : never;
11
+ type RemoveNeverDeep<T> = T extends Record<string, any> ? {
12
+ -readonly [K in keyof T as T[K] extends Record<string, any> ? RemoveNeverDeep<T[K]> extends never ? never : K : [T[K]] extends [never] ? never : K]: T[K] extends Record<string, any> ? RemoveNeverDeep<T[K]> : T[K];
13
+ } extends Record<string, never> ? never : {
14
+ -readonly [K in keyof T as T[K] extends Record<string, any> ? RemoveNeverDeep<T[K]> extends never ? never : K : [T[K]] extends [never] ? never : K]: T[K] extends Record<string, any> ? RemoveNeverDeep<T[K]> : T[K];
15
+ } : T;
16
+ /**
17
+ * Utility type to check if translation brackets are used incorrectly or a invalid formatter is used
18
+ *
19
+ * Should be used with EnsureValidTranslation
20
+ * */
21
+ export type ValidateTranslation<T> = RemoveNeverDeep<InternalValidateTranslation<T>>;
22
+ /**
23
+ * Utility type to ensure there is no validation errors
24
+ *
25
+ * */
26
+ export type EnsureValidTranslation<T extends never> = T | undefined;
13
27
  export {};
14
28
  //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts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IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACxD,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,KAAK,GACnC,KAAK,GACL,CAAC,GACD,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GACtB,KAAK,GACL,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACpC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACrB,CAAC,CAAC,CAAC,CAAC;CACN,GACC,CAAC,CAAC;AAEL;;;;KAIK;AACL,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,eAAe,CAAC,2BAA2B,CAAC,CAAC,CAAC,CAAC,CAAC;AAErF;;;KAGK;AACL,MAAM,MAAM,sBAAsB,CAAC,CAAC,SAAS,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC"}
package/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "typed-locales",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
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
- "build": "npm-run-all -s -c build:*",
10
- "build:tsc": "rimraf dist && tsc",
11
- "build:tsc-alias": "tsc-alias -p tsconfig.json",
9
+ "build": "rimraf dist && tsc",
12
10
  "dev": "tsx watch src/test.tsx",
13
11
  "deploy": "npm run build && npm publish --access public"
14
12
  },
@@ -18,14 +16,12 @@
18
16
  "packageManager": "pnpm@10.6.2",
19
17
  "dependencies": {
20
18
  "react": "^19.1.0",
21
- "react-dom": "^19.1.0"
22
- },
23
- "devDependencies": {
24
- "npm-run-all": "^4.1.5",
25
- "@types/react": "^19.1.5",
19
+ "react-dom": "^19.1.0",
26
20
  "rimraf": "^6.0.1",
27
- "tsc-alias": "^1.8.16",
28
21
  "tsx": "^4.19.4",
29
22
  "typescript": "^5.8.3"
23
+ },
24
+ "devDependencies": {
25
+ "@types/react": "^19.1.5"
30
26
  }
31
27
  }
@@ -1,3 +0,0 @@
1
- {
2
- "typescript.tsdk": "node_modules\\typescript\\lib"
3
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;AAElE,QAAA,MAAM,UAAU;gCACI,MAAM;gCACN,MAAM;iCACL,MAAM;;8BAET,MAAM;6BACP,MAAM;2BACR,MAAM;CACyB,CAAC;AAE/C,eAAe,UAAU,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-utilities.d.ts","sourceRoot":"","sources":["../src/react-utilities.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4E,MAAM,OAAO,CAAC;AAEjG,OAAO,EAAE,YAAY,EAAE,KAAK,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAAqB,MAAM,SAAS,CAAC;AAEtH,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;;CA+DxE,CAAC"}
@@ -1,61 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { createContext, useCallback, useContext, useState } from 'react';
3
- import { getTranslate } from './index.js';
4
- // Initial translation always should be loaded
5
- export const initReact = (allTranslations, initialLocale, extraFormatters) => {
6
- const TranslationContext = createContext(undefined);
7
- const TranslationProvider = ({ children }) => {
8
- const [state, setState] = useState(() => ({
9
- locale: initialLocale,
10
- t: getTranslate(allTranslations[initialLocale], initialLocale, extraFormatters),
11
- isLoading: false,
12
- }));
13
- const setLocaleInternal = useCallback(async (targetLocale) => {
14
- if (state.locale === targetLocale)
15
- return;
16
- try {
17
- const translationOrLoader = allTranslations[targetLocale];
18
- if (typeof translationOrLoader === 'function') {
19
- setState(prev => ({
20
- ...prev,
21
- isLoading: true,
22
- }));
23
- const translationData = await translationOrLoader();
24
- setState({
25
- locale: targetLocale,
26
- t: getTranslate(translationData, targetLocale, extraFormatters),
27
- isLoading: false,
28
- });
29
- }
30
- else {
31
- setState({
32
- locale: targetLocale,
33
- t: getTranslate(translationOrLoader, targetLocale, extraFormatters),
34
- isLoading: false,
35
- });
36
- }
37
- }
38
- catch (error) {
39
- console.error(`Failed to load translations for locale ${String(targetLocale)}:`, error);
40
- setState(prev => ({
41
- ...prev,
42
- isLoading: false,
43
- }));
44
- }
45
- }, [allTranslations, extraFormatters, state.locale]);
46
- return (_jsx(TranslationContext.Provider, { value: {
47
- ...state,
48
- setLocale: setLocaleInternal,
49
- }, children: children }));
50
- };
51
- const useTranslation = () => {
52
- const context = useContext(TranslationContext);
53
- if (!context)
54
- throw new Error('useTranslation must be used within a TranslationProvider');
55
- return context;
56
- };
57
- return {
58
- TranslationProvider,
59
- useTranslation
60
- };
61
- };
@@ -1 +0,0 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElE,KAAK,YAAY,CAAC,KAAK,SAAS,MAAM,EAAE,CAAC,SAAS,MAA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