@umituz/react-native-localization 2.5.0 → 2.6.0
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
|
@@ -5,22 +5,51 @@
|
|
|
5
5
|
* - React i18next integration
|
|
6
6
|
* - Direct i18n fallback
|
|
7
7
|
* - Type-safe translation function
|
|
8
|
+
* - Auto-namespace detection from dot notation
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
|
-
import { useTranslation } from 'react-i18next';
|
|
11
11
|
import i18n from '../config/i18n';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Hook for translation functionality
|
|
15
|
+
* Supports both formats:
|
|
16
|
+
* - t('namespace:key.subkey') - explicit namespace
|
|
17
|
+
* - t('namespace.key.subkey') - auto-detected namespace (first segment before dot)
|
|
15
18
|
*/
|
|
16
19
|
export const useTranslationFunction = (): ((key: string, options?: any) => string) => {
|
|
17
|
-
// Use direct i18n.t for reliability (no React context issues)
|
|
18
20
|
return (key: string, options?: any): string => {
|
|
19
|
-
if (i18n.isInitialized
|
|
21
|
+
if (!i18n.isInitialized || typeof i18n.t !== 'function') {
|
|
22
|
+
return key;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// If key already has namespace separator (:), use as-is
|
|
26
|
+
if (key.includes(':')) {
|
|
20
27
|
const result = i18n.t(key, options);
|
|
21
28
|
return typeof result === 'string' ? result : String(result);
|
|
22
29
|
}
|
|
23
|
-
|
|
24
|
-
|
|
30
|
+
|
|
31
|
+
// Auto-detect namespace from first dot segment
|
|
32
|
+
// e.g., 'settings.appearance.title' -> namespace: 'settings', key: 'appearance.title'
|
|
33
|
+
const firstDotIndex = key.indexOf('.');
|
|
34
|
+
if (firstDotIndex > 0) {
|
|
35
|
+
const potentialNamespace = key.substring(0, firstDotIndex);
|
|
36
|
+
const restOfKey = key.substring(firstDotIndex + 1);
|
|
37
|
+
|
|
38
|
+
// Check if this namespace exists in i18n resources
|
|
39
|
+
const hasNamespace = i18n.hasResourceBundle(i18n.language, potentialNamespace);
|
|
40
|
+
|
|
41
|
+
if (hasNamespace) {
|
|
42
|
+
const namespacedKey = `${potentialNamespace}:${restOfKey}`;
|
|
43
|
+
const result = i18n.t(namespacedKey, options);
|
|
44
|
+
// If translation found (not same as key), return it
|
|
45
|
+
if (result !== namespacedKey && result !== restOfKey) {
|
|
46
|
+
return typeof result === 'string' ? result : String(result);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Fallback: try original key as-is
|
|
52
|
+
const result = i18n.t(key, options);
|
|
53
|
+
return typeof result === 'string' ? result : String(result);
|
|
25
54
|
};
|
|
26
55
|
};
|