locon 1.0.3 → 1.1.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 +65 -14
- package/dist/index.js +25 -10
- package/dist/index.mjs +5 -10
- package/package.json +11 -2
package/README.md
CHANGED
|
@@ -25,10 +25,19 @@ npm install locon
|
|
|
25
25
|
yarn add locon
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
+
**Important:** After installation, run pod install for iOS:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
cd ios && pod install
|
|
32
|
+
```
|
|
33
|
+
|
|
28
34
|
**Peer dependencies**
|
|
29
35
|
|
|
30
36
|
- `react >= 19.0.0`
|
|
31
37
|
- `react-native >= 0.70.0`
|
|
38
|
+
- `react-native-localize >= 3.0.0` (installed automatically)
|
|
39
|
+
|
|
40
|
+
`react-native-localize` is used for accurate device language detection on iOS and Android.
|
|
32
41
|
|
|
33
42
|
---
|
|
34
43
|
|
|
@@ -80,7 +89,7 @@ function App() {
|
|
|
80
89
|
<Locon
|
|
81
90
|
assets={{ en, de }}
|
|
82
91
|
defaultLocale='en'
|
|
83
|
-
projectLocale='
|
|
92
|
+
projectLocale='de'
|
|
84
93
|
autodetect
|
|
85
94
|
>
|
|
86
95
|
<NavigationContainer theme={theme}>
|
|
@@ -96,9 +105,9 @@ export default App
|
|
|
96
105
|
If `currentLocale` is not provided and `autodetect` is `true` (default),
|
|
97
106
|
`locon` will:
|
|
98
107
|
|
|
99
|
-
1.
|
|
100
|
-
2.
|
|
101
|
-
3. If
|
|
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
|
|
102
111
|
4. Otherwise, fall back to `defaultLocale`
|
|
103
112
|
|
|
104
113
|
#### 3. Use the `useLocon()` hook
|
|
@@ -113,12 +122,22 @@ function LanguageSwitcher() {
|
|
|
113
122
|
|
|
114
123
|
return (
|
|
115
124
|
<View>
|
|
125
|
+
{/* Localised value by key */}
|
|
116
126
|
<Text>
|
|
117
127
|
{l('current_language')}: {currentLocale}
|
|
118
128
|
</Text>
|
|
119
|
-
|
|
120
|
-
<
|
|
121
|
-
|
|
129
|
+
{/* or by value in project- or default language */}
|
|
130
|
+
<Text>
|
|
131
|
+
{l('Current language')}: {currentLocale}
|
|
132
|
+
</Text>
|
|
133
|
+
<Button
|
|
134
|
+
title='English'
|
|
135
|
+
onPress={() => setLocale('en')}
|
|
136
|
+
/>
|
|
137
|
+
<Button
|
|
138
|
+
title='Deutsch'
|
|
139
|
+
onPress={() => setLocale('de')}
|
|
140
|
+
/>
|
|
122
141
|
</View>
|
|
123
142
|
)
|
|
124
143
|
}
|
|
@@ -134,8 +153,8 @@ import { LText } from 'locon'
|
|
|
134
153
|
function Welcome() {
|
|
135
154
|
return (
|
|
136
155
|
<View>
|
|
137
|
-
{/* Uses children as key */}
|
|
138
|
-
<LText>
|
|
156
|
+
{/* Uses children as value to find the key, with fallback children */}
|
|
157
|
+
<LText>Hello</LText>
|
|
139
158
|
|
|
140
159
|
{/* Or explicit key with fallback children */}
|
|
141
160
|
<LText assetKey='welcome_title'>Welcome</LText>
|
|
@@ -182,12 +201,12 @@ interface LoconProps extends PropsWithChildren {
|
|
|
182
201
|
import { useLocon } from 'locon'
|
|
183
202
|
|
|
184
203
|
const {
|
|
185
|
-
l,
|
|
186
|
-
assets,
|
|
204
|
+
l, // (key/value: string) => string
|
|
205
|
+
assets, // all assets map
|
|
187
206
|
currentLocale, // current language code
|
|
188
207
|
defaultLocale, // default language code
|
|
189
|
-
autodetect,
|
|
190
|
-
setLocale,
|
|
208
|
+
autodetect, // boolean
|
|
209
|
+
setLocale, // (locale: string) => void
|
|
191
210
|
} = useLocon()
|
|
192
211
|
```
|
|
193
212
|
|
|
@@ -219,6 +238,39 @@ interface LTextProps extends ComponentProps<typeof Text> {
|
|
|
219
238
|
|
|
220
239
|
---
|
|
221
240
|
|
|
241
|
+
### ESLint integration (`react-native/no-raw-text`)
|
|
242
|
+
|
|
243
|
+
If you use `eslint-plugin-react-native` with the `react-native/no-raw-text`
|
|
244
|
+
rule enabled, you may see errors like:
|
|
245
|
+
|
|
246
|
+
> Raw text (Hello) cannot be used outside of a `<Text>` tag
|
|
247
|
+
|
|
248
|
+
because the rule does not know that `LText` ultimately renders a React
|
|
249
|
+
Native `<Text>` under the hood.
|
|
250
|
+
|
|
251
|
+
To fix this, tell the rule to treat `LText` as an allowed wrapper:
|
|
252
|
+
|
|
253
|
+
```js
|
|
254
|
+
// .eslintrc.js
|
|
255
|
+
module.exports = {
|
|
256
|
+
// ...
|
|
257
|
+
rules: {
|
|
258
|
+
// other rules...
|
|
259
|
+
'react-native/no-raw-text': [
|
|
260
|
+
'error',
|
|
261
|
+
{
|
|
262
|
+
skip: ['LText'],
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
},
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
This keeps the rule active for other components, but lets you freely use
|
|
270
|
+
raw text inside `<LText>`.
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
222
274
|
### TypeScript
|
|
223
275
|
|
|
224
276
|
The published package contains:
|
|
@@ -235,4 +287,3 @@ support for `Locon`, `useLocon`, and `LText`.
|
|
|
235
287
|
### License
|
|
236
288
|
|
|
237
289
|
`locon` is released under the **ISC** license.
|
|
238
|
-
|
package/dist/index.js
CHANGED
|
@@ -3,22 +3,37 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var react = require('react');
|
|
6
|
+
var RNLocalize = require('react-native-localize');
|
|
6
7
|
var reactNative = require('react-native');
|
|
7
8
|
var jsxRuntime = require('react/jsx-runtime');
|
|
8
9
|
|
|
9
|
-
|
|
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);
|
|
10
29
|
|
|
11
|
-
// src/locon/
|
|
30
|
+
// src/locon/index.tsx
|
|
12
31
|
function getSystemLanguage(availableLocales) {
|
|
32
|
+
if (!availableLocales?.length) return null;
|
|
13
33
|
try {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (!languageCode) return null;
|
|
18
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
19
|
-
return supportedLanguage ? languageCode : null;
|
|
20
|
-
} catch (error) {
|
|
21
|
-
console.log("Could not detect system language:", error);
|
|
34
|
+
const best = RNLocalize__namespace.findBestLanguageTag(availableLocales);
|
|
35
|
+
return best?.languageTag ?? null;
|
|
36
|
+
} catch {
|
|
22
37
|
return null;
|
|
23
38
|
}
|
|
24
39
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
import { createContext, useContext, useState, useCallback, useMemo } from 'react';
|
|
2
|
+
import * as RNLocalize from 'react-native-localize';
|
|
2
3
|
import { Text } from 'react-native';
|
|
3
4
|
import { jsx } from 'react/jsx-runtime';
|
|
4
5
|
|
|
5
6
|
// src/locon/index.tsx
|
|
6
|
-
|
|
7
|
-
// src/locon/utils/getSystemLanguage.ts
|
|
8
7
|
function getSystemLanguage(availableLocales) {
|
|
8
|
+
if (!availableLocales?.length) return null;
|
|
9
9
|
try {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!languageCode) return null;
|
|
14
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
15
|
-
return supportedLanguage ? languageCode : null;
|
|
16
|
-
} catch (error) {
|
|
17
|
-
console.log("Could not detect system language:", error);
|
|
10
|
+
const best = RNLocalize.findBestLanguageTag(availableLocales);
|
|
11
|
+
return best?.languageTag ?? null;
|
|
12
|
+
} catch {
|
|
18
13
|
return null;
|
|
19
14
|
}
|
|
20
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "locon",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.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,7 +30,16 @@
|
|
|
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"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"react-native-localize": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"react-native-localize": "^3.2.1"
|
|
34
43
|
},
|
|
35
44
|
"devDependencies": {
|
|
36
45
|
"@babel/preset-react": "^7.24.7",
|