@arc-js/intl 0.0.6 → 0.0.8

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,42 +1,92 @@
1
- // arcIntl/providers/ArcIntlProvider.tsx
2
- import { createContext, useContext, useState, useEffect, useMemo } from 'react';
1
+ import { createContext, useContext, useState, useEffect, useMemo, useCallback } from 'react';
3
2
  import { TranslationService } from '../core/TranslationService';
4
- import { getSavedLocale, saveLocale, Locale, SUPPORTED_LOCALES } from '../config';
3
+ import { getSavedLocale, saveLocale, SUPPORTED_LOCALES, type TranslationsConfig } from '../config';
4
+ import { setTranslationsConfig } from '../utils/loaders';
5
+ import type { Locale } from '../config';
5
6
 
6
7
  const ArcIntlContext = createContext<ReturnType<typeof useArcIntlValue> | null>(null);
7
8
 
8
9
  const useArcIntlValue = (
10
+ translationsConfig: TranslationsConfig,
9
11
  supportedLocales: string[] = SUPPORTED_LOCALES
10
12
  ) => {
11
13
  const [service] = useState(() => new TranslationService(supportedLocales));
12
- const [currentLocale, setCurrentLocale] = useState<Locale>(getSavedLocale());
14
+ const [currentLocale, setCurrentLocale] = useState<Locale>(getSavedLocale(supportedLocales));
13
15
  const [isLoading, setIsLoading] = useState(true);
16
+ const [loadedModules, setLoadedModules] = useState<Set<string>>(new Set());
17
+
18
+ useEffect(() => {
19
+ setTranslationsConfig(translationsConfig);
20
+ }, [translationsConfig]);
21
+
22
+ useEffect(() => {
23
+ const loadAllModules = async () => {
24
+ if (translationsConfig.modules) {
25
+ const moduleNames = Object.keys(translationsConfig.modules);
26
+
27
+ for (const moduleName of moduleNames) {
28
+ try {
29
+ await service.loadModule(moduleName);
30
+ setLoadedModules(prev => new Set(prev).add(moduleName));
31
+ } catch (error) {
32
+ console.error(`Failed to load module ${moduleName}:`, error);
33
+ }
34
+ }
35
+ }
36
+ };
37
+
38
+ loadAllModules();
39
+ }, [translationsConfig.modules, service]);
14
40
 
15
41
  useEffect(() => {
16
42
  const initialize = async () => {
17
- await service.initialize(currentLocale);
18
- setIsLoading(false);
43
+ setIsLoading(true);
44
+ try {
45
+ await service.initialize(currentLocale);
46
+ } catch (error) {
47
+ console.error('Failed to initialize translations:', error);
48
+ } finally {
49
+ setIsLoading(false);
50
+ }
19
51
  };
20
52
  initialize();
21
- }, []);
53
+ }, [currentLocale, service]);
22
54
 
23
- const changeLocale = async (locale: Locale) => {
55
+ const changeLocale = useCallback(async (locale: Locale) => {
56
+ if (!supportedLocales.includes(locale)) {
57
+ console.warn(`Locale ${locale} is not supported`);
58
+ return;
59
+ }
60
+
61
+ if (locale === currentLocale) return;
62
+
24
63
  setIsLoading(true);
25
- await service.loadLocale(locale);
26
- service.setLocale(locale);
27
- setCurrentLocale(locale);
28
- saveLocale(locale);
29
- setIsLoading(false);
30
- };
64
+ try {
65
+ setCurrentLocale(locale);
66
+ saveLocale(locale);
67
+ } catch (error) {
68
+ console.error('Failed to change locale:', error);
69
+ setIsLoading(false);
70
+ }
71
+ }, [currentLocale, supportedLocales]);
31
72
 
32
- const loadModuleTranslations = async (moduleName: string) => {
73
+ const loadModuleTranslations = useCallback(async (moduleName: string) => {
33
74
  setIsLoading(true);
34
- await service.loadModule(moduleName);
35
- setIsLoading(false);
36
- };
75
+ try {
76
+ await service.loadModule(moduleName);
77
+ } catch (error) {
78
+ console.error(`Failed to load module ${moduleName}:`, error);
79
+ } finally {
80
+ setIsLoading(false);
81
+ }
82
+ }, [service]);
83
+
84
+ const t = useCallback((key: string, params?: Record<string, any>, options?: any) => {
85
+ return service.t(key, params || {}, options || {});
86
+ }, [service]);
37
87
 
38
88
  return {
39
- t: service.t.bind(service),
89
+ t,
40
90
  changeLocale,
41
91
  currentLocale,
42
92
  isLoading,
@@ -46,16 +96,22 @@ const useArcIntlValue = (
46
96
 
47
97
  export const ArcIntlProvider: React.FC<{
48
98
  children: React.ReactNode
99
+ translations: TranslationsConfig
49
100
  supportedLocales?: string[];
50
101
  }> = ({
102
+ translations,
51
103
  supportedLocales,
52
104
  children,
53
105
  }) => {
54
- const value = useArcIntlValue((
55
- typeof supportedLocales === 'object' &&
56
- !!Array.isArray(supportedLocales) &&
57
- supportedLocales.length > 0
58
- ) ? supportedLocales : SUPPORTED_LOCALES);
106
+ const value = useArcIntlValue(
107
+ translations,
108
+ (typeof supportedLocales === 'object' &&
109
+ !!Array.isArray(supportedLocales) &&
110
+ supportedLocales.length > 0)
111
+ ? supportedLocales
112
+ : SUPPORTED_LOCALES
113
+ );
114
+
59
115
  return <ArcIntlContext.Provider value={value}>{children}</ArcIntlContext.Provider>;
60
116
  };
61
117
 
@@ -63,4 +119,10 @@ export const useArcIntl = () => {
63
119
  const context = useContext(ArcIntlContext);
64
120
  if (!context) throw new Error('useArcIntl must be used within an ArcIntlProvider');
65
121
  return context;
66
- };
122
+ };
123
+
124
+ export default {
125
+ useArcIntlValue,
126
+ ArcIntlProvider,
127
+ useArcIntl,
128
+ }
@@ -1,3 +1,17 @@
1
+ interface TranslationMap {
2
+ [locale: string]: () => Promise<Record<string, any>>;
3
+ }
4
+ interface ModuleTranslations {
5
+ [moduleName: string]: TranslationMap;
6
+ }
7
+ interface TranslationsConfig {
8
+ base: {
9
+ [locale: string]: () => Promise<Record<string, any>>;
10
+ };
11
+ modules?: ModuleTranslations;
12
+ }
13
+
14
+ declare const setTranslationsConfig: (config: TranslationsConfig) => void;
1
15
  declare const loadBaseTranslations: (locale: string) => Promise<Record<string, any>>;
2
16
  declare const loadModulesTranslations: (locale: string) => Promise<Array<{
3
17
  moduleName: string;
@@ -8,4 +22,4 @@ declare const loadModuleTranslations: (moduleName: string, locale: string) => Pr
8
22
  translations: Record<string, any>;
9
23
  } | undefined>;
10
24
 
11
- export { loadBaseTranslations, loadModuleTranslations, loadModulesTranslations };
25
+ export { loadBaseTranslations, loadModuleTranslations, loadModulesTranslations, setTranslationsConfig };