typed-locales 1.0.56 → 1.0.58

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.
@@ -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, type ExtraFormatters, type Locales } from '../index.js';
3
3
  export interface TranslationContextType {
4
4
  isLoading: boolean;
5
5
  locale?: Locales;
@@ -12,9 +12,9 @@ type ExtraTranslation = {
12
12
  key: string;
13
13
  };
14
14
  type ExtraTranslations = ExtraTranslation[];
15
- export declare const initReact: (defaultTranslations: TranslationType, allTranslations: Record<Locales, () => Promise<{
16
- default: TranslationType;
17
- }>>, extraFormatters: ExtraFormatters, defaultLocale: Locales, fallbackLocale?: Locales, extraTranslations?: ExtraTranslations) => ({ children }: {
15
+ export declare const initReact: (defaultTranslations: object, allTranslations: Record<Locales, () => Promise<{
16
+ default: object;
17
+ }>>, extraFormatters: ExtraFormatters, defaultLocale: Locales, extraTranslations?: ExtraTranslations) => ({ children }: {
18
18
  children: React.ReactNode;
19
19
  }) => import("react/jsx-runtime").JSX.Element;
20
20
  export declare const useTranslation: () => TranslationContextType;
@@ -1 +1 @@
1
- {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/adapters/react.tsx"],"names":[],"mappings":"AAAA,OAAO,KAKN,MAAM,OAAO,CAAC;AAEf,OAAO,EACN,YAAY,EACZ,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,eAAe,EACpB,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;AAMD,KAAK,gBAAgB,GAA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AE,CAAC,CAClC,EACD,iBAAiB,eAAe,EAChC,eAAe,OAAO,EACtB,oBAAoB,iBAAiB,oBAEM;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,4CA6DxE,CAAC;AAEF,eAAO,MAAM,cAAc,8BAM1B,CAAC"}
@@ -21,40 +21,22 @@ const addExtraTranslations = (translation, extraTranslations, locale) => {
21
21
  return translation;
22
22
  };
23
23
  // Initial translation always should be loaded
24
- export const initReact = (defaultTranslations, allTranslations, extraFormatters, defaultLocale, fallbackLocale, extraTranslations) => {
24
+ export const initReact = (defaultTranslations, allTranslations, extraFormatters, defaultLocale, extraTranslations) => {
25
25
  const TranslationProvider = ({ children }) => {
26
+ const defaultTranslate = getTranslate(addExtraTranslations(defaultTranslations, extraTranslations ?? [], defaultLocale), defaultLocale, extraFormatters);
26
27
  const [state, setState] = useState({
27
28
  isLoading: false,
28
29
  locale: defaultLocale,
29
- translate: getTranslate(addExtraTranslations(defaultTranslations, extraTranslations ?? [], defaultLocale), defaultLocale, extraFormatters),
30
+ translate: defaultTranslate,
30
31
  });
31
32
  const setLocale = useCallback(async (targetLocale) => {
32
33
  try {
33
34
  const translationOrLoader = allTranslations[targetLocale];
34
- let translationData;
35
- if (typeof translationOrLoader === 'function') {
36
- setState(previous => ({ ...previous, isLoading: true }));
37
- translationData = await translationOrLoader().then(t => t.default);
38
- }
39
- else {
40
- translationData = translationOrLoader;
41
- }
42
- let fallbackTranslationData;
43
- if (fallbackLocale) {
44
- const fallbackTranslationOrLoader = allTranslations[targetLocale];
45
- if (typeof fallbackTranslationOrLoader === 'function') {
46
- fallbackTranslationData = await fallbackTranslationOrLoader().then(t => t.default);
47
- }
48
- else {
49
- fallbackTranslationData = fallbackTranslationOrLoader;
50
- }
51
- }
35
+ const translationData = await translationOrLoader().then(t => t.default);
52
36
  setState({
53
37
  isLoading: false,
54
38
  locale: targetLocale,
55
- translate: getTranslate(addExtraTranslations(translationData, extraTranslations ?? [], targetLocale), targetLocale, extraFormatters, fallbackLocale === targetLocale || !fallbackLocale
56
- ? undefined
57
- : getTranslate(fallbackTranslationData, fallbackLocale, extraFormatters)),
39
+ translate: getTranslate(addExtraTranslations(translationData, extraTranslations ?? [], targetLocale), targetLocale, extraFormatters, defaultTranslate),
58
40
  });
59
41
  }
60
42
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typed-locales",
3
- "version": "1.0.56",
3
+ "version": "1.0.58",
4
4
  "description": "Type safe utilities for translating strings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",