@umituz/react-native-localization 1.8.1 → 1.9.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/README.md
CHANGED
|
@@ -26,9 +26,11 @@ yarn add @umituz/react-native-localization
|
|
|
26
26
|
Make sure you have the following peer dependencies installed:
|
|
27
27
|
|
|
28
28
|
```bash
|
|
29
|
-
npm install zustand i18next react-i18next expo-localization @react-native-
|
|
29
|
+
npm install zustand i18next react-i18next expo-localization @umituz/react-native-storage
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
+
**Note:** `@umituz/react-native-storage` is required for persistent language preferences. It provides type-safe storage operations following Domain-Driven Design principles.
|
|
33
|
+
|
|
32
34
|
## Quick Start
|
|
33
35
|
|
|
34
36
|
### 1. Wrap Your App with LocalizationProvider
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-localization",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "English-only localization system for React Native apps with i18n support",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -39,13 +39,13 @@
|
|
|
39
39
|
"i18next": "^23.0.0",
|
|
40
40
|
"react-i18next": "^14.0.0",
|
|
41
41
|
"zustand": "^5.0.2",
|
|
42
|
-
"expo-localization": "~15.0.0"
|
|
43
|
-
"@react-native-async-storage/async-storage": "^1.21.0"
|
|
42
|
+
"expo-localization": "~15.0.0"
|
|
44
43
|
},
|
|
45
44
|
"peerDependencies": {
|
|
46
45
|
"react": ">=18.2.0",
|
|
47
46
|
"react-native": ">=0.74.0",
|
|
48
|
-
"@react-navigation/native": ">=6.0.0"
|
|
47
|
+
"@react-navigation/native": ">=6.0.0",
|
|
48
|
+
"@umituz/react-native-storage": "latest"
|
|
49
49
|
},
|
|
50
50
|
"peerDependenciesMeta": {
|
|
51
51
|
"@react-navigation/native": {
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"i18next": "^23.0.0",
|
|
63
63
|
"react-i18next": "^14.0.0",
|
|
64
64
|
"expo-localization": "~15.0.0",
|
|
65
|
-
"@react-native-
|
|
65
|
+
"@umituz/react-native-storage": "latest"
|
|
66
66
|
},
|
|
67
67
|
"publishConfig": {
|
|
68
68
|
"access": "public"
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Localization Store
|
|
3
3
|
* Zustand state management for language preferences with AsyncStorage persistence
|
|
4
|
+
*
|
|
5
|
+
* DDD ARCHITECTURE: Uses @umituz/react-native-storage for all storage operations
|
|
6
|
+
* - Type-safe storage with StorageKey
|
|
7
|
+
* - Result pattern for error handling
|
|
8
|
+
* - Single source of truth for all storage
|
|
4
9
|
*/
|
|
5
10
|
|
|
6
11
|
import { create } from 'zustand';
|
|
7
12
|
import { useTranslation } from 'react-i18next';
|
|
8
|
-
import {
|
|
13
|
+
import { storageRepository, unwrap } from '@umituz/react-native-storage';
|
|
9
14
|
import i18n from '../config/i18n';
|
|
10
15
|
import { SUPPORTED_LANGUAGES, DEFAULT_LANGUAGE, getLanguageByCode, getDeviceLocale } from '../config/languages';
|
|
11
16
|
import type { Language } from '../../domain/repositories/ILocalizationRepository';
|
|
12
17
|
|
|
18
|
+
// Storage key for language preference
|
|
19
|
+
const LANGUAGE_STORAGE_KEY = '@localization:language';
|
|
20
|
+
|
|
13
21
|
interface LocalizationState {
|
|
14
22
|
currentLanguage: string;
|
|
15
23
|
isRTL: boolean;
|
|
@@ -42,7 +50,8 @@ export const useLocalizationStore = create<LocalizationState>((set, get) => ({
|
|
|
42
50
|
}
|
|
43
51
|
|
|
44
52
|
// Get saved language preference
|
|
45
|
-
const
|
|
53
|
+
const result = await storageRepository.getString(LANGUAGE_STORAGE_KEY, DEFAULT_LANGUAGE);
|
|
54
|
+
const savedLanguage = unwrap(result, DEFAULT_LANGUAGE);
|
|
46
55
|
|
|
47
56
|
// ✅ DEVICE LOCALE DETECTION: Use device locale on first launch
|
|
48
57
|
let languageCode: string;
|
|
@@ -53,7 +62,7 @@ export const useLocalizationStore = create<LocalizationState>((set, get) => ({
|
|
|
53
62
|
// First launch → Detect device locale automatically
|
|
54
63
|
languageCode = getDeviceLocale();
|
|
55
64
|
// Save detected locale for future launches
|
|
56
|
-
await
|
|
65
|
+
await storageRepository.setString(LANGUAGE_STORAGE_KEY, languageCode);
|
|
57
66
|
}
|
|
58
67
|
|
|
59
68
|
// ✅ DEFENSIVE: Validate language exists, fallback to default
|
|
@@ -106,7 +115,7 @@ export const useLocalizationStore = create<LocalizationState>((set, get) => ({
|
|
|
106
115
|
});
|
|
107
116
|
|
|
108
117
|
// Persist language preference
|
|
109
|
-
await
|
|
118
|
+
await storageRepository.setString(LANGUAGE_STORAGE_KEY, languageCode);
|
|
110
119
|
} catch (error) {
|
|
111
120
|
throw error;
|
|
112
121
|
}
|