locon 1.1.2 → 1.2.1
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 +11 -24
- package/dist/index.js +53 -22
- package/dist/index.mjs +53 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,29 +47,19 @@ npm install locon
|
|
|
47
47
|
yarn add locon
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
**
|
|
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`:
|
|
50
|
+
**Important:** After installation, run pod install for iOS:
|
|
59
51
|
|
|
60
52
|
```bash
|
|
61
|
-
|
|
62
|
-
# or
|
|
63
|
-
yarn add react-native-localize
|
|
53
|
+
cd ios && pod install
|
|
64
54
|
```
|
|
65
55
|
|
|
66
|
-
|
|
56
|
+
**Peer dependencies**
|
|
67
57
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
58
|
+
- `react >= 19.0.0`
|
|
59
|
+
- `react-native >= 0.70.0`
|
|
60
|
+
- `react-native-localize >= 3.0.0` (installed automatically)
|
|
71
61
|
|
|
72
|
-
|
|
62
|
+
`react-native-localize` is used for accurate device language detection on iOS and Android.
|
|
73
63
|
|
|
74
64
|
---
|
|
75
65
|
|
|
@@ -135,13 +125,10 @@ export default App
|
|
|
135
125
|
If `currentLocale` is not provided and `autodetect` is `true` (default),
|
|
136
126
|
`locon` will:
|
|
137
127
|
|
|
138
|
-
1.
|
|
139
|
-
2.
|
|
140
|
-
|
|
141
|
-
|
|
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`
|
|
128
|
+
1. Use `react-native-localize` to detect device locale on iOS/Android
|
|
129
|
+
2. Match the detected locale (including region) or just language code against your `assets`
|
|
130
|
+
3. If a match is found, use it as initial locale
|
|
131
|
+
4. Otherwise, fall back to `defaultLocale`
|
|
145
132
|
|
|
146
133
|
#### 3. Use the `useLocon()` hook
|
|
147
134
|
|
package/dist/index.js
CHANGED
|
@@ -19,32 +19,62 @@ function getSystemLanguage(availableLocales) {
|
|
|
19
19
|
try {
|
|
20
20
|
try {
|
|
21
21
|
const RNLocalize = __require("react-native-localize");
|
|
22
|
-
if (
|
|
22
|
+
if (typeof RNLocalize.findBestLanguageTag === "function") {
|
|
23
23
|
const best = RNLocalize.findBestLanguageTag(availableLocales);
|
|
24
24
|
if (best?.languageTag) {
|
|
25
|
-
|
|
25
|
+
const { languageTag } = best;
|
|
26
|
+
const languageCode = languageTag.split("-")[0]?.toLowerCase();
|
|
27
|
+
if (availableLocales.includes(languageTag)) {
|
|
28
|
+
return languageTag;
|
|
29
|
+
}
|
|
30
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
31
|
+
return languageCode;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (typeof RNLocalize.getLocales === "function") {
|
|
36
|
+
const locales = RNLocalize.getLocales();
|
|
37
|
+
const matchedLocale = locales.find((locale) => {
|
|
38
|
+
const languageCode = locale.languageCode.toLowerCase();
|
|
39
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
if (locale.languageTag) {
|
|
43
|
+
const tagCode = locale.languageTag.split("-")[0]?.toLowerCase();
|
|
44
|
+
if (tagCode && availableLocales.includes(tagCode)) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
});
|
|
50
|
+
if (matchedLocale) {
|
|
51
|
+
const languageCode = matchedLocale.languageCode.toLowerCase();
|
|
52
|
+
if (availableLocales.includes(languageCode)) {
|
|
53
|
+
return languageCode;
|
|
54
|
+
}
|
|
55
|
+
if (matchedLocale.languageTag) {
|
|
56
|
+
const tagCode = matchedLocale.languageTag.split("-")[0]?.toLowerCase();
|
|
57
|
+
if (tagCode && availableLocales.includes(tagCode)) {
|
|
58
|
+
return tagCode;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
26
61
|
}
|
|
27
62
|
}
|
|
28
63
|
} catch (localizeError) {
|
|
64
|
+
console.warn("[locon] \u274C react-native-localize error:", localizeError);
|
|
29
65
|
}
|
|
30
66
|
try {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const SettingsManager = NativeModules?.SettingsManager;
|
|
67
|
+
const { NativeModules, Platform } = __require("react-native");
|
|
68
|
+
if (Platform.OS === "ios") {
|
|
69
|
+
const { SettingsManager } = NativeModules;
|
|
35
70
|
if (SettingsManager?.settings?.AppleLanguages) {
|
|
36
71
|
const appleLanguages = SettingsManager.settings.AppleLanguages;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
41
|
-
if (supportedLanguage) {
|
|
42
|
-
return supportedLanguage;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
72
|
+
const supportedLanguage = appleLanguages.map((lang) => lang.split("-")[0]?.toLowerCase()).filter((languageCode) => Boolean(languageCode)).find((languageCode) => availableLocales.includes(languageCode));
|
|
73
|
+
if (supportedLanguage) {
|
|
74
|
+
return supportedLanguage;
|
|
45
75
|
}
|
|
46
76
|
}
|
|
47
|
-
const I18nManager = NativeModules
|
|
77
|
+
const { I18nManager } = NativeModules;
|
|
48
78
|
if (I18nManager?.localeIdentifier) {
|
|
49
79
|
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
50
80
|
if (languageCode) {
|
|
@@ -54,8 +84,8 @@ function getSystemLanguage(availableLocales) {
|
|
|
54
84
|
}
|
|
55
85
|
}
|
|
56
86
|
}
|
|
57
|
-
} else if (Platform
|
|
58
|
-
const I18nManager = NativeModules
|
|
87
|
+
} else if (Platform.OS === "android") {
|
|
88
|
+
const { I18nManager } = NativeModules;
|
|
59
89
|
if (I18nManager?.localeIdentifier) {
|
|
60
90
|
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
61
91
|
if (languageCode) {
|
|
@@ -67,6 +97,7 @@ function getSystemLanguage(availableLocales) {
|
|
|
67
97
|
}
|
|
68
98
|
}
|
|
69
99
|
} catch (rnError) {
|
|
100
|
+
console.warn("[locon] \u274C react-native error:", rnError);
|
|
70
101
|
}
|
|
71
102
|
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
72
103
|
if (systemLocale) {
|
|
@@ -80,10 +111,11 @@ function getSystemLanguage(availableLocales) {
|
|
|
80
111
|
}
|
|
81
112
|
return null;
|
|
82
113
|
} catch (error) {
|
|
83
|
-
console.
|
|
114
|
+
console.warn("[locon] \u274C Could not detect system language:", error);
|
|
84
115
|
return null;
|
|
85
116
|
}
|
|
86
117
|
}
|
|
118
|
+
var getSystemLanguage_default = getSystemLanguage;
|
|
87
119
|
function useLocon() {
|
|
88
120
|
const locon = react.useContext(LoconContext);
|
|
89
121
|
return locon;
|
|
@@ -118,10 +150,9 @@ function Locon({
|
|
|
118
150
|
return currentLocaleProps;
|
|
119
151
|
}
|
|
120
152
|
if (autodetect) {
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return systemLang;
|
|
153
|
+
const systemLocale = getSystemLanguage_default(Object.keys(assets));
|
|
154
|
+
if (systemLocale && assets[systemLocale]) {
|
|
155
|
+
return systemLocale;
|
|
125
156
|
}
|
|
126
157
|
}
|
|
127
158
|
return defaultLocale;
|
package/dist/index.mjs
CHANGED
|
@@ -15,32 +15,62 @@ function getSystemLanguage(availableLocales) {
|
|
|
15
15
|
try {
|
|
16
16
|
try {
|
|
17
17
|
const RNLocalize = __require("react-native-localize");
|
|
18
|
-
if (
|
|
18
|
+
if (typeof RNLocalize.findBestLanguageTag === "function") {
|
|
19
19
|
const best = RNLocalize.findBestLanguageTag(availableLocales);
|
|
20
20
|
if (best?.languageTag) {
|
|
21
|
-
|
|
21
|
+
const { languageTag } = best;
|
|
22
|
+
const languageCode = languageTag.split("-")[0]?.toLowerCase();
|
|
23
|
+
if (availableLocales.includes(languageTag)) {
|
|
24
|
+
return languageTag;
|
|
25
|
+
}
|
|
26
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
27
|
+
return languageCode;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (typeof RNLocalize.getLocales === "function") {
|
|
32
|
+
const locales = RNLocalize.getLocales();
|
|
33
|
+
const matchedLocale = locales.find((locale) => {
|
|
34
|
+
const languageCode = locale.languageCode.toLowerCase();
|
|
35
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
if (locale.languageTag) {
|
|
39
|
+
const tagCode = locale.languageTag.split("-")[0]?.toLowerCase();
|
|
40
|
+
if (tagCode && availableLocales.includes(tagCode)) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
});
|
|
46
|
+
if (matchedLocale) {
|
|
47
|
+
const languageCode = matchedLocale.languageCode.toLowerCase();
|
|
48
|
+
if (availableLocales.includes(languageCode)) {
|
|
49
|
+
return languageCode;
|
|
50
|
+
}
|
|
51
|
+
if (matchedLocale.languageTag) {
|
|
52
|
+
const tagCode = matchedLocale.languageTag.split("-")[0]?.toLowerCase();
|
|
53
|
+
if (tagCode && availableLocales.includes(tagCode)) {
|
|
54
|
+
return tagCode;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
22
57
|
}
|
|
23
58
|
}
|
|
24
59
|
} catch (localizeError) {
|
|
60
|
+
console.warn("[locon] \u274C react-native-localize error:", localizeError);
|
|
25
61
|
}
|
|
26
62
|
try {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const SettingsManager = NativeModules?.SettingsManager;
|
|
63
|
+
const { NativeModules, Platform } = __require("react-native");
|
|
64
|
+
if (Platform.OS === "ios") {
|
|
65
|
+
const { SettingsManager } = NativeModules;
|
|
31
66
|
if (SettingsManager?.settings?.AppleLanguages) {
|
|
32
67
|
const appleLanguages = SettingsManager.settings.AppleLanguages;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
37
|
-
if (supportedLanguage) {
|
|
38
|
-
return supportedLanguage;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
68
|
+
const supportedLanguage = appleLanguages.map((lang) => lang.split("-")[0]?.toLowerCase()).filter((languageCode) => Boolean(languageCode)).find((languageCode) => availableLocales.includes(languageCode));
|
|
69
|
+
if (supportedLanguage) {
|
|
70
|
+
return supportedLanguage;
|
|
41
71
|
}
|
|
42
72
|
}
|
|
43
|
-
const I18nManager = NativeModules
|
|
73
|
+
const { I18nManager } = NativeModules;
|
|
44
74
|
if (I18nManager?.localeIdentifier) {
|
|
45
75
|
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
46
76
|
if (languageCode) {
|
|
@@ -50,8 +80,8 @@ function getSystemLanguage(availableLocales) {
|
|
|
50
80
|
}
|
|
51
81
|
}
|
|
52
82
|
}
|
|
53
|
-
} else if (Platform
|
|
54
|
-
const I18nManager = NativeModules
|
|
83
|
+
} else if (Platform.OS === "android") {
|
|
84
|
+
const { I18nManager } = NativeModules;
|
|
55
85
|
if (I18nManager?.localeIdentifier) {
|
|
56
86
|
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
57
87
|
if (languageCode) {
|
|
@@ -63,6 +93,7 @@ function getSystemLanguage(availableLocales) {
|
|
|
63
93
|
}
|
|
64
94
|
}
|
|
65
95
|
} catch (rnError) {
|
|
96
|
+
console.warn("[locon] \u274C react-native error:", rnError);
|
|
66
97
|
}
|
|
67
98
|
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
68
99
|
if (systemLocale) {
|
|
@@ -76,10 +107,11 @@ function getSystemLanguage(availableLocales) {
|
|
|
76
107
|
}
|
|
77
108
|
return null;
|
|
78
109
|
} catch (error) {
|
|
79
|
-
console.
|
|
110
|
+
console.warn("[locon] \u274C Could not detect system language:", error);
|
|
80
111
|
return null;
|
|
81
112
|
}
|
|
82
113
|
}
|
|
114
|
+
var getSystemLanguage_default = getSystemLanguage;
|
|
83
115
|
function useLocon() {
|
|
84
116
|
const locon = useContext(LoconContext);
|
|
85
117
|
return locon;
|
|
@@ -114,10 +146,9 @@ function Locon({
|
|
|
114
146
|
return currentLocaleProps;
|
|
115
147
|
}
|
|
116
148
|
if (autodetect) {
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return systemLang;
|
|
149
|
+
const systemLocale = getSystemLanguage_default(Object.keys(assets));
|
|
150
|
+
if (systemLocale && assets[systemLocale]) {
|
|
151
|
+
return systemLocale;
|
|
121
152
|
}
|
|
122
153
|
}
|
|
123
154
|
return defaultLocale;
|
package/package.json
CHANGED