@arc-js/intl 0.0.91 → 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 = {}) {
|
package/hooks/useTranslation.jsx
CHANGED
|
@@ -6,7 +6,7 @@ export function useTranslation(moduleName) {
|
|
|
6
6
|
const [isModuleLoading, setIsModuleLoading] = useState(false);
|
|
7
7
|
const [moduleLoaded, setModuleLoaded] = useState(!moduleName);
|
|
8
8
|
const t = useCallback((key, params, options) => {
|
|
9
|
-
if (moduleName && !moduleLoaded
|
|
9
|
+
if (moduleName && !moduleLoaded) {
|
|
10
10
|
console.warn(`Module ${moduleName} not loaded yet, using default value`);
|
|
11
11
|
return options?.defaultValue || key;
|
|
12
12
|
}
|
|
@@ -17,7 +17,7 @@ export function useTranslation(moduleName) {
|
|
|
17
17
|
return arcIntl.t(key, params || {}, finalOptions);
|
|
18
18
|
}, [arcIntl, moduleName, moduleLoaded]);
|
|
19
19
|
useEffect(() => {
|
|
20
|
-
if (moduleName && !moduleLoaded) {
|
|
20
|
+
if (moduleName && !moduleLoaded && !arcIntl.isLoading) {
|
|
21
21
|
setIsModuleLoading(true);
|
|
22
22
|
arcIntl.loadModuleTranslations(moduleName).then(() => {
|
|
23
23
|
setModuleLoaded(true);
|
|
@@ -27,7 +27,7 @@ export function useTranslation(moduleName) {
|
|
|
27
27
|
setIsModuleLoading(false);
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
}, [moduleName, moduleLoaded, arcIntl]);
|
|
30
|
+
}, [moduleName, moduleLoaded, arcIntl, arcIntl.isLoading]);
|
|
31
31
|
return {
|
|
32
32
|
t,
|
|
33
33
|
changeLocale: arcIntl.changeLocale,
|
package/hooks/useTranslation.tsx
CHANGED
|
@@ -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
|
|
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.
|
|
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": [],
|
|
@@ -9,30 +9,17 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
|
|
|
9
9
|
const [currentLocale, setCurrentLocale] = useState(getSavedLocale(supportedLocales));
|
|
10
10
|
const [isLoading, setIsLoading] = useState(true);
|
|
11
11
|
const [loadedModules, setLoadedModules] = useState(new Set());
|
|
12
|
+
const [initialized, setInitialized] = useState(false);
|
|
12
13
|
useEffect(() => {
|
|
13
14
|
setTranslationsConfig(translationsConfig);
|
|
14
|
-
}, [translationsConfig])
|
|
15
|
-
|
|
16
|
-
const loadAllModules = async () => {
|
|
17
|
-
if (translationsConfig.modules) {
|
|
18
|
-
const moduleNames = Object.keys(translationsConfig.modules);
|
|
19
|
-
for (const moduleName of moduleNames) {
|
|
20
|
-
try {
|
|
21
|
-
await service.loadModule(moduleName);
|
|
22
|
-
setLoadedModules(prev => new Set(prev).add(moduleName));
|
|
23
|
-
} catch (error) {
|
|
24
|
-
console.error(`Failed to load module ${moduleName}:`, error);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
loadAllModules();
|
|
30
|
-
}, [translationsConfig.modules, service]);
|
|
15
|
+
}, [translationsConfig])
|
|
16
|
+
|
|
31
17
|
useEffect(() => {
|
|
32
18
|
const initialize = async () => {
|
|
33
19
|
setIsLoading(true);
|
|
34
20
|
try {
|
|
35
21
|
await service.initialize(currentLocale);
|
|
22
|
+
setInitialized(true);
|
|
36
23
|
} catch (error) {
|
|
37
24
|
console.error('Failed to initialize translations:', error);
|
|
38
25
|
} finally {
|
|
@@ -40,7 +27,23 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
|
|
|
40
27
|
}
|
|
41
28
|
};
|
|
42
29
|
initialize();
|
|
43
|
-
}, [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]);
|
|
44
47
|
const changeLocale = useCallback(async locale => {
|
|
45
48
|
if (!supportedLocales.includes(locale)) {
|
|
46
49
|
console.warn(`Locale ${locale} is not supported`);
|
|
@@ -49,23 +52,28 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
|
|
|
49
52
|
if (locale === currentLocale) return;
|
|
50
53
|
setIsLoading(true);
|
|
51
54
|
try {
|
|
55
|
+
await service.initialize(locale);
|
|
52
56
|
setCurrentLocale(locale);
|
|
53
57
|
saveLocale(locale);
|
|
54
58
|
} catch (error) {
|
|
55
59
|
console.error('Failed to change locale:', error);
|
|
60
|
+
} finally {
|
|
56
61
|
setIsLoading(false);
|
|
57
62
|
}
|
|
58
|
-
}, [currentLocale, supportedLocales]);
|
|
63
|
+
}, [currentLocale, supportedLocales, service]);
|
|
59
64
|
const loadModuleTranslations = useCallback(async moduleName => {
|
|
65
|
+
if (loadedModules.has(moduleName)) return;
|
|
60
66
|
setIsLoading(true);
|
|
61
67
|
try {
|
|
62
68
|
await service.loadModule(moduleName);
|
|
69
|
+
setLoadedModules(prev => new Set(prev).add(moduleName));
|
|
63
70
|
} catch (error) {
|
|
64
71
|
console.error(`Failed to load module ${moduleName}:`, error);
|
|
72
|
+
throw error;
|
|
65
73
|
} finally {
|
|
66
74
|
setIsLoading(false);
|
|
67
75
|
}
|
|
68
|
-
}, [service]);
|
|
76
|
+
}, [service, loadedModules]);
|
|
69
77
|
const t = useCallback((key, params, options) => {
|
|
70
78
|
return service.t(key, params || {}, options || {});
|
|
71
79
|
}, [service]);
|
|
@@ -74,7 +82,8 @@ const useArcIntlValue = (translationsConfig, supportedLocales = SUPPORTED_LOCALE
|
|
|
74
82
|
changeLocale,
|
|
75
83
|
currentLocale,
|
|
76
84
|
isLoading,
|
|
77
|
-
loadModuleTranslations
|
|
85
|
+
loadModuleTranslations,
|
|
86
|
+
loadedModules: Array.from(loadedModules)
|
|
78
87
|
};
|
|
79
88
|
};
|
|
80
89
|
export const ArcIntlProvider = ({
|
|
@@ -91,9 +100,4 @@ export const useArcIntl = () => {
|
|
|
91
100
|
const context = useContext(ArcIntlContext);
|
|
92
101
|
if (!context) throw new Error('useArcIntl must be used within an ArcIntlProvider');
|
|
93
102
|
return context;
|
|
94
|
-
};
|
|
95
|
-
export default {
|
|
96
|
-
useArcIntlValue,
|
|
97
|
-
ArcIntlProvider,
|
|
98
|
-
useArcIntl
|
|
99
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
|
-
|
|
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
|
+
};
|