locon 1.2.0 → 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 CHANGED
@@ -47,29 +47,19 @@ npm install locon
47
47
  yarn add locon
48
48
  ```
49
49
 
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`:
50
+ **Important:** After installation, run pod install for iOS:
59
51
 
60
52
  ```bash
61
- npm install react-native-localize
62
- # or
63
- yarn add react-native-localize
53
+ cd ios && pod install
64
54
  ```
65
55
 
66
- Then run pod install for iOS:
56
+ **Peer dependencies**
67
57
 
68
- ```bash
69
- cd ios && pod install
70
- ```
58
+ - `react >= 19.0.0`
59
+ - `react-native >= 0.70.0`
60
+ - `react-native-localize >= 3.0.0` (installed automatically)
71
61
 
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.
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. 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`
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
@@ -16,74 +16,65 @@ 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);
21
19
  try {
22
20
  try {
23
21
  const RNLocalize = __require("react-native-localize");
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
- }
22
+ if (typeof RNLocalize.findBestLanguageTag === "function") {
23
+ const best = RNLocalize.findBestLanguageTag(availableLocales);
24
+ if (best?.languageTag) {
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;
37
32
  }
38
33
  }
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;
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;
48
46
  }
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
- }
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;
56
59
  }
57
60
  }
58
- } else {
59
- console.log("[getSystemLanguage] \u26A0\uFE0F getLocales() is not available");
60
61
  }
61
- } else {
62
- console.log("[getSystemLanguage] \u26A0\uFE0F react-native-localize module not found or not linked");
63
62
  }
64
63
  } catch (localizeError) {
65
- console.log("[getSystemLanguage] \u274C react-native-localize error:", localizeError);
64
+ console.warn("[locon] \u274C react-native-localize error:", localizeError);
66
65
  }
67
66
  try {
68
- const reactNative = __require("react-native");
69
- const { NativeModules, Platform } = reactNative;
70
- if (Platform?.OS === "ios") {
71
- const SettingsManager = NativeModules?.SettingsManager;
67
+ const { NativeModules, Platform } = __require("react-native");
68
+ if (Platform.OS === "ios") {
69
+ const { SettingsManager } = NativeModules;
72
70
  if (SettingsManager?.settings?.AppleLanguages) {
73
71
  const appleLanguages = SettingsManager.settings.AppleLanguages;
74
- console.log("[getSystemLanguage] iOS AppleLanguages:", appleLanguages);
75
- for (const lang of appleLanguages) {
76
- const languageCode = lang.split("-")[0]?.toLowerCase();
77
- if (languageCode) {
78
- const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
79
- if (supportedLanguage) {
80
- console.log("[getSystemLanguage] \u2705 Found via iOS SettingsManager:", supportedLanguage);
81
- return supportedLanguage;
82
- }
83
- }
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;
84
75
  }
85
76
  }
86
- const I18nManager = NativeModules?.I18nManager;
77
+ const { I18nManager } = NativeModules;
87
78
  if (I18nManager?.localeIdentifier) {
88
79
  const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
89
80
  if (languageCode) {
@@ -93,8 +84,8 @@ function getSystemLanguage(availableLocales) {
93
84
  }
94
85
  }
95
86
  }
96
- } else if (Platform?.OS === "android") {
97
- const I18nManager = NativeModules?.I18nManager;
87
+ } else if (Platform.OS === "android") {
88
+ const { I18nManager } = NativeModules;
98
89
  if (I18nManager?.localeIdentifier) {
99
90
  const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
100
91
  if (languageCode) {
@@ -106,27 +97,25 @@ function getSystemLanguage(availableLocales) {
106
97
  }
107
98
  }
108
99
  } catch (rnError) {
100
+ console.warn("[locon] \u274C react-native error:", rnError);
109
101
  }
110
102
  const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
111
- console.log("[getSystemLanguage] Intl locale:", systemLocale);
112
103
  if (systemLocale) {
113
104
  const languageCode = systemLocale.split("-")[0]?.toLowerCase();
114
105
  if (languageCode) {
115
106
  const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
116
107
  if (supportedLanguage) {
117
- console.log("[getSystemLanguage] \u2705 Found via Intl API:", supportedLanguage);
118
108
  return supportedLanguage;
119
109
  }
120
110
  }
121
111
  }
122
- console.log("[getSystemLanguage] \u274C No matching locale found, returning null");
123
- console.log("[getSystemLanguage] === END ===");
124
112
  return null;
125
113
  } catch (error) {
126
- console.log("Could not detect system language:", error);
114
+ console.warn("[locon] \u274C Could not detect system language:", error);
127
115
  return null;
128
116
  }
129
117
  }
118
+ var getSystemLanguage_default = getSystemLanguage;
130
119
  function useLocon() {
131
120
  const locon = react.useContext(LoconContext);
132
121
  return locon;
@@ -137,8 +126,6 @@ function LText({ children, assetKey, ...props }) {
137
126
  return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { ...props, children: l(assetKey || children) });
138
127
  }
139
128
  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");
142
129
  var LoconContext = react.createContext({
143
130
  assets: {},
144
131
  currentLocale: "en",
@@ -163,10 +150,9 @@ function Locon({
163
150
  return currentLocaleProps;
164
151
  }
165
152
  if (autodetect) {
166
- const availableLocales = Object.keys(assets);
167
- const systemLang = getSystemLanguage(availableLocales);
168
- if (systemLang && assets[systemLang]) {
169
- return systemLang;
153
+ const systemLocale = getSystemLanguage_default(Object.keys(assets));
154
+ if (systemLocale && assets[systemLocale]) {
155
+ return systemLocale;
170
156
  }
171
157
  }
172
158
  return defaultLocale;
package/dist/index.mjs CHANGED
@@ -12,74 +12,65 @@ 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);
17
15
  try {
18
16
  try {
19
17
  const RNLocalize = __require("react-native-localize");
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
- }
18
+ if (typeof RNLocalize.findBestLanguageTag === "function") {
19
+ const best = RNLocalize.findBestLanguageTag(availableLocales);
20
+ if (best?.languageTag) {
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;
33
28
  }
34
29
  }
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;
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;
44
42
  }
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
- }
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;
52
55
  }
53
56
  }
54
- } else {
55
- console.log("[getSystemLanguage] \u26A0\uFE0F getLocales() is not available");
56
57
  }
57
- } else {
58
- console.log("[getSystemLanguage] \u26A0\uFE0F react-native-localize module not found or not linked");
59
58
  }
60
59
  } catch (localizeError) {
61
- console.log("[getSystemLanguage] \u274C react-native-localize error:", localizeError);
60
+ console.warn("[locon] \u274C react-native-localize error:", localizeError);
62
61
  }
63
62
  try {
64
- const reactNative = __require("react-native");
65
- const { NativeModules, Platform } = reactNative;
66
- if (Platform?.OS === "ios") {
67
- const SettingsManager = NativeModules?.SettingsManager;
63
+ const { NativeModules, Platform } = __require("react-native");
64
+ if (Platform.OS === "ios") {
65
+ const { SettingsManager } = NativeModules;
68
66
  if (SettingsManager?.settings?.AppleLanguages) {
69
67
  const appleLanguages = SettingsManager.settings.AppleLanguages;
70
- console.log("[getSystemLanguage] iOS AppleLanguages:", appleLanguages);
71
- for (const lang of appleLanguages) {
72
- const languageCode = lang.split("-")[0]?.toLowerCase();
73
- if (languageCode) {
74
- const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
75
- if (supportedLanguage) {
76
- console.log("[getSystemLanguage] \u2705 Found via iOS SettingsManager:", supportedLanguage);
77
- return supportedLanguage;
78
- }
79
- }
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;
80
71
  }
81
72
  }
82
- const I18nManager = NativeModules?.I18nManager;
73
+ const { I18nManager } = NativeModules;
83
74
  if (I18nManager?.localeIdentifier) {
84
75
  const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
85
76
  if (languageCode) {
@@ -89,8 +80,8 @@ function getSystemLanguage(availableLocales) {
89
80
  }
90
81
  }
91
82
  }
92
- } else if (Platform?.OS === "android") {
93
- const I18nManager = NativeModules?.I18nManager;
83
+ } else if (Platform.OS === "android") {
84
+ const { I18nManager } = NativeModules;
94
85
  if (I18nManager?.localeIdentifier) {
95
86
  const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
96
87
  if (languageCode) {
@@ -102,27 +93,25 @@ function getSystemLanguage(availableLocales) {
102
93
  }
103
94
  }
104
95
  } catch (rnError) {
96
+ console.warn("[locon] \u274C react-native error:", rnError);
105
97
  }
106
98
  const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
107
- console.log("[getSystemLanguage] Intl locale:", systemLocale);
108
99
  if (systemLocale) {
109
100
  const languageCode = systemLocale.split("-")[0]?.toLowerCase();
110
101
  if (languageCode) {
111
102
  const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
112
103
  if (supportedLanguage) {
113
- console.log("[getSystemLanguage] \u2705 Found via Intl API:", supportedLanguage);
114
104
  return supportedLanguage;
115
105
  }
116
106
  }
117
107
  }
118
- console.log("[getSystemLanguage] \u274C No matching locale found, returning null");
119
- console.log("[getSystemLanguage] === END ===");
120
108
  return null;
121
109
  } catch (error) {
122
- console.log("Could not detect system language:", error);
110
+ console.warn("[locon] \u274C Could not detect system language:", error);
123
111
  return null;
124
112
  }
125
113
  }
114
+ var getSystemLanguage_default = getSystemLanguage;
126
115
  function useLocon() {
127
116
  const locon = useContext(LoconContext);
128
117
  return locon;
@@ -133,8 +122,6 @@ function LText({ children, assetKey, ...props }) {
133
122
  return /* @__PURE__ */ jsx(Text, { ...props, children: l(assetKey || children) });
134
123
  }
135
124
  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");
138
125
  var LoconContext = createContext({
139
126
  assets: {},
140
127
  currentLocale: "en",
@@ -159,10 +146,9 @@ function Locon({
159
146
  return currentLocaleProps;
160
147
  }
161
148
  if (autodetect) {
162
- const availableLocales = Object.keys(assets);
163
- const systemLang = getSystemLanguage(availableLocales);
164
- if (systemLang && assets[systemLang]) {
165
- return systemLang;
149
+ const systemLocale = getSystemLanguage_default(Object.keys(assets));
150
+ if (systemLocale && assets[systemLocale]) {
151
+ return systemLocale;
166
152
  }
167
153
  }
168
154
  return defaultLocale;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locon",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
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",