locon 0.0.5 → 1.0.1
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.d.mts +22 -24
- package/dist/index.d.ts +22 -24
- package/dist/index.js +91 -64
- package/dist/index.mjs +88 -61
- package/package.json +13 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,36 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
declare function L({ children }: Props): string;
|
|
5
|
-
|
|
6
|
-
declare function l(assetKey: string): string;
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactElement, ComponentProps, PropsWithChildren } from 'react';
|
|
3
|
+
import { Text } from 'react-native';
|
|
7
4
|
|
|
8
5
|
type Assets = Record<string, Record<string, string>>;
|
|
9
6
|
|
|
10
|
-
interface
|
|
7
|
+
interface LoconContext$1 {
|
|
8
|
+
l: (assetKey: string) => string;
|
|
11
9
|
assets: Assets;
|
|
12
10
|
currentLocale: string;
|
|
13
11
|
defaultLocale: string;
|
|
14
12
|
autodetect: boolean;
|
|
13
|
+
setLocale: (locale: string) => void;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
var __LOCON_SETTINGS__: Settings;
|
|
22
|
-
namespace NodeJS {
|
|
23
|
-
interface Global {
|
|
24
|
-
__LOCON_SETTINGS__: Settings;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
16
|
+
interface Props$1 extends ComponentProps<typeof Text> {
|
|
17
|
+
children: string;
|
|
18
|
+
assetKey?: string;
|
|
27
19
|
}
|
|
28
|
-
declare function
|
|
29
|
-
|
|
20
|
+
declare function LText({ children, assetKey, ...props }: Props$1): ReactElement;
|
|
21
|
+
|
|
22
|
+
declare function useLocon(): LoconContext$1;
|
|
30
23
|
|
|
31
|
-
declare const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
declare const LoconContext: react.Context<LoconContext$1>;
|
|
25
|
+
interface Props extends PropsWithChildren {
|
|
26
|
+
assets?: Record<string, Record<string, string>>;
|
|
27
|
+
currentLocale?: string;
|
|
28
|
+
defaultLocale?: string;
|
|
29
|
+
projectLocale?: string;
|
|
30
|
+
autodetect?: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function Locon({ children, assets, currentLocale: currentLocaleProps, defaultLocale, projectLocale, autodetect, }: Props): ReactElement;
|
|
35
33
|
|
|
36
|
-
export {
|
|
34
|
+
export { LText, LoconContext, Locon as default, useLocon };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,36 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
declare function L({ children }: Props): string;
|
|
5
|
-
|
|
6
|
-
declare function l(assetKey: string): string;
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactElement, ComponentProps, PropsWithChildren } from 'react';
|
|
3
|
+
import { Text } from 'react-native';
|
|
7
4
|
|
|
8
5
|
type Assets = Record<string, Record<string, string>>;
|
|
9
6
|
|
|
10
|
-
interface
|
|
7
|
+
interface LoconContext$1 {
|
|
8
|
+
l: (assetKey: string) => string;
|
|
11
9
|
assets: Assets;
|
|
12
10
|
currentLocale: string;
|
|
13
11
|
defaultLocale: string;
|
|
14
12
|
autodetect: boolean;
|
|
13
|
+
setLocale: (locale: string) => void;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
var __LOCON_SETTINGS__: Settings;
|
|
22
|
-
namespace NodeJS {
|
|
23
|
-
interface Global {
|
|
24
|
-
__LOCON_SETTINGS__: Settings;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
16
|
+
interface Props$1 extends ComponentProps<typeof Text> {
|
|
17
|
+
children: string;
|
|
18
|
+
assetKey?: string;
|
|
27
19
|
}
|
|
28
|
-
declare function
|
|
29
|
-
|
|
20
|
+
declare function LText({ children, assetKey, ...props }: Props$1): ReactElement;
|
|
21
|
+
|
|
22
|
+
declare function useLocon(): LoconContext$1;
|
|
30
23
|
|
|
31
|
-
declare const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
declare const LoconContext: react.Context<LoconContext$1>;
|
|
25
|
+
interface Props extends PropsWithChildren {
|
|
26
|
+
assets?: Record<string, Record<string, string>>;
|
|
27
|
+
currentLocale?: string;
|
|
28
|
+
defaultLocale?: string;
|
|
29
|
+
projectLocale?: string;
|
|
30
|
+
autodetect?: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function Locon({ children, assets, currentLocale: currentLocaleProps, defaultLocale, projectLocale, autodetect, }: Props): ReactElement;
|
|
35
33
|
|
|
36
|
-
export {
|
|
34
|
+
export { LText, LoconContext, Locon as default, useLocon };
|
package/dist/index.js
CHANGED
|
@@ -2,72 +2,99 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
var
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var reactNative = require('react-native');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
7
8
|
|
|
8
|
-
// src/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
defaultLocale: "en",
|
|
13
|
-
autodetect: true
|
|
14
|
-
};
|
|
15
|
-
var globalSettings = defaultSettings;
|
|
16
|
-
function init(options = {}) {
|
|
17
|
-
globalSettings = { ...defaultSettings, ...options };
|
|
18
|
-
if (isBrowser) {
|
|
19
|
-
window.__LOCON_SETTINGS__ = globalSettings;
|
|
20
|
-
} else {
|
|
21
|
-
global.__LOCON_SETTINGS__ = globalSettings;
|
|
22
|
-
}
|
|
23
|
-
return globalSettings;
|
|
24
|
-
}
|
|
25
|
-
function getSettings() {
|
|
26
|
-
if (isBrowser) {
|
|
27
|
-
return window.__LOCON_SETTINGS__ || globalSettings;
|
|
28
|
-
} else {
|
|
29
|
-
return global.__LOCON_SETTINGS__ || globalSettings;
|
|
30
|
-
}
|
|
9
|
+
// src/locon/index.tsx
|
|
10
|
+
function useLocon() {
|
|
11
|
+
const locon = react.useContext(LoconContext);
|
|
12
|
+
return locon;
|
|
31
13
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const {
|
|
35
|
-
|
|
36
|
-
return assets ?? {};
|
|
14
|
+
var useLocon_default = useLocon;
|
|
15
|
+
function LText({ children, assetKey, ...props }) {
|
|
16
|
+
const { l } = useLocon_default();
|
|
17
|
+
return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { ...props, children: l(assetKey || children) });
|
|
37
18
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
19
|
+
var LText_default = LText;
|
|
20
|
+
var LoconContext = react.createContext({
|
|
21
|
+
assets: {},
|
|
22
|
+
currentLocale: "en",
|
|
23
|
+
defaultLocale: "en",
|
|
24
|
+
autodetect: true,
|
|
25
|
+
setLocale: () => {
|
|
26
|
+
},
|
|
27
|
+
// Default implementation
|
|
28
|
+
l: (assetKey) => assetKey
|
|
29
|
+
// Default implementation
|
|
30
|
+
});
|
|
31
|
+
function Locon({
|
|
32
|
+
children,
|
|
33
|
+
assets = {},
|
|
34
|
+
currentLocale: currentLocaleProps = "en",
|
|
35
|
+
defaultLocale = "en",
|
|
36
|
+
projectLocale = "en",
|
|
37
|
+
autodetect = true
|
|
38
|
+
}) {
|
|
39
|
+
const [currentLocale, setCurrentLocale] = react.useState(currentLocaleProps);
|
|
40
|
+
const getAssets = react.useCallback(() => {
|
|
41
|
+
const currentAssets = assets[currentLocale] ?? assets[defaultLocale];
|
|
42
|
+
return currentAssets ?? {};
|
|
43
|
+
}, [assets, currentLocale, defaultLocale]);
|
|
44
|
+
const getAsset = react.useCallback(
|
|
45
|
+
(assetKey) => {
|
|
46
|
+
const assetsObj = getAssets();
|
|
47
|
+
return assetsObj[assetKey] ?? "";
|
|
48
|
+
},
|
|
49
|
+
[getAssets]
|
|
50
|
+
);
|
|
51
|
+
const setCurrentLocaleHandler = react.useCallback(
|
|
52
|
+
(locale) => {
|
|
53
|
+
if (assets[locale]) {
|
|
54
|
+
setCurrentLocale(locale);
|
|
55
|
+
} else {
|
|
56
|
+
console.warn(`Locale "${locale}" not found in assets.`);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
[assets]
|
|
60
|
+
);
|
|
61
|
+
const getAssetByValue = react.useCallback(
|
|
62
|
+
(assetValue, locale = projectLocale) => {
|
|
63
|
+
const currentAssets = assets[locale];
|
|
64
|
+
if (!currentAssets) {
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
const assetKey = Object.keys(currentAssets).find((key) => currentAssets[key] === assetValue);
|
|
68
|
+
return assetKey ? getAsset(assetKey) : void 0;
|
|
69
|
+
},
|
|
70
|
+
[assets, getAsset, projectLocale]
|
|
71
|
+
);
|
|
72
|
+
const l = react.useCallback(
|
|
73
|
+
(assetKey) => {
|
|
74
|
+
if (!assetKey) {
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
const asset = getAsset(assetKey) || getAssetByValue(assetKey) || getAssetByValue(assetKey, defaultLocale);
|
|
78
|
+
return asset ?? assetKey;
|
|
79
|
+
},
|
|
80
|
+
[getAsset, getAssetByValue, defaultLocale]
|
|
81
|
+
);
|
|
82
|
+
const value = react.useMemo(
|
|
83
|
+
() => ({
|
|
84
|
+
assets,
|
|
85
|
+
currentLocale,
|
|
86
|
+
defaultLocale,
|
|
87
|
+
autodetect,
|
|
88
|
+
l,
|
|
89
|
+
setLocale: setCurrentLocaleHandler
|
|
90
|
+
}),
|
|
91
|
+
[assets, currentLocale, defaultLocale, autodetect, l, setCurrentLocaleHandler]
|
|
92
|
+
);
|
|
93
|
+
return /* @__PURE__ */ jsxRuntime.jsx(LoconContext, { value, children });
|
|
64
94
|
}
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
// src/index.ts
|
|
68
|
-
var src_default = { init, setLocale };
|
|
95
|
+
var locon_default = Locon;
|
|
69
96
|
|
|
70
|
-
exports.
|
|
71
|
-
exports.
|
|
72
|
-
exports.
|
|
73
|
-
exports.
|
|
97
|
+
exports.LText = LText_default;
|
|
98
|
+
exports.LoconContext = LoconContext;
|
|
99
|
+
exports.default = locon_default;
|
|
100
|
+
exports.useLocon = useLocon_default;
|
package/dist/index.mjs
CHANGED
|
@@ -1,66 +1,93 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { createContext, useContext, useState, useCallback, useMemo } from 'react';
|
|
2
|
+
import { Text } from 'react-native';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
4
|
|
|
4
|
-
// src/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
defaultLocale: "en",
|
|
9
|
-
autodetect: true
|
|
10
|
-
};
|
|
11
|
-
var globalSettings = defaultSettings;
|
|
12
|
-
function init(options = {}) {
|
|
13
|
-
globalSettings = { ...defaultSettings, ...options };
|
|
14
|
-
if (isBrowser) {
|
|
15
|
-
window.__LOCON_SETTINGS__ = globalSettings;
|
|
16
|
-
} else {
|
|
17
|
-
global.__LOCON_SETTINGS__ = globalSettings;
|
|
18
|
-
}
|
|
19
|
-
return globalSettings;
|
|
20
|
-
}
|
|
21
|
-
function getSettings() {
|
|
22
|
-
if (isBrowser) {
|
|
23
|
-
return window.__LOCON_SETTINGS__ || globalSettings;
|
|
24
|
-
} else {
|
|
25
|
-
return global.__LOCON_SETTINGS__ || globalSettings;
|
|
26
|
-
}
|
|
5
|
+
// src/locon/index.tsx
|
|
6
|
+
function useLocon() {
|
|
7
|
+
const locon = useContext(LoconContext);
|
|
8
|
+
return locon;
|
|
27
9
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const {
|
|
31
|
-
|
|
32
|
-
return assets ?? {};
|
|
10
|
+
var useLocon_default = useLocon;
|
|
11
|
+
function LText({ children, assetKey, ...props }) {
|
|
12
|
+
const { l } = useLocon_default();
|
|
13
|
+
return /* @__PURE__ */ jsx(Text, { ...props, children: l(assetKey || children) });
|
|
33
14
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
15
|
+
var LText_default = LText;
|
|
16
|
+
var LoconContext = createContext({
|
|
17
|
+
assets: {},
|
|
18
|
+
currentLocale: "en",
|
|
19
|
+
defaultLocale: "en",
|
|
20
|
+
autodetect: true,
|
|
21
|
+
setLocale: () => {
|
|
22
|
+
},
|
|
23
|
+
// Default implementation
|
|
24
|
+
l: (assetKey) => assetKey
|
|
25
|
+
// Default implementation
|
|
26
|
+
});
|
|
27
|
+
function Locon({
|
|
28
|
+
children,
|
|
29
|
+
assets = {},
|
|
30
|
+
currentLocale: currentLocaleProps = "en",
|
|
31
|
+
defaultLocale = "en",
|
|
32
|
+
projectLocale = "en",
|
|
33
|
+
autodetect = true
|
|
34
|
+
}) {
|
|
35
|
+
const [currentLocale, setCurrentLocale] = useState(currentLocaleProps);
|
|
36
|
+
const getAssets = useCallback(() => {
|
|
37
|
+
const currentAssets = assets[currentLocale] ?? assets[defaultLocale];
|
|
38
|
+
return currentAssets ?? {};
|
|
39
|
+
}, [assets, currentLocale, defaultLocale]);
|
|
40
|
+
const getAsset = useCallback(
|
|
41
|
+
(assetKey) => {
|
|
42
|
+
const assetsObj = getAssets();
|
|
43
|
+
return assetsObj[assetKey] ?? "";
|
|
44
|
+
},
|
|
45
|
+
[getAssets]
|
|
46
|
+
);
|
|
47
|
+
const setCurrentLocaleHandler = useCallback(
|
|
48
|
+
(locale) => {
|
|
49
|
+
if (assets[locale]) {
|
|
50
|
+
setCurrentLocale(locale);
|
|
51
|
+
} else {
|
|
52
|
+
console.warn(`Locale "${locale}" not found in assets.`);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
[assets]
|
|
56
|
+
);
|
|
57
|
+
const getAssetByValue = useCallback(
|
|
58
|
+
(assetValue, locale = projectLocale) => {
|
|
59
|
+
const currentAssets = assets[locale];
|
|
60
|
+
if (!currentAssets) {
|
|
61
|
+
return void 0;
|
|
62
|
+
}
|
|
63
|
+
const assetKey = Object.keys(currentAssets).find((key) => currentAssets[key] === assetValue);
|
|
64
|
+
return assetKey ? getAsset(assetKey) : void 0;
|
|
65
|
+
},
|
|
66
|
+
[assets, getAsset, projectLocale]
|
|
67
|
+
);
|
|
68
|
+
const l = useCallback(
|
|
69
|
+
(assetKey) => {
|
|
70
|
+
if (!assetKey) {
|
|
71
|
+
return "";
|
|
72
|
+
}
|
|
73
|
+
const asset = getAsset(assetKey) || getAssetByValue(assetKey) || getAssetByValue(assetKey, defaultLocale);
|
|
74
|
+
return asset ?? assetKey;
|
|
75
|
+
},
|
|
76
|
+
[getAsset, getAssetByValue, defaultLocale]
|
|
77
|
+
);
|
|
78
|
+
const value = useMemo(
|
|
79
|
+
() => ({
|
|
80
|
+
assets,
|
|
81
|
+
currentLocale,
|
|
82
|
+
defaultLocale,
|
|
83
|
+
autodetect,
|
|
84
|
+
l,
|
|
85
|
+
setLocale: setCurrentLocaleHandler
|
|
86
|
+
}),
|
|
87
|
+
[assets, currentLocale, defaultLocale, autodetect, l, setCurrentLocaleHandler]
|
|
88
|
+
);
|
|
89
|
+
return /* @__PURE__ */ jsx(LoconContext, { value, children });
|
|
60
90
|
}
|
|
61
|
-
var
|
|
62
|
-
|
|
63
|
-
// src/index.ts
|
|
64
|
-
var src_default = { init, setLocale };
|
|
91
|
+
var locon_default = Locon;
|
|
65
92
|
|
|
66
|
-
export {
|
|
93
|
+
export { LText_default as LText, LoconContext, locon_default as default, useLocon_default as useLocon };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "locon",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "locon is a simple and lightweight library for React
|
|
3
|
+
"version": "1.0.1",
|
|
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",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
],
|
|
29
29
|
"author": "loconapp",
|
|
30
30
|
"license": "ISC",
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=19.0.0",
|
|
33
|
+
"react-native": ">=0.70.0"
|
|
34
|
+
},
|
|
31
35
|
"devDependencies": {
|
|
32
36
|
"@babel/preset-react": "^7.24.7",
|
|
33
37
|
"@eslint/js": "^9.10.0",
|
|
@@ -38,19 +42,21 @@
|
|
|
38
42
|
"@testing-library/jest-dom": "^6.5.0",
|
|
39
43
|
"@testing-library/react": "^16.0.1",
|
|
40
44
|
"@types/jest": "^29.5.12",
|
|
41
|
-
"@types/react-dom": "^
|
|
42
|
-
"@types/react": "^
|
|
45
|
+
"@types/react-dom": "^19.1.0",
|
|
46
|
+
"@types/react": "^19.1.1",
|
|
47
|
+
"@types/react-native": "^0.73.0",
|
|
43
48
|
"babel-jest": "^29.7.0",
|
|
44
49
|
"eslint-plugin-react": "^7.35.2",
|
|
45
50
|
"eslint": "^9.10.0",
|
|
46
51
|
"jest-environment-jsdom": "^29.7.0",
|
|
47
52
|
"jest": "^29.7.0",
|
|
48
53
|
"prettier": "3.4.1",
|
|
49
|
-
"react-dom": "^
|
|
50
|
-
"react": "^
|
|
54
|
+
"react-dom": "^19.1.1",
|
|
55
|
+
"react": "^19.1.1",
|
|
56
|
+
"react-native": "^0.73.0",
|
|
51
57
|
"ts-jest": "^29.2.5",
|
|
52
58
|
"tsup": "^8.2.4",
|
|
53
59
|
"typescript-eslint": "^8.5.0",
|
|
54
|
-
"typescript": "^5.
|
|
60
|
+
"typescript": "^5.8.3"
|
|
55
61
|
}
|
|
56
62
|
}
|