@umituz/react-native-localization 1.6.4 → 1.6.6
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
|
@@ -197,27 +197,33 @@ const buildResources = (): Record<string, { translation: any }> => {
|
|
|
197
197
|
|
|
198
198
|
const resources = buildResources();
|
|
199
199
|
|
|
200
|
-
// Debug: Log loaded resources in development
|
|
201
|
-
|
|
202
|
-
if (typeof
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
200
|
+
// Debug: Log loaded resources in development (only once to prevent spam)
|
|
201
|
+
// Use global flag to prevent multiple logs when module is imported multiple times
|
|
202
|
+
if (typeof global !== 'undefined' && !(global as any).__i18n_resources_logged) {
|
|
203
|
+
/* eslint-disable-next-line no-console */
|
|
204
|
+
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
205
|
+
console.log('🌍 i18n Resources loaded:', {
|
|
206
|
+
languages: Object.keys(resources),
|
|
207
|
+
enUSKeys: resources['en-US']?.translation ? Object.keys(resources['en-US'].translation) : [],
|
|
208
|
+
hasGoals: !!resources['en-US']?.translation?.goals,
|
|
209
|
+
navigationKeys: resources['en-US']?.translation?.navigation ? Object.keys(resources['en-US'].translation.navigation) : [],
|
|
210
|
+
hasMilestones: !!resources['en-US']?.translation?.navigation?.milestones,
|
|
211
|
+
hasStatistics: !!resources['en-US']?.translation?.navigation?.statistics,
|
|
212
|
+
});
|
|
213
|
+
(global as any).__i18n_resources_logged = true;
|
|
214
|
+
}
|
|
211
215
|
}
|
|
212
216
|
|
|
213
217
|
/**
|
|
214
218
|
* Initialize i18next
|
|
215
|
-
*
|
|
219
|
+
* CRITICAL: Check i18n.isInitialized to prevent multiple initializations
|
|
220
|
+
* This prevents "i18next is already initialized" warnings when module is imported multiple times
|
|
216
221
|
*/
|
|
217
|
-
let isInitialized = false;
|
|
218
|
-
|
|
219
222
|
const initializeI18n = () => {
|
|
220
|
-
if (
|
|
223
|
+
// CRITICAL: Check if i18n is already initialized (prevents multiple init calls)
|
|
224
|
+
if (i18n.isInitialized) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
221
227
|
|
|
222
228
|
try {
|
|
223
229
|
// Check if initReactI18next is available
|
|
@@ -246,8 +252,6 @@ const initializeI18n = () => {
|
|
|
246
252
|
debug: typeof __DEV__ !== 'undefined' && __DEV__,
|
|
247
253
|
});
|
|
248
254
|
|
|
249
|
-
isInitialized = true;
|
|
250
|
-
|
|
251
255
|
// Debug: Verify initialization
|
|
252
256
|
/* eslint-disable-next-line no-console */
|
|
253
257
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
@@ -269,6 +273,7 @@ const initializeI18n = () => {
|
|
|
269
273
|
// Initialize immediately - no need to defer
|
|
270
274
|
// React Native and React are ready when this module loads
|
|
271
275
|
// Deferring causes race conditions with useTranslation hook
|
|
276
|
+
// CRITICAL: i18n.isInitialized check prevents multiple initializations
|
|
272
277
|
initializeI18n();
|
|
273
278
|
|
|
274
279
|
export default i18n;
|
|
@@ -134,17 +134,22 @@ export const useLocalization = () => {
|
|
|
134
134
|
// Always call useTranslation hook (React hooks rules - must be unconditional)
|
|
135
135
|
// Pass i18n instance explicitly to ensure react-i18next finds it
|
|
136
136
|
// This fixes the "NO_I18NEXT_INSTANCE" error
|
|
137
|
+
// Even if i18n is not fully initialized, useTranslation will handle it gracefully
|
|
138
|
+
// with the explicit i18n instance passed
|
|
137
139
|
const translationResult = useTranslation(undefined, { i18n });
|
|
138
140
|
|
|
139
|
-
// Use translation function from react-i18next
|
|
140
|
-
//
|
|
141
|
-
const t: (key: string, options?: any) => string = translationResult?.t
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
141
|
+
// Use translation function from react-i18next if available and valid
|
|
142
|
+
// Otherwise fallback to direct i18n.t
|
|
143
|
+
const t: (key: string, options?: any) => string = (translationResult?.t && typeof translationResult.t === 'function' && i18n.isInitialized)
|
|
144
|
+
? translationResult.t
|
|
145
|
+
: ((key: string, options?: any) => {
|
|
146
|
+
// Fallback to direct i18n.t if react-i18next is not ready
|
|
147
|
+
if (i18n.isInitialized && typeof i18n.t === 'function') {
|
|
148
|
+
return i18n.t(key, options);
|
|
149
|
+
}
|
|
150
|
+
// Final fallback: return key if i18n is not ready
|
|
151
|
+
return key;
|
|
152
|
+
});
|
|
148
153
|
|
|
149
154
|
return {
|
|
150
155
|
t, // Translation function from react-i18next or i18n fallback
|