locon 1.1.1 → 1.2.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 +24 -11
- package/dist/index.js +50 -26
- package/dist/index.mjs +50 -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
|
@@ -16,14 +16,53 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
16
16
|
// src/locon/utils/getSystemLanguage.ts
|
|
17
17
|
function getSystemLanguage(availableLocales) {
|
|
18
18
|
if (!availableLocales?.length) return null;
|
|
19
|
+
console.log("[getSystemLanguage] === START ===");
|
|
20
|
+
console.log("[getSystemLanguage] Available locales:", availableLocales);
|
|
19
21
|
try {
|
|
20
22
|
try {
|
|
21
23
|
const RNLocalize = __require("react-native-localize");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
if (RNLocalize) {
|
|
25
|
+
if (typeof RNLocalize.findBestLanguageTag === "function") {
|
|
26
|
+
const best = RNLocalize.findBestLanguageTag(availableLocales);
|
|
27
|
+
if (best?.languageTag) {
|
|
28
|
+
const languageTag = best.languageTag;
|
|
29
|
+
const languageCode = languageTag.split("-")[0]?.toLowerCase();
|
|
30
|
+
if (availableLocales.includes(languageTag)) {
|
|
31
|
+
return languageTag;
|
|
32
|
+
}
|
|
33
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
34
|
+
console.log("[getSystemLanguage] \u2705 Found via findBestLanguageTag:", languageCode);
|
|
35
|
+
return languageCode;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (typeof RNLocalize.getLocales === "function") {
|
|
40
|
+
const locales = RNLocalize.getLocales();
|
|
41
|
+
console.log("[getSystemLanguage] System locales from getLocales():", JSON.stringify(locales, null, 2));
|
|
42
|
+
for (const locale of locales) {
|
|
43
|
+
const languageCode = locale.languageCode?.toLowerCase();
|
|
44
|
+
console.log("[getSystemLanguage] Checking locale:", locale.languageCode, "->", languageCode);
|
|
45
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
46
|
+
console.log("[getSystemLanguage] \u2705 Found via getLocales languageCode:", languageCode);
|
|
47
|
+
return languageCode;
|
|
48
|
+
}
|
|
49
|
+
if (locale.languageTag) {
|
|
50
|
+
const tagCode = locale.languageTag.split("-")[0]?.toLowerCase();
|
|
51
|
+
console.log("[getSystemLanguage] Checking languageTag:", locale.languageTag, "->", tagCode);
|
|
52
|
+
if (tagCode && availableLocales.includes(tagCode)) {
|
|
53
|
+
console.log("[getSystemLanguage] \u2705 Found via getLocales languageTag:", tagCode);
|
|
54
|
+
return tagCode;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
console.log("[getSystemLanguage] \u26A0\uFE0F getLocales() is not available");
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
console.log("[getSystemLanguage] \u26A0\uFE0F react-native-localize module not found or not linked");
|
|
25
63
|
}
|
|
26
64
|
} catch (localizeError) {
|
|
65
|
+
console.log("[getSystemLanguage] \u274C react-native-localize error:", localizeError);
|
|
27
66
|
}
|
|
28
67
|
try {
|
|
29
68
|
const reactNative = __require("react-native");
|
|
@@ -32,11 +71,13 @@ function getSystemLanguage(availableLocales) {
|
|
|
32
71
|
const SettingsManager = NativeModules?.SettingsManager;
|
|
33
72
|
if (SettingsManager?.settings?.AppleLanguages) {
|
|
34
73
|
const appleLanguages = SettingsManager.settings.AppleLanguages;
|
|
74
|
+
console.log("[getSystemLanguage] iOS AppleLanguages:", appleLanguages);
|
|
35
75
|
for (const lang of appleLanguages) {
|
|
36
76
|
const languageCode = lang.split("-")[0]?.toLowerCase();
|
|
37
77
|
if (languageCode) {
|
|
38
78
|
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
39
79
|
if (supportedLanguage) {
|
|
80
|
+
console.log("[getSystemLanguage] \u2705 Found via iOS SettingsManager:", supportedLanguage);
|
|
40
81
|
return supportedLanguage;
|
|
41
82
|
}
|
|
42
83
|
}
|
|
@@ -66,39 +107,20 @@ function getSystemLanguage(availableLocales) {
|
|
|
66
107
|
}
|
|
67
108
|
} catch (rnError) {
|
|
68
109
|
}
|
|
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
110
|
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
111
|
+
console.log("[getSystemLanguage] Intl locale:", systemLocale);
|
|
93
112
|
if (systemLocale) {
|
|
94
113
|
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
|
95
114
|
if (languageCode) {
|
|
96
115
|
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
97
116
|
if (supportedLanguage) {
|
|
117
|
+
console.log("[getSystemLanguage] \u2705 Found via Intl API:", supportedLanguage);
|
|
98
118
|
return supportedLanguage;
|
|
99
119
|
}
|
|
100
120
|
}
|
|
101
121
|
}
|
|
122
|
+
console.log("[getSystemLanguage] \u274C No matching locale found, returning null");
|
|
123
|
+
console.log("[getSystemLanguage] === END ===");
|
|
102
124
|
return null;
|
|
103
125
|
} catch (error) {
|
|
104
126
|
console.log("Could not detect system language:", error);
|
|
@@ -115,6 +137,8 @@ function LText({ children, assetKey, ...props }) {
|
|
|
115
137
|
return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { ...props, children: l(assetKey || children) });
|
|
116
138
|
}
|
|
117
139
|
var LText_default = LText;
|
|
140
|
+
console.log("\u{1F680}\u{1F680}\u{1F680} [LOCON] LOCAL VERSION LOADED! \u{1F680}\u{1F680}\u{1F680}");
|
|
141
|
+
console.log("[LOCON] This log means we are using the local source from ../../../locon/locon/src/locon");
|
|
118
142
|
var LoconContext = react.createContext({
|
|
119
143
|
assets: {},
|
|
120
144
|
currentLocale: "en",
|
package/dist/index.mjs
CHANGED
|
@@ -12,14 +12,53 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
12
12
|
// src/locon/utils/getSystemLanguage.ts
|
|
13
13
|
function getSystemLanguage(availableLocales) {
|
|
14
14
|
if (!availableLocales?.length) return null;
|
|
15
|
+
console.log("[getSystemLanguage] === START ===");
|
|
16
|
+
console.log("[getSystemLanguage] Available locales:", availableLocales);
|
|
15
17
|
try {
|
|
16
18
|
try {
|
|
17
19
|
const RNLocalize = __require("react-native-localize");
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
if (RNLocalize) {
|
|
21
|
+
if (typeof RNLocalize.findBestLanguageTag === "function") {
|
|
22
|
+
const best = RNLocalize.findBestLanguageTag(availableLocales);
|
|
23
|
+
if (best?.languageTag) {
|
|
24
|
+
const languageTag = best.languageTag;
|
|
25
|
+
const languageCode = languageTag.split("-")[0]?.toLowerCase();
|
|
26
|
+
if (availableLocales.includes(languageTag)) {
|
|
27
|
+
return languageTag;
|
|
28
|
+
}
|
|
29
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
30
|
+
console.log("[getSystemLanguage] \u2705 Found via findBestLanguageTag:", languageCode);
|
|
31
|
+
return languageCode;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (typeof RNLocalize.getLocales === "function") {
|
|
36
|
+
const locales = RNLocalize.getLocales();
|
|
37
|
+
console.log("[getSystemLanguage] System locales from getLocales():", JSON.stringify(locales, null, 2));
|
|
38
|
+
for (const locale of locales) {
|
|
39
|
+
const languageCode = locale.languageCode?.toLowerCase();
|
|
40
|
+
console.log("[getSystemLanguage] Checking locale:", locale.languageCode, "->", languageCode);
|
|
41
|
+
if (languageCode && availableLocales.includes(languageCode)) {
|
|
42
|
+
console.log("[getSystemLanguage] \u2705 Found via getLocales languageCode:", languageCode);
|
|
43
|
+
return languageCode;
|
|
44
|
+
}
|
|
45
|
+
if (locale.languageTag) {
|
|
46
|
+
const tagCode = locale.languageTag.split("-")[0]?.toLowerCase();
|
|
47
|
+
console.log("[getSystemLanguage] Checking languageTag:", locale.languageTag, "->", tagCode);
|
|
48
|
+
if (tagCode && availableLocales.includes(tagCode)) {
|
|
49
|
+
console.log("[getSystemLanguage] \u2705 Found via getLocales languageTag:", tagCode);
|
|
50
|
+
return tagCode;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
console.log("[getSystemLanguage] \u26A0\uFE0F getLocales() is not available");
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
console.log("[getSystemLanguage] \u26A0\uFE0F react-native-localize module not found or not linked");
|
|
21
59
|
}
|
|
22
60
|
} catch (localizeError) {
|
|
61
|
+
console.log("[getSystemLanguage] \u274C react-native-localize error:", localizeError);
|
|
23
62
|
}
|
|
24
63
|
try {
|
|
25
64
|
const reactNative = __require("react-native");
|
|
@@ -28,11 +67,13 @@ function getSystemLanguage(availableLocales) {
|
|
|
28
67
|
const SettingsManager = NativeModules?.SettingsManager;
|
|
29
68
|
if (SettingsManager?.settings?.AppleLanguages) {
|
|
30
69
|
const appleLanguages = SettingsManager.settings.AppleLanguages;
|
|
70
|
+
console.log("[getSystemLanguage] iOS AppleLanguages:", appleLanguages);
|
|
31
71
|
for (const lang of appleLanguages) {
|
|
32
72
|
const languageCode = lang.split("-")[0]?.toLowerCase();
|
|
33
73
|
if (languageCode) {
|
|
34
74
|
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
35
75
|
if (supportedLanguage) {
|
|
76
|
+
console.log("[getSystemLanguage] \u2705 Found via iOS SettingsManager:", supportedLanguage);
|
|
36
77
|
return supportedLanguage;
|
|
37
78
|
}
|
|
38
79
|
}
|
|
@@ -62,39 +103,20 @@ function getSystemLanguage(availableLocales) {
|
|
|
62
103
|
}
|
|
63
104
|
} catch (rnError) {
|
|
64
105
|
}
|
|
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
106
|
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
107
|
+
console.log("[getSystemLanguage] Intl locale:", systemLocale);
|
|
89
108
|
if (systemLocale) {
|
|
90
109
|
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
|
91
110
|
if (languageCode) {
|
|
92
111
|
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
93
112
|
if (supportedLanguage) {
|
|
113
|
+
console.log("[getSystemLanguage] \u2705 Found via Intl API:", supportedLanguage);
|
|
94
114
|
return supportedLanguage;
|
|
95
115
|
}
|
|
96
116
|
}
|
|
97
117
|
}
|
|
118
|
+
console.log("[getSystemLanguage] \u274C No matching locale found, returning null");
|
|
119
|
+
console.log("[getSystemLanguage] === END ===");
|
|
98
120
|
return null;
|
|
99
121
|
} catch (error) {
|
|
100
122
|
console.log("Could not detect system language:", error);
|
|
@@ -111,6 +133,8 @@ function LText({ children, assetKey, ...props }) {
|
|
|
111
133
|
return /* @__PURE__ */ jsx(Text, { ...props, children: l(assetKey || children) });
|
|
112
134
|
}
|
|
113
135
|
var LText_default = LText;
|
|
136
|
+
console.log("\u{1F680}\u{1F680}\u{1F680} [LOCON] LOCAL VERSION LOADED! \u{1F680}\u{1F680}\u{1F680}");
|
|
137
|
+
console.log("[LOCON] This log means we are using the local source from ../../../locon/locon/src/locon");
|
|
114
138
|
var LoconContext = createContext({
|
|
115
139
|
assets: {},
|
|
116
140
|
currentLocale: "en",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "locon",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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",
|