typed-locales 1.0.35 → 1.0.37

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
@@ -11,4 +11,6 @@
11
11
  - optional: "{param?number}"
12
12
  - required: "{param:number}"
13
13
  Make formatters input type safe in the string (catch {test:date|uppercase})
14
- Add a incrementalTranslaiton type that is like translation type but all values can be undefined
14
+ Add a incrementalTranslaiton type that is like translation type but all values can be undefined
15
+ Allow passing custom types to validate in string
16
+ Allow accessing object properties from values
package/dist/react.d.ts CHANGED
@@ -3,10 +3,10 @@ import { getTranslate, type ExtraFormatters, type Locales, type TranslationType
3
3
  export interface TranslationContextType {
4
4
  isLoading: boolean;
5
5
  locale: Locales;
6
- setLocale: (locale: Locales) => void;
6
+ setLocale: (locale: Locales) => Promise<Locales>;
7
7
  t: ReturnType<typeof getTranslate>;
8
8
  }
9
- export declare const initReact: (initialTranslation: TranslationType, initialLocale: Locales, allTranslations: Record<Locales, TranslationType | (() => Promise<TranslationType>)>, extraFormatters: ExtraFormatters) => {
9
+ export declare const initReact: (initialTranslation: TranslationType, initialLocale: Locales, allTranslations: Record<Locales, (() => Promise<TranslationType>) | TranslationType>, extraFormatters: ExtraFormatters) => {
10
10
  TranslationProvider: ({ children }: {
11
11
  children: React.ReactNode;
12
12
  }) => import("react/jsx-runtime").JSX.Element;
@@ -1 +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,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,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;wCAKW;QAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;KAAE;;CAgExE,CAAC"}
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,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,eAAe,CAAC,CAAC,GAAG,eAAe,CAAC,EACpF,iBAAiB,eAAe;wCAKW;QAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;KAAE;;CAsExE,CAAC"}
package/dist/react.js CHANGED
@@ -7,30 +7,30 @@ export const initReact = (initialTranslation, initialLocale, allTranslations, ex
7
7
  const initialTranslate = getTranslate(initialTranslation, initialLocale, extraFormatters);
8
8
  const TranslationProvider = ({ children }) => {
9
9
  const [state, setState] = useState({
10
+ isLoading: false,
10
11
  locale: initialLocale,
11
12
  translate: initialTranslate,
12
- isLoading: false,
13
13
  });
14
14
  const loadTranslation = async (targetLocale) => {
15
15
  try {
16
16
  const translationOrLoader = allTranslations[targetLocale];
17
17
  let translationData;
18
18
  if (typeof translationOrLoader === 'function') {
19
- setState(prev => ({ ...prev, isLoading: true }));
19
+ setState(previous => ({ ...previous, isLoading: true }));
20
20
  translationData = await translationOrLoader();
21
21
  }
22
22
  else {
23
23
  translationData = translationOrLoader;
24
24
  }
25
25
  setState({
26
- translate: getTranslate(translationData, targetLocale, extraFormatters, initialTranslate),
27
- locale: targetLocale,
28
26
  isLoading: false,
27
+ locale: targetLocale,
28
+ translate: getTranslate(translationData, targetLocale, extraFormatters, initialLocale === targetLocale ? undefined : initialTranslate),
29
29
  });
30
30
  }
31
31
  catch (error) {
32
32
  console.error(`Failed to load translations for locale ${String(targetLocale)}:`, error);
33
- setState(prev => ({ ...prev, isLoading: false }));
33
+ setState(previous => ({ ...previous, isLoading: false }));
34
34
  }
35
35
  };
36
36
  return (_jsx(TranslationContext.Provider, { value: {
@@ -40,6 +40,7 @@ export const initReact = (initialTranslation, initialLocale, allTranslations, ex
40
40
  if (newLocale !== state.locale) {
41
41
  await loadTranslation(newLocale);
42
42
  }
43
+ return newLocale;
43
44
  },
44
45
  t: state.translate,
45
46
  }, children: children }));
@@ -52,6 +53,6 @@ export const initReact = (initialTranslation, initialLocale, allTranslations, ex
52
53
  };
53
54
  return {
54
55
  TranslationProvider,
55
- useTranslation
56
+ useTranslation,
56
57
  };
57
58
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typed-locales",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
4
4
  "description": "Type safe utilities for translating strings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",