locon 1.0.3 → 1.0.4
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 +53 -11
- package/dist/index.js +25 -10
- package/dist/index.mjs +5 -10
- package/package.json +11 -2
package/README.md
CHANGED
|
@@ -80,7 +80,7 @@ function App() {
|
|
|
80
80
|
<Locon
|
|
81
81
|
assets={{ en, de }}
|
|
82
82
|
defaultLocale='en'
|
|
83
|
-
projectLocale='
|
|
83
|
+
projectLocale='de'
|
|
84
84
|
autodetect
|
|
85
85
|
>
|
|
86
86
|
<NavigationContainer theme={theme}>
|
|
@@ -113,12 +113,22 @@ function LanguageSwitcher() {
|
|
|
113
113
|
|
|
114
114
|
return (
|
|
115
115
|
<View>
|
|
116
|
+
{/* Localised value by key */}
|
|
116
117
|
<Text>
|
|
117
118
|
{l('current_language')}: {currentLocale}
|
|
118
119
|
</Text>
|
|
119
|
-
|
|
120
|
-
<
|
|
121
|
-
|
|
120
|
+
{/* or by value in project- or default language */}
|
|
121
|
+
<Text>
|
|
122
|
+
{l('Current language')}: {currentLocale}
|
|
123
|
+
</Text>
|
|
124
|
+
<Button
|
|
125
|
+
title='English'
|
|
126
|
+
onPress={() => setLocale('en')}
|
|
127
|
+
/>
|
|
128
|
+
<Button
|
|
129
|
+
title='Deutsch'
|
|
130
|
+
onPress={() => setLocale('de')}
|
|
131
|
+
/>
|
|
122
132
|
</View>
|
|
123
133
|
)
|
|
124
134
|
}
|
|
@@ -134,8 +144,8 @@ import { LText } from 'locon'
|
|
|
134
144
|
function Welcome() {
|
|
135
145
|
return (
|
|
136
146
|
<View>
|
|
137
|
-
{/* Uses children as key */}
|
|
138
|
-
<LText>
|
|
147
|
+
{/* Uses children as value to find the key, with fallback children */}
|
|
148
|
+
<LText>Hello</LText>
|
|
139
149
|
|
|
140
150
|
{/* Or explicit key with fallback children */}
|
|
141
151
|
<LText assetKey='welcome_title'>Welcome</LText>
|
|
@@ -182,12 +192,12 @@ interface LoconProps extends PropsWithChildren {
|
|
|
182
192
|
import { useLocon } from 'locon'
|
|
183
193
|
|
|
184
194
|
const {
|
|
185
|
-
l,
|
|
186
|
-
assets,
|
|
195
|
+
l, // (key/value: string) => string
|
|
196
|
+
assets, // all assets map
|
|
187
197
|
currentLocale, // current language code
|
|
188
198
|
defaultLocale, // default language code
|
|
189
|
-
autodetect,
|
|
190
|
-
setLocale,
|
|
199
|
+
autodetect, // boolean
|
|
200
|
+
setLocale, // (locale: string) => void
|
|
191
201
|
} = useLocon()
|
|
192
202
|
```
|
|
193
203
|
|
|
@@ -219,6 +229,39 @@ interface LTextProps extends ComponentProps<typeof Text> {
|
|
|
219
229
|
|
|
220
230
|
---
|
|
221
231
|
|
|
232
|
+
### ESLint integration (`react-native/no-raw-text`)
|
|
233
|
+
|
|
234
|
+
If you use `eslint-plugin-react-native` with the `react-native/no-raw-text`
|
|
235
|
+
rule enabled, you may see errors like:
|
|
236
|
+
|
|
237
|
+
> Raw text (Hello) cannot be used outside of a `<Text>` tag
|
|
238
|
+
|
|
239
|
+
because the rule does not know that `LText` ultimately renders a React
|
|
240
|
+
Native `<Text>` under the hood.
|
|
241
|
+
|
|
242
|
+
To fix this, tell the rule to treat `LText` as an allowed wrapper:
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
// .eslintrc.js
|
|
246
|
+
module.exports = {
|
|
247
|
+
// ...
|
|
248
|
+
rules: {
|
|
249
|
+
// other rules...
|
|
250
|
+
'react-native/no-raw-text': [
|
|
251
|
+
'error',
|
|
252
|
+
{
|
|
253
|
+
skip: ['LText'],
|
|
254
|
+
},
|
|
255
|
+
],
|
|
256
|
+
},
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
This keeps the rule active for other components, but lets you freely use
|
|
261
|
+
raw text inside `<LText>`.
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
222
265
|
### TypeScript
|
|
223
266
|
|
|
224
267
|
The published package contains:
|
|
@@ -235,4 +278,3 @@ support for `Locon`, `useLocon`, and `LText`.
|
|
|
235
278
|
### License
|
|
236
279
|
|
|
237
280
|
`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.0.4",
|
|
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",
|