locon 1.1.1 → 1.1.2
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 +24 -11
- package/dist/index.js +5 -26
- package/dist/index.mjs +5 -26
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -47,19 +47,29 @@ npm install locon
|
|
|
47
47
|
yarn add locon
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
**
|
|
50
|
+
**Peer dependencies**
|
|
51
|
+
|
|
52
|
+
- `react >= 19.0.0`
|
|
53
|
+
- `react-native >= 0.70.0`
|
|
54
|
+
- `react-native-localize >= 3.0.0` (optional, recommended)
|
|
55
|
+
|
|
56
|
+
**Optional: Enhanced language detection**
|
|
57
|
+
|
|
58
|
+
For more accurate device language detection on iOS and Android, install `react-native-localize`:
|
|
51
59
|
|
|
52
60
|
```bash
|
|
53
|
-
|
|
61
|
+
npm install react-native-localize
|
|
62
|
+
# or
|
|
63
|
+
yarn add react-native-localize
|
|
54
64
|
```
|
|
55
65
|
|
|
56
|
-
|
|
66
|
+
Then run pod install for iOS:
|
|
57
67
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
```bash
|
|
69
|
+
cd ios && pod install
|
|
70
|
+
```
|
|
61
71
|
|
|
62
|
-
`react-native-localize` is
|
|
72
|
+
If `react-native-localize` is not installed, `locon` will use fallback methods (native modules, `navigator.languages`, `Intl` API) to detect the system language. The library works fine without it, but `react-native-localize` provides the most accurate detection.
|
|
63
73
|
|
|
64
74
|
---
|
|
65
75
|
|
|
@@ -125,10 +135,13 @@ export default App
|
|
|
125
135
|
If `currentLocale` is not provided and `autodetect` is `true` (default),
|
|
126
136
|
`locon` will:
|
|
127
137
|
|
|
128
|
-
1.
|
|
129
|
-
2.
|
|
130
|
-
|
|
131
|
-
|
|
138
|
+
1. Try to use `react-native-localize` to detect device locale on iOS/Android (if installed)
|
|
139
|
+
2. If `react-native-localize` is not available, use fallback methods in order:
|
|
140
|
+
- Native modules (`SettingsManager` on iOS, `I18nManager` on Android/iOS)
|
|
141
|
+
- `Intl.DateTimeFormat().resolvedOptions().locale`
|
|
142
|
+
3. Match the detected locale (including region) or just language code against your `assets`
|
|
143
|
+
4. If a match is found, use it as initial locale
|
|
144
|
+
5. Otherwise, fall back to `defaultLocale`
|
|
132
145
|
|
|
133
146
|
#### 3. Use the `useLocon()` hook
|
|
134
147
|
|
package/dist/index.js
CHANGED
|
@@ -19,9 +19,11 @@ function getSystemLanguage(availableLocales) {
|
|
|
19
19
|
try {
|
|
20
20
|
try {
|
|
21
21
|
const RNLocalize = __require("react-native-localize");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
if (RNLocalize && typeof RNLocalize.findBestLanguageTag === "function") {
|
|
23
|
+
const best = RNLocalize.findBestLanguageTag(availableLocales);
|
|
24
|
+
if (best?.languageTag) {
|
|
25
|
+
return best.languageTag;
|
|
26
|
+
}
|
|
25
27
|
}
|
|
26
28
|
} catch (localizeError) {
|
|
27
29
|
}
|
|
@@ -66,29 +68,6 @@ function getSystemLanguage(availableLocales) {
|
|
|
66
68
|
}
|
|
67
69
|
} catch (rnError) {
|
|
68
70
|
}
|
|
69
|
-
const navigatorObj = typeof navigator !== "undefined" ? navigator : null;
|
|
70
|
-
const preferredLanguages = navigatorObj?.languages;
|
|
71
|
-
if (preferredLanguages) {
|
|
72
|
-
for (const lang of preferredLanguages) {
|
|
73
|
-
const languageCode = lang.split("-")[0]?.toLowerCase();
|
|
74
|
-
if (languageCode) {
|
|
75
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
76
|
-
if (supportedLanguage) {
|
|
77
|
-
return supportedLanguage;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const navigatorLanguage = navigatorObj?.language;
|
|
83
|
-
if (navigatorLanguage) {
|
|
84
|
-
const languageCode = navigatorLanguage.split("-")[0]?.toLowerCase();
|
|
85
|
-
if (languageCode) {
|
|
86
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
87
|
-
if (supportedLanguage) {
|
|
88
|
-
return supportedLanguage;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
71
|
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
93
72
|
if (systemLocale) {
|
|
94
73
|
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
package/dist/index.mjs
CHANGED
|
@@ -15,9 +15,11 @@ function getSystemLanguage(availableLocales) {
|
|
|
15
15
|
try {
|
|
16
16
|
try {
|
|
17
17
|
const RNLocalize = __require("react-native-localize");
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
if (RNLocalize && typeof RNLocalize.findBestLanguageTag === "function") {
|
|
19
|
+
const best = RNLocalize.findBestLanguageTag(availableLocales);
|
|
20
|
+
if (best?.languageTag) {
|
|
21
|
+
return best.languageTag;
|
|
22
|
+
}
|
|
21
23
|
}
|
|
22
24
|
} catch (localizeError) {
|
|
23
25
|
}
|
|
@@ -62,29 +64,6 @@ function getSystemLanguage(availableLocales) {
|
|
|
62
64
|
}
|
|
63
65
|
} catch (rnError) {
|
|
64
66
|
}
|
|
65
|
-
const navigatorObj = typeof navigator !== "undefined" ? navigator : null;
|
|
66
|
-
const preferredLanguages = navigatorObj?.languages;
|
|
67
|
-
if (preferredLanguages) {
|
|
68
|
-
for (const lang of preferredLanguages) {
|
|
69
|
-
const languageCode = lang.split("-")[0]?.toLowerCase();
|
|
70
|
-
if (languageCode) {
|
|
71
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
72
|
-
if (supportedLanguage) {
|
|
73
|
-
return supportedLanguage;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
const navigatorLanguage = navigatorObj?.language;
|
|
79
|
-
if (navigatorLanguage) {
|
|
80
|
-
const languageCode = navigatorLanguage.split("-")[0]?.toLowerCase();
|
|
81
|
-
if (languageCode) {
|
|
82
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
83
|
-
if (supportedLanguage) {
|
|
84
|
-
return supportedLanguage;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
67
|
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
89
68
|
if (systemLocale) {
|
|
90
69
|
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "locon",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "locon is a simple and lightweight library for React Native that provides a simple way to manage translations in your app.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -30,10 +30,13 @@
|
|
|
30
30
|
"license": "ISC",
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"react": ">=19.0.0",
|
|
33
|
-
"react-native": ">=0.70.0"
|
|
33
|
+
"react-native": ">=0.70.0",
|
|
34
|
+
"react-native-localize": "^3.0.0"
|
|
34
35
|
},
|
|
35
|
-
"
|
|
36
|
-
"react-native-localize":
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"react-native-localize": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
37
40
|
},
|
|
38
41
|
"devDependencies": {
|
|
39
42
|
"@babel/preset-react": "^7.24.7",
|