@arc-js/intl 0.0.9 → 0.0.92

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.
@@ -1020,22 +1020,30 @@ class TranslationService {
1020
1020
  }
1021
1021
  loadModule(moduleName) {
1022
1022
  return __awaiter(this, void 0, void 0, function* () {
1023
+ console.log(`Loading module ${moduleName}...`);
1024
+ console.log(`Already loaded modules:`, Array.from(this.loadedModules));
1023
1025
  if (this.loadedModules.has(moduleName)) {
1026
+ console.log(`Module ${moduleName} already loaded`);
1024
1027
  return;
1025
1028
  }
1026
1029
  for (const locale of this.supportedLocales) {
1030
+ console.log(`Loading module ${moduleName} for locale ${locale}...`);
1027
1031
  const moduleData = yield loadModuleTranslations(moduleName, locale);
1028
1032
  if (moduleData === null || moduleData === void 0 ? void 0 : moduleData.translations) {
1033
+ console.log(`Found translations for ${moduleName} in ${locale}`);
1029
1034
  if (!this.resources[locale]) {
1035
+ console.log(`Creating resources for locale ${locale}`);
1030
1036
  this.resources[locale] = { core: {} };
1031
1037
  }
1032
1038
  this.resources[locale][moduleName] = moduleData.translations;
1039
+ console.log(`Resources for ${locale}:`, Object.keys(this.resources[locale]));
1033
1040
  }
1034
1041
  else {
1035
1042
  console.warn(`No translations found for module ${moduleName} in locale ${locale}`);
1036
1043
  }
1037
1044
  }
1038
1045
  this.loadedModules.add(moduleName);
1046
+ console.log(`Module ${moduleName} loaded successfully`);
1039
1047
  });
1040
1048
  }
1041
1049
  t(key, params = {}, options = {}) {
@@ -1,3 +1,4 @@
1
+ import React from "react";
1
2
  import { useEffect, useState, useCallback } from 'react';
2
3
  import { useArcIntl } from '../providers/IntlProvider';
3
4
  export function useTranslation(moduleName) {
@@ -5,7 +6,7 @@ export function useTranslation(moduleName) {
5
6
  const [isModuleLoading, setIsModuleLoading] = useState(false);
6
7
  const [moduleLoaded, setModuleLoaded] = useState(!moduleName);
7
8
  const t = useCallback((key, params, options) => {
8
- if (moduleName && !moduleLoaded && !options?.moduleName) {
9
+ if (moduleName && !moduleLoaded) {
9
10
  console.warn(`Module ${moduleName} not loaded yet, using default value`);
10
11
  return options?.defaultValue || key;
11
12
  }
@@ -16,7 +17,7 @@ export function useTranslation(moduleName) {
16
17
  return arcIntl.t(key, params || {}, finalOptions);
17
18
  }, [arcIntl, moduleName, moduleLoaded]);
18
19
  useEffect(() => {
19
- if (moduleName && !moduleLoaded) {
20
+ if (moduleName && !moduleLoaded && !arcIntl.isLoading) {
20
21
  setIsModuleLoading(true);
21
22
  arcIntl.loadModuleTranslations(moduleName).then(() => {
22
23
  setModuleLoaded(true);
@@ -26,7 +27,7 @@ export function useTranslation(moduleName) {
26
27
  setIsModuleLoading(false);
27
28
  });
28
29
  }
29
- }, [moduleName, moduleLoaded, arcIntl]);
30
+ }, [moduleName, moduleLoaded, arcIntl, arcIntl.isLoading]);
30
31
  return {
31
32
  t,
32
33
  changeLocale: arcIntl.changeLocale,
@@ -1,14 +1,13 @@
1
1
  import { useEffect, useState, useCallback } from 'react';
2
2
  import { useArcIntl } from '../providers/IntlProvider';
3
3
 
4
-
5
4
  export function useTranslation(moduleName?: string) {
6
5
  const arcIntl = useArcIntl();
7
6
  const [isModuleLoading, setIsModuleLoading] = useState(false);
8
7
  const [moduleLoaded, setModuleLoaded] = useState(!moduleName);
9
8
 
10
9
  const t = useCallback((key: string, params?: Record<string, any>, options?: any) => {
11
- if (moduleName && !moduleLoaded && !options?.moduleName) {
10
+ if (moduleName && !moduleLoaded) {
12
11
  console.warn(`Module ${moduleName} not loaded yet, using default value`);
13
12
  return options?.defaultValue || key;
14
13
  }
@@ -22,7 +21,7 @@ export function useTranslation(moduleName?: string) {
22
21
  }, [arcIntl, moduleName, moduleLoaded]);
23
22
 
24
23
  useEffect(() => {
25
- if (moduleName && !moduleLoaded) {
24
+ if (moduleName && !moduleLoaded && !arcIntl.isLoading) {
26
25
  setIsModuleLoading(true);
27
26
  arcIntl.loadModuleTranslations(moduleName)
28
27
  .then(() => {
@@ -35,7 +34,7 @@ export function useTranslation(moduleName?: string) {
35
34
  setIsModuleLoading(false);
36
35
  });
37
36
  }
38
- }, [moduleName, moduleLoaded, arcIntl]);
37
+ }, [moduleName, moduleLoaded, arcIntl, arcIntl.isLoading]);
39
38
 
40
39
  return {
41
40
  t,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.0.9",
6
+ "version": "0.0.92",
7
7
  "description": "INTL est un système de gestion d'internationalisation (i18n) modulaire et performant pour les applications React avec TypeScript/JavaScript. Il fournit une gestion avancée des traductions, un chargement dynamique des modules, et une intégration transparente avec l'écosystème Arc.",
8
8
  "main": "index.js",
9
9
  "keywords": [],
@@ -1,3 +1,4 @@
1
+ import React from "react";
1
2
  import { createContext, useContext, useState, useEffect, useCallback } from 'react';
2
3
  import { TranslationService } from '../core/TranslationService';
3
4
  import { getSavedLocale, saveLocale, SUPPORTED_LOCALES } from '../config';
@@ -8,30 +9,17 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
8
9
  const [currentLocale, setCurrentLocale] = useState(getSavedLocale(supportedLocales));
9
10
  const [isLoading, setIsLoading] = useState(true);
10
11
  const [loadedModules, setLoadedModules] = useState(new Set());
12
+ const [initialized, setInitialized] = useState(false);
11
13
  useEffect(() => {
12
14
  setTranslationsConfig(translationsConfig);
13
- }, [translationsConfig]);
14
- useEffect(() => {
15
- const loadAllModules = async () => {
16
- if (translationsConfig.modules) {
17
- const moduleNames = Object.keys(translationsConfig.modules);
18
- for (const moduleName of moduleNames) {
19
- try {
20
- await service.loadModule(moduleName);
21
- setLoadedModules(prev => new Set(prev).add(moduleName));
22
- } catch (error) {
23
- console.error(`Failed to load module ${moduleName}:`, error);
24
- }
25
- }
26
- }
27
- };
28
- loadAllModules();
29
- }, [translationsConfig.modules, service]);
15
+ }, [translationsConfig])
16
+
30
17
  useEffect(() => {
31
18
  const initialize = async () => {
32
19
  setIsLoading(true);
33
20
  try {
34
21
  await service.initialize(currentLocale);
22
+ setInitialized(true);
35
23
  } catch (error) {
36
24
  console.error('Failed to initialize translations:', error);
37
25
  } finally {
@@ -39,7 +27,23 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
39
27
  }
40
28
  };
41
29
  initialize();
42
- }, [currentLocale, service]);
30
+ }, [currentLocale, service])
31
+
32
+ useEffect(() => {
33
+ if (!initialized || !translationsConfig.modules) return;
34
+ const loadAllModules = async () => {
35
+ const moduleNames = Object.keys(translationsConfig.modules || {});
36
+ for (const moduleName of moduleNames) {
37
+ try {
38
+ await service.loadModule(moduleName);
39
+ setLoadedModules(prev => new Set(prev).add(moduleName));
40
+ } catch (error) {
41
+ console.error(`Failed to load module ${moduleName}:`, error);
42
+ }
43
+ }
44
+ };
45
+ loadAllModules();
46
+ }, [initialized, translationsConfig.modules, service]);
43
47
  const changeLocale = useCallback(async locale => {
44
48
  if (!supportedLocales.includes(locale)) {
45
49
  console.warn(`Locale ${locale} is not supported`);
@@ -48,23 +52,28 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
48
52
  if (locale === currentLocale) return;
49
53
  setIsLoading(true);
50
54
  try {
55
+ await service.initialize(locale);
51
56
  setCurrentLocale(locale);
52
57
  saveLocale(locale);
53
58
  } catch (error) {
54
59
  console.error('Failed to change locale:', error);
60
+ } finally {
55
61
  setIsLoading(false);
56
62
  }
57
- }, [currentLocale, supportedLocales]);
63
+ }, [currentLocale, supportedLocales, service]);
58
64
  const loadModuleTranslations = useCallback(async moduleName => {
65
+ if (loadedModules.has(moduleName)) return;
59
66
  setIsLoading(true);
60
67
  try {
61
68
  await service.loadModule(moduleName);
69
+ setLoadedModules(prev => new Set(prev).add(moduleName));
62
70
  } catch (error) {
63
71
  console.error(`Failed to load module ${moduleName}:`, error);
72
+ throw error;
64
73
  } finally {
65
74
  setIsLoading(false);
66
75
  }
67
- }, [service]);
76
+ }, [service, loadedModules]);
68
77
  const t = useCallback((key, params, options) => {
69
78
  return service.t(key, params || {}, options || {});
70
79
  }, [service]);
@@ -73,7 +82,8 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
73
82
  changeLocale,
74
83
  currentLocale,
75
84
  isLoading,
76
- loadModuleTranslations
85
+ loadModuleTranslations,
86
+ loadedModules: Array.from(loadedModules)
77
87
  };
78
88
  };
79
89
  export const ArcIntlProvider = ({
@@ -90,9 +100,4 @@ export const useArcIntl = () => {
90
100
  const context = useContext(ArcIntlContext);
91
101
  if (!context) throw new Error('useArcIntl must be used within an ArcIntlProvider');
92
102
  return context;
93
- };
94
- export default {
95
- useArcIntlValue,
96
- ArcIntlProvider,
97
- useArcIntl
98
103
  };
@@ -14,35 +14,19 @@ const useArcIntlValue = (
14
14
  const [currentLocale, setCurrentLocale] = useState<Locale>(getSavedLocale(supportedLocales));
15
15
  const [isLoading, setIsLoading] = useState(true);
16
16
  const [loadedModules, setLoadedModules] = useState<Set<string>>(new Set());
17
+ const [initialized, setInitialized] = useState(false);
17
18
 
18
19
  useEffect(() => {
19
20
  setTranslationsConfig(translationsConfig);
20
21
  }, [translationsConfig]);
21
22
 
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]);
40
-
23
+
41
24
  useEffect(() => {
42
25
  const initialize = async () => {
43
26
  setIsLoading(true);
44
27
  try {
45
28
  await service.initialize(currentLocale);
29
+ setInitialized(true);
46
30
  } catch (error) {
47
31
  console.error('Failed to initialize translations:', error);
48
32
  } finally {
@@ -52,6 +36,26 @@ const useArcIntlValue = (
52
36
  initialize();
53
37
  }, [currentLocale, service]);
54
38
 
39
+
40
+ useEffect(() => {
41
+ if (!initialized || !translationsConfig.modules) return;
42
+
43
+ const loadAllModules = async () => {
44
+ const moduleNames = Object.keys(translationsConfig.modules || {});
45
+
46
+ for (const moduleName of moduleNames) {
47
+ try {
48
+ await service.loadModule(moduleName);
49
+ setLoadedModules(prev => new Set(prev).add(moduleName));
50
+ } catch (error) {
51
+ console.error(`Failed to load module ${moduleName}:`, error);
52
+ }
53
+ }
54
+ };
55
+
56
+ loadAllModules();
57
+ }, [initialized, translationsConfig.modules, service]);
58
+
55
59
  const changeLocale = useCallback(async (locale: Locale) => {
56
60
  if (!supportedLocales.includes(locale)) {
57
61
  console.warn(`Locale ${locale} is not supported`);
@@ -62,24 +66,30 @@ const useArcIntlValue = (
62
66
 
63
67
  setIsLoading(true);
64
68
  try {
69
+ await service.initialize(locale);
65
70
  setCurrentLocale(locale);
66
71
  saveLocale(locale);
67
72
  } catch (error) {
68
73
  console.error('Failed to change locale:', error);
74
+ } finally {
69
75
  setIsLoading(false);
70
76
  }
71
- }, [currentLocale, supportedLocales]);
77
+ }, [currentLocale, supportedLocales, service]);
72
78
 
73
79
  const loadModuleTranslations = useCallback(async (moduleName: string) => {
80
+ if (loadedModules.has(moduleName)) return;
81
+
74
82
  setIsLoading(true);
75
83
  try {
76
84
  await service.loadModule(moduleName);
85
+ setLoadedModules(prev => new Set(prev).add(moduleName));
77
86
  } catch (error) {
78
87
  console.error(`Failed to load module ${moduleName}:`, error);
88
+ throw error;
79
89
  } finally {
80
90
  setIsLoading(false);
81
91
  }
82
- }, [service]);
92
+ }, [service, loadedModules]);
83
93
 
84
94
  const t = useCallback((key: string, params?: Record<string, any>, options?: any) => {
85
95
  return service.t(key, params || {}, options || {});
@@ -91,6 +101,7 @@ const useArcIntlValue = (
91
101
  currentLocale,
92
102
  isLoading,
93
103
  loadModuleTranslations,
104
+ loadedModules: Array.from(loadedModules),
94
105
  };
95
106
  };
96
107
 
@@ -119,10 +130,4 @@ export const useArcIntl = () => {
119
130
  const context = useContext(ArcIntlContext);
120
131
  if (!context) throw new Error('useArcIntl must be used within an ArcIntlProvider');
121
132
  return context;
122
- };
123
-
124
- export default {
125
- useArcIntlValue,
126
- ArcIntlProvider,
127
- useArcIntl,
128
- }
133
+ };