typed-locales 1.0.55 → 1.0.57

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.
@@ -12,9 +12,9 @@ type ExtraTranslation = {
12
12
  key: string;
13
13
  };
14
14
  type ExtraTranslations = ExtraTranslation[];
15
- export declare const initReact: (allTranslations: Record<Locales, (() => Promise<{
15
+ export declare const initReact: (defaultTranslations: TranslationType, allTranslations: Record<Locales, () => Promise<{
16
16
  default: TranslationType;
17
- }>) | TranslationType>, extraFormatters: ExtraFormatters, defaultLocale: Locales, fallbackLocale?: Locales, extraTranslations?: ExtraTranslations) => ({ children }: {
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,GAAG;KACtB,MAAM,IAAI,OAAO,CAAC,CAAC,EAAE,MAAM;CAC5B,GAAG;IACH,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,KAAK,iBAAiB,GAAG,gBAAgB,EAAE,CAAC;AA2B5C,eAAO,MAAM,SAAS,GACrB,iBAAiB,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AM1B,CAAC"}
@@ -21,16 +21,13 @@ const addExtraTranslations = (translation, extraTranslations, locale) => {
21
21
  return translation;
22
22
  };
23
23
  // Initial translation always should be loaded
24
- export const initReact = (allTranslations, extraFormatters, defaultLocale, fallbackLocale, extraTranslations) => {
25
- const defaultTranslations = allTranslations[defaultLocale];
26
- if (typeof defaultTranslations === 'function') {
27
- throw new Error('Default locale in all translations object cant be a promise');
28
- }
24
+ export const initReact = (defaultTranslations, allTranslations, extraFormatters, defaultLocale, extraTranslations) => {
29
25
  const TranslationProvider = ({ children }) => {
26
+ const defaultTranslate = getTranslate(addExtraTranslations(defaultTranslations, extraTranslations ?? [], defaultLocale), defaultLocale, extraFormatters);
30
27
  const [state, setState] = useState({
31
28
  isLoading: false,
32
29
  locale: defaultLocale,
33
- translate: getTranslate(addExtraTranslations(defaultTranslations, extraTranslations ?? [], defaultLocale), defaultLocale, extraFormatters),
30
+ translate: defaultTranslate,
34
31
  });
35
32
  const setLocale = useCallback(async (targetLocale) => {
36
33
  try {
@@ -43,22 +40,10 @@ export const initReact = (allTranslations, extraFormatters, defaultLocale, fallb
43
40
  else {
44
41
  translationData = translationOrLoader;
45
42
  }
46
- let fallbackTranslationData;
47
- if (fallbackLocale) {
48
- const fallbackTranslationOrLoader = allTranslations[targetLocale];
49
- if (typeof fallbackTranslationOrLoader === 'function') {
50
- fallbackTranslationData = await fallbackTranslationOrLoader().then(t => t.default);
51
- }
52
- else {
53
- fallbackTranslationData = fallbackTranslationOrLoader;
54
- }
55
- }
56
43
  setState({
57
44
  isLoading: false,
58
45
  locale: targetLocale,
59
- translate: getTranslate(addExtraTranslations(translationData, extraTranslations ?? [], targetLocale), targetLocale, extraFormatters, fallbackLocale === targetLocale || !fallbackLocale
60
- ? undefined
61
- : getTranslate(fallbackTranslationData, fallbackLocale, extraFormatters)),
46
+ translate: getTranslate(addExtraTranslations(translationData, extraTranslations ?? [], targetLocale), targetLocale, extraFormatters, defaultTranslate),
62
47
  });
63
48
  }
64
49
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typed-locales",
3
- "version": "1.0.55",
3
+ "version": "1.0.57",
4
4
  "description": "Type safe utilities for translating strings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",