locon 1.2.1 → 1.3.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 +0 -12
- package/dist/index.js +57 -143
- package/dist/index.mjs +14 -129
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,18 +14,6 @@ function Example() {
|
|
|
14
14
|
}
|
|
15
15
|
```
|
|
16
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
|
-
|
|
29
17
|
---
|
|
30
18
|
|
|
31
19
|
### Features
|
package/dist/index.js
CHANGED
|
@@ -1,132 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function getSystemLanguage(availableLocales) {
|
|
18
|
-
if (!availableLocales?.length) return null;
|
|
19
|
-
try {
|
|
20
|
-
try {
|
|
21
|
-
const RNLocalize = __require("react-native-localize");
|
|
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;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
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;
|
|
46
|
-
}
|
|
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;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
} catch (localizeError) {
|
|
64
|
-
console.warn("[locon] \u274C react-native-localize error:", localizeError);
|
|
65
|
-
}
|
|
66
|
-
try {
|
|
67
|
-
const { NativeModules, Platform } = __require("react-native");
|
|
68
|
-
if (Platform.OS === "ios") {
|
|
69
|
-
const { SettingsManager } = NativeModules;
|
|
70
|
-
if (SettingsManager?.settings?.AppleLanguages) {
|
|
71
|
-
const appleLanguages = SettingsManager.settings.AppleLanguages;
|
|
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;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const { I18nManager } = NativeModules;
|
|
78
|
-
if (I18nManager?.localeIdentifier) {
|
|
79
|
-
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
80
|
-
if (languageCode) {
|
|
81
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
82
|
-
if (supportedLanguage) {
|
|
83
|
-
return supportedLanguage;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
} else if (Platform.OS === "android") {
|
|
88
|
-
const { I18nManager } = NativeModules;
|
|
89
|
-
if (I18nManager?.localeIdentifier) {
|
|
90
|
-
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
91
|
-
if (languageCode) {
|
|
92
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
93
|
-
if (supportedLanguage) {
|
|
94
|
-
return supportedLanguage;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
} catch (rnError) {
|
|
100
|
-
console.warn("[locon] \u274C react-native error:", rnError);
|
|
101
|
-
}
|
|
102
|
-
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
103
|
-
if (systemLocale) {
|
|
104
|
-
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
|
105
|
-
if (languageCode) {
|
|
106
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
107
|
-
if (supportedLanguage) {
|
|
108
|
-
return supportedLanguage;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return null;
|
|
113
|
-
} catch (error) {
|
|
114
|
-
console.warn("[locon] \u274C Could not detect system language:", error);
|
|
115
|
-
return null;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
116
17
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
var
|
|
129
|
-
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var locon_exports = {};
|
|
30
|
+
__export(locon_exports, {
|
|
31
|
+
LText: () => import_LText.default,
|
|
32
|
+
LoconContext: () => LoconContext,
|
|
33
|
+
default: () => locon_default,
|
|
34
|
+
useLocon: () => import_useLocon.default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(locon_exports);
|
|
37
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
38
|
+
var import_react = require("react");
|
|
39
|
+
var import_getSystemLanguage = __toESM(require("./utils/getSystemLanguage"));
|
|
40
|
+
var import_LText = __toESM(require("./components/LText"));
|
|
41
|
+
var import_useLocon = __toESM(require("./hooks/useLocon"));
|
|
42
|
+
const LoconContext = (0, import_react.createContext)({
|
|
130
43
|
assets: {},
|
|
131
44
|
currentLocale: "en",
|
|
132
45
|
defaultLocale: "en",
|
|
@@ -145,40 +58,40 @@ function Locon({
|
|
|
145
58
|
projectLocale = "en",
|
|
146
59
|
autodetect = true
|
|
147
60
|
}) {
|
|
148
|
-
const [currentLocale, setCurrentLocale] =
|
|
61
|
+
const [currentLocale, setCurrentLocale] = (0, import_react.useState)(() => {
|
|
149
62
|
if (currentLocaleProps) {
|
|
150
63
|
return currentLocaleProps;
|
|
151
64
|
}
|
|
152
65
|
if (autodetect) {
|
|
153
|
-
const systemLocale =
|
|
66
|
+
const systemLocale = (0, import_getSystemLanguage.default)(Object.keys(assets));
|
|
154
67
|
if (systemLocale && assets[systemLocale]) {
|
|
155
68
|
return systemLocale;
|
|
156
69
|
}
|
|
157
70
|
}
|
|
158
71
|
return defaultLocale;
|
|
159
72
|
});
|
|
160
|
-
const getAssets =
|
|
73
|
+
const getAssets = (0, import_react.useCallback)(() => {
|
|
161
74
|
const currentAssets = assets[currentLocale] ?? assets[defaultLocale];
|
|
162
75
|
return currentAssets ?? {};
|
|
163
76
|
}, [assets, currentLocale, defaultLocale]);
|
|
164
|
-
const getAsset =
|
|
77
|
+
const getAsset = (0, import_react.useCallback)(
|
|
165
78
|
(assetKey) => {
|
|
166
79
|
const assetsObj = getAssets();
|
|
167
80
|
return assetsObj[assetKey] ?? "";
|
|
168
81
|
},
|
|
169
82
|
[getAssets]
|
|
170
83
|
);
|
|
171
|
-
const setCurrentLocaleHandler =
|
|
84
|
+
const setCurrentLocaleHandler = (0, import_react.useCallback)(
|
|
172
85
|
(locale) => {
|
|
173
86
|
if (assets[locale]) {
|
|
174
87
|
setCurrentLocale(locale);
|
|
175
88
|
} else {
|
|
176
|
-
console.warn(`Locale "${locale}" not found in assets.`);
|
|
89
|
+
console.warn(`[locon] Locale "${locale}" not found in assets.`);
|
|
177
90
|
}
|
|
178
91
|
},
|
|
179
92
|
[assets]
|
|
180
93
|
);
|
|
181
|
-
const getAssetByValue =
|
|
94
|
+
const getAssetByValue = (0, import_react.useCallback)(
|
|
182
95
|
(assetValue, locale = projectLocale) => {
|
|
183
96
|
const currentAssets = assets[locale];
|
|
184
97
|
if (!currentAssets) {
|
|
@@ -189,7 +102,7 @@ function Locon({
|
|
|
189
102
|
},
|
|
190
103
|
[assets, getAsset, projectLocale]
|
|
191
104
|
);
|
|
192
|
-
const l =
|
|
105
|
+
const l = (0, import_react.useCallback)(
|
|
193
106
|
(assetKey) => {
|
|
194
107
|
if (!assetKey) {
|
|
195
108
|
return "";
|
|
@@ -199,7 +112,7 @@ function Locon({
|
|
|
199
112
|
},
|
|
200
113
|
[getAsset, getAssetByValue, defaultLocale]
|
|
201
114
|
);
|
|
202
|
-
const value =
|
|
115
|
+
const value = (0, import_react.useMemo)(
|
|
203
116
|
() => ({
|
|
204
117
|
assets,
|
|
205
118
|
currentLocale,
|
|
@@ -210,11 +123,12 @@ function Locon({
|
|
|
210
123
|
}),
|
|
211
124
|
[assets, currentLocale, defaultLocale, autodetect, l, setCurrentLocaleHandler]
|
|
212
125
|
);
|
|
213
|
-
return /* @__PURE__ */
|
|
126
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoconContext, { value, children });
|
|
214
127
|
}
|
|
215
128
|
var locon_default = Locon;
|
|
216
|
-
|
|
217
|
-
exports
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
129
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
130
|
+
0 && (module.exports = {
|
|
131
|
+
LText,
|
|
132
|
+
LoconContext,
|
|
133
|
+
useLocon
|
|
134
|
+
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,128 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
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
|
|
13
|
-
function getSystemLanguage(availableLocales) {
|
|
14
|
-
if (!availableLocales?.length) return null;
|
|
15
|
-
try {
|
|
16
|
-
try {
|
|
17
|
-
const RNLocalize = __require("react-native-localize");
|
|
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;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
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;
|
|
42
|
-
}
|
|
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;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
} catch (localizeError) {
|
|
60
|
-
console.warn("[locon] \u274C react-native-localize error:", localizeError);
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
const { NativeModules, Platform } = __require("react-native");
|
|
64
|
-
if (Platform.OS === "ios") {
|
|
65
|
-
const { SettingsManager } = NativeModules;
|
|
66
|
-
if (SettingsManager?.settings?.AppleLanguages) {
|
|
67
|
-
const appleLanguages = SettingsManager.settings.AppleLanguages;
|
|
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;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const { I18nManager } = NativeModules;
|
|
74
|
-
if (I18nManager?.localeIdentifier) {
|
|
75
|
-
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
76
|
-
if (languageCode) {
|
|
77
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
78
|
-
if (supportedLanguage) {
|
|
79
|
-
return supportedLanguage;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
} else if (Platform.OS === "android") {
|
|
84
|
-
const { I18nManager } = NativeModules;
|
|
85
|
-
if (I18nManager?.localeIdentifier) {
|
|
86
|
-
const languageCode = I18nManager.localeIdentifier.split("-")[0]?.toLowerCase();
|
|
87
|
-
if (languageCode) {
|
|
88
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
89
|
-
if (supportedLanguage) {
|
|
90
|
-
return supportedLanguage;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
} catch (rnError) {
|
|
96
|
-
console.warn("[locon] \u274C react-native error:", rnError);
|
|
97
|
-
}
|
|
98
|
-
const systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
99
|
-
if (systemLocale) {
|
|
100
|
-
const languageCode = systemLocale.split("-")[0]?.toLowerCase();
|
|
101
|
-
if (languageCode) {
|
|
102
|
-
const supportedLanguage = availableLocales.find((locale) => locale === languageCode);
|
|
103
|
-
if (supportedLanguage) {
|
|
104
|
-
return supportedLanguage;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return null;
|
|
109
|
-
} catch (error) {
|
|
110
|
-
console.warn("[locon] \u274C Could not detect system language:", error);
|
|
111
|
-
return null;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
var getSystemLanguage_default = getSystemLanguage;
|
|
115
|
-
function useLocon() {
|
|
116
|
-
const locon = useContext(LoconContext);
|
|
117
|
-
return locon;
|
|
118
|
-
}
|
|
119
|
-
var useLocon_default = useLocon;
|
|
120
|
-
function LText({ children, assetKey, ...props }) {
|
|
121
|
-
const { l } = useLocon_default();
|
|
122
|
-
return /* @__PURE__ */ jsx(Text, { ...props, children: l(assetKey || children) });
|
|
123
|
-
}
|
|
124
|
-
var LText_default = LText;
|
|
125
|
-
var LoconContext = createContext({
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useCallback, useMemo, useState } from "react";
|
|
3
|
+
import getSystemLanguage from "./utils/getSystemLanguage";
|
|
4
|
+
const LoconContext = createContext({
|
|
126
5
|
assets: {},
|
|
127
6
|
currentLocale: "en",
|
|
128
7
|
defaultLocale: "en",
|
|
@@ -146,7 +25,7 @@ function Locon({
|
|
|
146
25
|
return currentLocaleProps;
|
|
147
26
|
}
|
|
148
27
|
if (autodetect) {
|
|
149
|
-
const systemLocale =
|
|
28
|
+
const systemLocale = getSystemLanguage(Object.keys(assets));
|
|
150
29
|
if (systemLocale && assets[systemLocale]) {
|
|
151
30
|
return systemLocale;
|
|
152
31
|
}
|
|
@@ -169,7 +48,7 @@ function Locon({
|
|
|
169
48
|
if (assets[locale]) {
|
|
170
49
|
setCurrentLocale(locale);
|
|
171
50
|
} else {
|
|
172
|
-
console.warn(`Locale "${locale}" not found in assets.`);
|
|
51
|
+
console.warn(`[locon] Locale "${locale}" not found in assets.`);
|
|
173
52
|
}
|
|
174
53
|
},
|
|
175
54
|
[assets]
|
|
@@ -209,5 +88,11 @@ function Locon({
|
|
|
209
88
|
return /* @__PURE__ */ jsx(LoconContext, { value, children });
|
|
210
89
|
}
|
|
211
90
|
var locon_default = Locon;
|
|
212
|
-
|
|
213
|
-
|
|
91
|
+
import { default as default2 } from "./components/LText";
|
|
92
|
+
import { default as default3 } from "./hooks/useLocon";
|
|
93
|
+
export {
|
|
94
|
+
default2 as LText,
|
|
95
|
+
LoconContext,
|
|
96
|
+
locon_default as default,
|
|
97
|
+
default3 as useLocon
|
|
98
|
+
};
|
package/package.json
CHANGED