@umituz/react-native-localization 3.7.21 → 3.7.23
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.
package/package.json
CHANGED
|
@@ -14,6 +14,9 @@ import type { Language } from '../../domain/repositories/ILocalizationRepository
|
|
|
14
14
|
* Provides current language, RTL state, language switching, and translation function
|
|
15
15
|
*/
|
|
16
16
|
export const useLocalization = () => {
|
|
17
|
+
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
18
|
+
console.log('[useLocalization] Hook called');
|
|
19
|
+
}
|
|
17
20
|
const store = useLocalizationStore();
|
|
18
21
|
const { t } = useTranslationFunction();
|
|
19
22
|
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* - i18n setup
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
declare const __DEV__: boolean;
|
|
11
|
+
|
|
10
12
|
import { storageRepository } from '@umituz/react-native-design-system';
|
|
11
13
|
import i18n from '../config/i18n';
|
|
12
14
|
import { languageRepository } from '../repository/LanguageRepository';
|
|
@@ -31,7 +33,9 @@ export class LanguageInitializer {
|
|
|
31
33
|
|
|
32
34
|
return finalLanguage;
|
|
33
35
|
} catch {
|
|
34
|
-
|
|
36
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
37
|
+
console.warn("[LanguageInitializer] Failed to restore language, falling back to device locale");
|
|
38
|
+
}
|
|
35
39
|
return await this.setupFallbackLanguage();
|
|
36
40
|
}
|
|
37
41
|
}
|
|
@@ -66,14 +70,12 @@ export class LanguageInitializer {
|
|
|
66
70
|
languageCode: string;
|
|
67
71
|
isRTL: boolean;
|
|
68
72
|
}> {
|
|
69
|
-
try
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
throw fallbackError;
|
|
77
|
-
}
|
|
73
|
+
// No try-catch needed - let errors propagate naturally
|
|
74
|
+
// This preserves the full stack trace
|
|
75
|
+
await i18n.changeLanguage(DEFAULT_LANGUAGE);
|
|
76
|
+
return {
|
|
77
|
+
languageCode: DEFAULT_LANGUAGE,
|
|
78
|
+
isRTL: false,
|
|
79
|
+
};
|
|
78
80
|
}
|
|
79
81
|
}
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
* - Persistence
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
declare const __DEV__: boolean;
|
|
10
|
+
|
|
9
11
|
import { storageRepository } from '@umituz/react-native-design-system';
|
|
10
12
|
import i18n from '../config/i18n';
|
|
11
13
|
import { languageRepository } from '../repository/LanguageRepository';
|
|
@@ -20,17 +22,27 @@ export class LanguageSwitcher {
|
|
|
20
22
|
languageCode: string;
|
|
21
23
|
isRTL: boolean;
|
|
22
24
|
}> {
|
|
23
|
-
|
|
25
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
26
|
+
console.log('[LanguageSwitcher] switchLanguage called:', languageCode);
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
const language = languageRepository.getLanguageByCode(languageCode);
|
|
25
|
-
console.log('[LanguageSwitcher] Language object:', language);
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
32
|
+
console.log('[LanguageSwitcher] Language object:', language);
|
|
33
|
+
}
|
|
34
|
+
|
|
28
35
|
await i18n.changeLanguage(languageCode);
|
|
29
|
-
console.log('[LanguageSwitcher] i18n language changed to:', i18n.language);
|
|
30
36
|
|
|
31
|
-
|
|
37
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
38
|
+
console.log('[LanguageSwitcher] i18n language changed to:', i18n.language);
|
|
39
|
+
}
|
|
40
|
+
|
|
32
41
|
await storageRepository.setString(LANGUAGE_STORAGE_KEY, languageCode);
|
|
33
|
-
|
|
42
|
+
|
|
43
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
44
|
+
console.log('[LanguageSwitcher] Saved to storage');
|
|
45
|
+
}
|
|
34
46
|
|
|
35
47
|
return {
|
|
36
48
|
languageCode,
|
|
@@ -9,8 +9,16 @@ import { LanguageInitializer } from './LanguageInitializer';
|
|
|
9
9
|
import { LanguageSwitcher } from './LanguageSwitcher';
|
|
10
10
|
import { languageRepository } from '../repository/LanguageRepository';
|
|
11
11
|
|
|
12
|
+
declare const __DEV__: boolean;
|
|
13
|
+
|
|
12
14
|
type LocalizationStoreType = LocalizationState & LocalizationActions & LocalizationGetters;
|
|
13
15
|
|
|
16
|
+
// Mutex to prevent race condition in initialize
|
|
17
|
+
let initializeInProgress = false;
|
|
18
|
+
// Debounce timer for language switching
|
|
19
|
+
let languageSwitchTimer: ReturnType<typeof setTimeout> | null = null;
|
|
20
|
+
const LANGUAGE_SWITCH_DEBOUNCE_MS = 300;
|
|
21
|
+
|
|
14
22
|
export const useLocalizationStore = create<LocalizationStoreType>((set, get) => ({
|
|
15
23
|
// State
|
|
16
24
|
currentLanguage: 'en-US',
|
|
@@ -21,10 +29,15 @@ export const useLocalizationStore = create<LocalizationStoreType>((set, get) =>
|
|
|
21
29
|
// Actions
|
|
22
30
|
initialize: async () => {
|
|
23
31
|
const { isInitialized: alreadyInitialized } = get();
|
|
24
|
-
|
|
32
|
+
|
|
33
|
+
// Atomic check: both state flag AND in-progress mutex
|
|
34
|
+
if (alreadyInitialized || initializeInProgress) {
|
|
25
35
|
return;
|
|
26
36
|
}
|
|
27
37
|
|
|
38
|
+
// Set mutex immediately (synchronous)
|
|
39
|
+
initializeInProgress = true;
|
|
40
|
+
|
|
28
41
|
try {
|
|
29
42
|
const result = await LanguageInitializer.initialize();
|
|
30
43
|
|
|
@@ -39,19 +52,49 @@ export const useLocalizationStore = create<LocalizationStoreType>((set, get) =>
|
|
|
39
52
|
isRTL: false,
|
|
40
53
|
isInitialized: true,
|
|
41
54
|
});
|
|
55
|
+
} finally {
|
|
56
|
+
// Reset mutex after completion (success or failure)
|
|
57
|
+
initializeInProgress = false;
|
|
42
58
|
}
|
|
43
59
|
},
|
|
44
60
|
|
|
45
61
|
setLanguage: async (languageCode: string) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
62
|
+
// Debounce rapid language switches
|
|
63
|
+
if (languageSwitchTimer) {
|
|
64
|
+
clearTimeout(languageSwitchTimer);
|
|
65
|
+
}
|
|
49
66
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
67
|
+
return new Promise<void>((resolve) => {
|
|
68
|
+
languageSwitchTimer = setTimeout(async () => {
|
|
69
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
70
|
+
console.log('[LocalizationStore] setLanguage called:', languageCode);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const result = await LanguageSwitcher.switchLanguage(languageCode);
|
|
75
|
+
|
|
76
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
77
|
+
console.log('[LocalizationStore] LanguageSwitcher result:', result);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
set({
|
|
81
|
+
currentLanguage: result.languageCode,
|
|
82
|
+
isRTL: result.isRTL,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
86
|
+
console.log('[LocalizationStore] Store updated with new language:', result.languageCode);
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
90
|
+
console.error('[LocalizationStore] Language switch failed:', error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
languageSwitchTimer = null;
|
|
95
|
+
resolve();
|
|
96
|
+
}, LANGUAGE_SWITCH_DEBOUNCE_MS);
|
|
53
97
|
});
|
|
54
|
-
console.log('[LocalizationStore] Store updated with new language:', result.languageCode);
|
|
55
98
|
},
|
|
56
99
|
|
|
57
100
|
reset: () => {
|