locon 1.0.0 → 1.0.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/dist/index.js +29 -2
- package/dist/index.mjs +29 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,6 +7,21 @@ var reactNative = require('react-native');
|
|
|
7
7
|
var jsxRuntime = require('react/jsx-runtime');
|
|
8
8
|
|
|
9
9
|
// src/locon/index.tsx
|
|
10
|
+
|
|
11
|
+
// src/locon/utils/getSystemLanguage.ts
|
|
12
|
+
function getSystemLanguage(availableLocales) {
|
|
13
|
+
try {
|
|
14
|
+
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
15
|
+
if (!systemLocale) return null;
|
|
16
|
+
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
|
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);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
10
25
|
function useLocon() {
|
|
11
26
|
const locon = react.useContext(LoconContext);
|
|
12
27
|
return locon;
|
|
@@ -31,12 +46,24 @@ var LoconContext = react.createContext({
|
|
|
31
46
|
function Locon({
|
|
32
47
|
children,
|
|
33
48
|
assets = {},
|
|
34
|
-
currentLocale: currentLocaleProps
|
|
49
|
+
currentLocale: currentLocaleProps,
|
|
35
50
|
defaultLocale = "en",
|
|
36
51
|
projectLocale = "en",
|
|
37
52
|
autodetect = true
|
|
38
53
|
}) {
|
|
39
|
-
const [currentLocale, setCurrentLocale] = react.useState(
|
|
54
|
+
const [currentLocale, setCurrentLocale] = react.useState(() => {
|
|
55
|
+
if (currentLocaleProps) {
|
|
56
|
+
return currentLocaleProps;
|
|
57
|
+
}
|
|
58
|
+
if (autodetect) {
|
|
59
|
+
const availableLocales = Object.keys(assets);
|
|
60
|
+
const systemLang = getSystemLanguage(availableLocales);
|
|
61
|
+
if (systemLang && assets[systemLang]) {
|
|
62
|
+
return systemLang;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return defaultLocale;
|
|
66
|
+
});
|
|
40
67
|
const getAssets = react.useCallback(() => {
|
|
41
68
|
const currentAssets = assets[currentLocale] ?? assets[defaultLocale];
|
|
42
69
|
return currentAssets ?? {};
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,21 @@ import { Text } from 'react-native';
|
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
|
|
5
5
|
// src/locon/index.tsx
|
|
6
|
+
|
|
7
|
+
// src/locon/utils/getSystemLanguage.ts
|
|
8
|
+
function getSystemLanguage(availableLocales) {
|
|
9
|
+
try {
|
|
10
|
+
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
11
|
+
if (!systemLocale) return null;
|
|
12
|
+
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
|
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);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
6
21
|
function useLocon() {
|
|
7
22
|
const locon = useContext(LoconContext);
|
|
8
23
|
return locon;
|
|
@@ -27,12 +42,24 @@ var LoconContext = createContext({
|
|
|
27
42
|
function Locon({
|
|
28
43
|
children,
|
|
29
44
|
assets = {},
|
|
30
|
-
currentLocale: currentLocaleProps
|
|
45
|
+
currentLocale: currentLocaleProps,
|
|
31
46
|
defaultLocale = "en",
|
|
32
47
|
projectLocale = "en",
|
|
33
48
|
autodetect = true
|
|
34
49
|
}) {
|
|
35
|
-
const [currentLocale, setCurrentLocale] = useState(
|
|
50
|
+
const [currentLocale, setCurrentLocale] = useState(() => {
|
|
51
|
+
if (currentLocaleProps) {
|
|
52
|
+
return currentLocaleProps;
|
|
53
|
+
}
|
|
54
|
+
if (autodetect) {
|
|
55
|
+
const availableLocales = Object.keys(assets);
|
|
56
|
+
const systemLang = getSystemLanguage(availableLocales);
|
|
57
|
+
if (systemLang && assets[systemLang]) {
|
|
58
|
+
return systemLang;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return defaultLocale;
|
|
62
|
+
});
|
|
36
63
|
const getAssets = useCallback(() => {
|
|
37
64
|
const currentAssets = assets[currentLocale] ?? assets[defaultLocale];
|
|
38
65
|
return currentAssets ?? {};
|
package/package.json
CHANGED