locon 0.0.5 → 1.0.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/dist/index.d.mts CHANGED
@@ -1,36 +1,34 @@
1
- interface Props {
2
- children: string;
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 Settings {
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
- declare global {
18
- interface Window {
19
- __LOCON_SETTINGS__: Settings;
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 init(options?: {}): Settings;
29
- declare function setLocale(locale: string): void;
20
+ declare function LText({ children, assetKey, ...props }: Props$1): ReactElement;
21
+
22
+ declare function useLocon(): LoconContext$1;
30
23
 
31
- declare const _default: {
32
- init: typeof init;
33
- setLocale: typeof setLocale;
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 { L, _default as default, l, setLocale };
34
+ export { LText, LoconContext, Locon as default, useLocon };
package/dist/index.d.ts CHANGED
@@ -1,36 +1,34 @@
1
- interface Props {
2
- children: string;
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 Settings {
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
- declare global {
18
- interface Window {
19
- __LOCON_SETTINGS__: Settings;
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 init(options?: {}): Settings;
29
- declare function setLocale(locale: string): void;
20
+ declare function LText({ children, assetKey, ...props }: Props$1): ReactElement;
21
+
22
+ declare function useLocon(): LoconContext$1;
30
23
 
31
- declare const _default: {
32
- init: typeof init;
33
- setLocale: typeof setLocale;
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 { L, _default as default, l, setLocale };
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
- // src/helpers/environment.ts
6
- var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
5
+ var react = require('react');
6
+ var reactNative = require('react-native');
7
+ var jsxRuntime = require('react/jsx-runtime');
7
8
 
8
- // src/settings/index.ts
9
- var defaultSettings = {
10
- assets: { en: {} },
11
- currentLocale: "en",
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
- function getAssets() {
33
- const settings = getSettings();
34
- const { currentLocale, defaultLocale } = settings;
35
- const assets = settings.assets[currentLocale] ?? settings.assets[defaultLocale];
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
- function getAsset(assetKey) {
39
- const assets = getAssets();
40
- return assets[assetKey] ?? "";
41
- }
42
- function setLocale(locale) {
43
- if (!locale) {
44
- return;
45
- }
46
- const settings = getSettings();
47
- settings.currentLocale = locale;
48
- init(settings);
49
- }
50
-
51
- // src/functions/l.ts
52
- function l(assetKey) {
53
- if (!assetKey) {
54
- return "";
55
- }
56
- const asset = getAsset(assetKey);
57
- return asset;
58
- }
59
- var l_default = l;
60
-
61
- // src/components/L.ts
62
- function L({ children }) {
63
- return l_default(children);
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 L_default = L;
66
-
67
- // src/index.ts
68
- var src_default = { init, setLocale };
95
+ var locon_default = Locon;
69
96
 
70
- exports.L = L_default;
71
- exports.default = src_default;
72
- exports.l = l_default;
73
- exports.setLocale = setLocale;
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
- // src/helpers/environment.ts
2
- var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
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/settings/index.ts
5
- var defaultSettings = {
6
- assets: { en: {} },
7
- currentLocale: "en",
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
- function getAssets() {
29
- const settings = getSettings();
30
- const { currentLocale, defaultLocale } = settings;
31
- const assets = settings.assets[currentLocale] ?? settings.assets[defaultLocale];
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
- function getAsset(assetKey) {
35
- const assets = getAssets();
36
- return assets[assetKey] ?? "";
37
- }
38
- function setLocale(locale) {
39
- if (!locale) {
40
- return;
41
- }
42
- const settings = getSettings();
43
- settings.currentLocale = locale;
44
- init(settings);
45
- }
46
-
47
- // src/functions/l.ts
48
- function l(assetKey) {
49
- if (!assetKey) {
50
- return "";
51
- }
52
- const asset = getAsset(assetKey);
53
- return asset;
54
- }
55
- var l_default = l;
56
-
57
- // src/components/L.ts
58
- function L({ children }) {
59
- return l_default(children);
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 L_default = L;
62
-
63
- // src/index.ts
64
- var src_default = { init, setLocale };
91
+ var locon_default = Locon;
65
92
 
66
- export { L_default as L, src_default as default, l_default as l, setLocale };
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": "0.0.5",
4
- "description": "locon is a simple and lightweight library for React and React Native that provides a simple way to manage translations in your app.",
3
+ "version": "1.0.0",
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": "^18.3.0",
42
- "@types/react": "^18.3.5",
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": "^18.3.1",
50
- "react": "^18.3.1",
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.7.2"
60
+ "typescript": "^5.8.3"
55
61
  }
56
62
  }