locon 1.1.0 → 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 CHANGED
@@ -4,6 +4,28 @@
4
4
  It provides a context provider, a hook, and a simple text component to keep
5
5
  your translations flat, explicit, and easy to use.
6
6
 
7
+ Basic usage: use `<LText>` instead of `<Text>`. Put plain text in your project language (or the default language) inside and it will be localized automatically.
8
+
9
+ ```tsx
10
+ import { LText } from 'locon'
11
+
12
+ function Example() {
13
+ return <LText>Hello</LText>
14
+ }
15
+ ```
16
+
17
+ For dynamic strings (variables), use `useLocon()` and `l()`:
18
+
19
+ ```tsx
20
+ import { Text } from 'react-native'
21
+ import { useLocon } from 'locon'
22
+
23
+ function Example({ userName }: { userName: string }) {
24
+ const { l } = useLocon()
25
+ return <Text>{l('hello')}, {userName}</Text>
26
+ }
27
+ ```
28
+
7
29
  ---
8
30
 
9
31
  ### Features
@@ -25,19 +47,29 @@ npm install locon
25
47
  yarn add locon
26
48
  ```
27
49
 
28
- **Important:** After installation, run pod install for iOS:
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`:
29
59
 
30
60
  ```bash
31
- cd ios && pod install
61
+ npm install react-native-localize
62
+ # or
63
+ yarn add react-native-localize
32
64
  ```
33
65
 
34
- **Peer dependencies**
66
+ Then run pod install for iOS:
35
67
 
36
- - `react >= 19.0.0`
37
- - `react-native >= 0.70.0`
38
- - `react-native-localize >= 3.0.0` (installed automatically)
68
+ ```bash
69
+ cd ios && pod install
70
+ ```
39
71
 
40
- `react-native-localize` is used for accurate device language detection on iOS and Android.
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.
41
73
 
42
74
  ---
43
75
 
@@ -88,9 +120,7 @@ function App() {
88
120
  return (
89
121
  <Locon
90
122
  assets={{ en, de }}
91
- defaultLocale='en'
92
123
  projectLocale='de'
93
- autodetect
94
124
  >
95
125
  <NavigationContainer theme={theme}>
96
126
  <RootNavigator />
@@ -105,10 +135,13 @@ export default App
105
135
  If `currentLocale` is not provided and `autodetect` is `true` (default),
106
136
  `locon` will:
107
137
 
108
- 1. Use `react-native-localize` to detect device locale on iOS/Android
109
- 2. Match the detected locale (including region) or just language code against your `assets`
110
- 3. If a match is found, use it as initial locale
111
- 4. Otherwise, fall back to `defaultLocale`
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`
112
145
 
113
146
  #### 3. Use the `useLocon()` hook
114
147
 
@@ -154,7 +187,7 @@ function Welcome() {
154
187
  return (
155
188
  <View>
156
189
  {/* Uses children as value to find the key, with fallback children */}
157
- <LText>Hello</LText>
190
+ <LText>Hallo</LText>
158
191
 
159
192
  {/* Or explicit key with fallback children */}
160
193
  <LText assetKey='welcome_title'>Welcome</LText>
@@ -189,6 +222,7 @@ interface LoconProps extends PropsWithChildren {
189
222
  Used when key is missing in the current locale.
190
223
 
191
224
  - **`projectLocale`** (default `'en'`)
225
+ Define language you use for texts in your project.
192
226
  Used for reverse lookup by value (e.g. when you only know the string in
193
227
  project language).
194
228
 
package/dist/index.js CHANGED
@@ -3,37 +3,84 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var react = require('react');
6
- var RNLocalize = require('react-native-localize');
7
6
  var reactNative = require('react-native');
8
7
  var jsxRuntime = require('react/jsx-runtime');
9
8
 
10
- function _interopNamespace(e) {
11
- if (e && e.__esModule) return e;
12
- var n = Object.create(null);
13
- if (e) {
14
- Object.keys(e).forEach(function (k) {
15
- if (k !== 'default') {
16
- var d = Object.getOwnPropertyDescriptor(e, k);
17
- Object.defineProperty(n, k, d.get ? d : {
18
- enumerable: true,
19
- get: function () { return e[k]; }
20
- });
21
- }
22
- });
23
- }
24
- n.default = e;
25
- return Object.freeze(n);
26
- }
27
-
28
- var RNLocalize__namespace = /*#__PURE__*/_interopNamespace(RNLocalize);
9
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
+ }) : x)(function(x) {
12
+ if (typeof require !== "undefined") return require.apply(this, arguments);
13
+ throw Error('Dynamic require of "' + x + '" is not supported');
14
+ });
29
15
 
30
- // src/locon/index.tsx
16
+ // src/locon/utils/getSystemLanguage.ts
31
17
  function getSystemLanguage(availableLocales) {
32
18
  if (!availableLocales?.length) return null;
33
19
  try {
34
- const best = RNLocalize__namespace.findBestLanguageTag(availableLocales);
35
- return best?.languageTag ?? null;
36
- } catch {
20
+ try {
21
+ const RNLocalize = __require("react-native-localize");
22
+ if (RNLocalize && typeof RNLocalize.findBestLanguageTag === "function") {
23
+ const best = RNLocalize.findBestLanguageTag(availableLocales);
24
+ if (best?.languageTag) {
25
+ return best.languageTag;
26
+ }
27
+ }
28
+ } catch (localizeError) {
29
+ }
30
+ try {
31
+ const reactNative = __require("react-native");
32
+ const { NativeModules, Platform } = reactNative;
33
+ if (Platform?.OS === "ios") {
34
+ const SettingsManager = NativeModules?.SettingsManager;
35
+ if (SettingsManager?.settings?.AppleLanguages) {
36
+ const appleLanguages = SettingsManager.settings.AppleLanguages;
37
+ for (const lang of appleLanguages) {
38
+ const languageCode = lang.split("-")[0]?.toLowerCase();
39
+ if (languageCode) {
40
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
41
+ if (supportedLanguage) {
42
+ return supportedLanguage;
43
+ }
44
+ }
45
+ }
46
+ }
47
+ const I18nManager = NativeModules?.I18nManager;
48
+ if (I18nManager?.localeIdentifier) {
49
+ const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
50
+ if (languageCode) {
51
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
52
+ if (supportedLanguage) {
53
+ return supportedLanguage;
54
+ }
55
+ }
56
+ }
57
+ } else if (Platform?.OS === "android") {
58
+ const I18nManager = NativeModules?.I18nManager;
59
+ if (I18nManager?.localeIdentifier) {
60
+ const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
61
+ if (languageCode) {
62
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
63
+ if (supportedLanguage) {
64
+ return supportedLanguage;
65
+ }
66
+ }
67
+ }
68
+ }
69
+ } catch (rnError) {
70
+ }
71
+ const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
72
+ if (systemLocale) {
73
+ const languageCode = systemLocale.split("-")[0]?.toLowerCase();
74
+ if (languageCode) {
75
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
76
+ if (supportedLanguage) {
77
+ return supportedLanguage;
78
+ }
79
+ }
80
+ }
81
+ return null;
82
+ } catch (error) {
83
+ console.log("Could not detect system language:", error);
37
84
  return null;
38
85
  }
39
86
  }
package/dist/index.mjs CHANGED
@@ -1,15 +1,82 @@
1
1
  import { createContext, useContext, useState, useCallback, useMemo } from 'react';
2
- import * as RNLocalize from 'react-native-localize';
3
2
  import { Text } from 'react-native';
4
3
  import { jsx } from 'react/jsx-runtime';
5
4
 
6
- // src/locon/index.tsx
5
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
6
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
7
+ }) : x)(function(x) {
8
+ if (typeof require !== "undefined") return require.apply(this, arguments);
9
+ throw Error('Dynamic require of "' + x + '" is not supported');
10
+ });
11
+
12
+ // src/locon/utils/getSystemLanguage.ts
7
13
  function getSystemLanguage(availableLocales) {
8
14
  if (!availableLocales?.length) return null;
9
15
  try {
10
- const best = RNLocalize.findBestLanguageTag(availableLocales);
11
- return best?.languageTag ?? null;
12
- } catch {
16
+ try {
17
+ const RNLocalize = __require("react-native-localize");
18
+ if (RNLocalize && typeof RNLocalize.findBestLanguageTag === "function") {
19
+ const best = RNLocalize.findBestLanguageTag(availableLocales);
20
+ if (best?.languageTag) {
21
+ return best.languageTag;
22
+ }
23
+ }
24
+ } catch (localizeError) {
25
+ }
26
+ try {
27
+ const reactNative = __require("react-native");
28
+ const { NativeModules, Platform } = reactNative;
29
+ if (Platform?.OS === "ios") {
30
+ const SettingsManager = NativeModules?.SettingsManager;
31
+ if (SettingsManager?.settings?.AppleLanguages) {
32
+ const appleLanguages = SettingsManager.settings.AppleLanguages;
33
+ for (const lang of appleLanguages) {
34
+ const languageCode = lang.split("-")[0]?.toLowerCase();
35
+ if (languageCode) {
36
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
37
+ if (supportedLanguage) {
38
+ return supportedLanguage;
39
+ }
40
+ }
41
+ }
42
+ }
43
+ const I18nManager = NativeModules?.I18nManager;
44
+ if (I18nManager?.localeIdentifier) {
45
+ const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
46
+ if (languageCode) {
47
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
48
+ if (supportedLanguage) {
49
+ return supportedLanguage;
50
+ }
51
+ }
52
+ }
53
+ } else if (Platform?.OS === "android") {
54
+ const I18nManager = NativeModules?.I18nManager;
55
+ if (I18nManager?.localeIdentifier) {
56
+ const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
57
+ if (languageCode) {
58
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
59
+ if (supportedLanguage) {
60
+ return supportedLanguage;
61
+ }
62
+ }
63
+ }
64
+ }
65
+ } catch (rnError) {
66
+ }
67
+ const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
68
+ if (systemLocale) {
69
+ const languageCode = systemLocale.split("-")[0]?.toLowerCase();
70
+ if (languageCode) {
71
+ const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
72
+ if (supportedLanguage) {
73
+ return supportedLanguage;
74
+ }
75
+ }
76
+ }
77
+ return null;
78
+ } catch (error) {
79
+ console.log("Could not detect system language:", error);
13
80
  return null;
14
81
  }
15
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locon",
3
- "version": "1.1.0",
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",
@@ -31,16 +31,13 @@
31
31
  "peerDependencies": {
32
32
  "react": ">=19.0.0",
33
33
  "react-native": ">=0.70.0",
34
- "react-native-localize": ">=3.0.0"
34
+ "react-native-localize": "^3.0.0"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "react-native-localize": {
38
38
  "optional": true
39
39
  }
40
40
  },
41
- "dependencies": {
42
- "react-native-localize": "^3.2.1"
43
- },
44
41
  "devDependencies": {
45
42
  "@babel/preset-react": "^7.24.7",
46
43
  "@eslint/js": "^9.10.0",