locon 0.0.2 → 0.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/dist/index.d.mts CHANGED
@@ -5,16 +5,32 @@ declare function L({ children }: Props): string;
5
5
 
6
6
  declare function l(assetKey: string): string;
7
7
 
8
+ type Assets = Record<string, Record<string, string>>;
9
+
10
+ interface Settings {
11
+ assets: Assets;
12
+ currentLocale: string;
13
+ defaultLocale: string;
14
+ autodetect: boolean;
15
+ }
16
+
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
+ }
27
+ }
28
+ declare function init(options?: {}): Settings;
29
+ declare function setLocale(locale: string): void;
30
+
8
31
  declare const _default: {
9
- L: typeof L;
10
- init: (options?: {}) => {
11
- defaultLocale: string;
12
- locales: {
13
- en: {};
14
- };
15
- autodetect: boolean;
16
- };
17
- l: typeof l;
32
+ init: typeof init;
33
+ setLocale: typeof setLocale;
18
34
  };
19
35
 
20
- export { _default as default };
36
+ export { L, _default as default, l, setLocale };
package/dist/index.d.ts CHANGED
@@ -5,16 +5,32 @@ declare function L({ children }: Props): string;
5
5
 
6
6
  declare function l(assetKey: string): string;
7
7
 
8
+ type Assets = Record<string, Record<string, string>>;
9
+
10
+ interface Settings {
11
+ assets: Assets;
12
+ currentLocale: string;
13
+ defaultLocale: string;
14
+ autodetect: boolean;
15
+ }
16
+
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
+ }
27
+ }
28
+ declare function init(options?: {}): Settings;
29
+ declare function setLocale(locale: string): void;
30
+
8
31
  declare const _default: {
9
- L: typeof L;
10
- init: (options?: {}) => {
11
- defaultLocale: string;
12
- locales: {
13
- en: {};
14
- };
15
- autodetect: boolean;
16
- };
17
- l: typeof l;
32
+ init: typeof init;
33
+ setLocale: typeof setLocale;
18
34
  };
19
35
 
20
- export { _default as default };
36
+ export { L, _default as default, l, setLocale };
package/dist/index.js CHANGED
@@ -1,28 +1,19 @@
1
1
  'use strict';
2
2
 
3
- // src/functions/l.ts
4
- function l(assetKey) {
5
- return assetKey;
6
- }
7
- var l_default = l;
8
-
9
- // src/components/L.ts
10
- function L({ children }) {
11
- return l_default(children);
12
- }
13
- var L_default = L;
3
+ Object.defineProperty(exports, '__esModule', { value: true });
14
4
 
15
5
  // src/helpers/environment.ts
16
6
  var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
17
7
 
18
8
  // src/settings/index.ts
19
9
  var defaultSettings = {
10
+ assets: { en: {} },
11
+ currentLocale: "en",
20
12
  defaultLocale: "en",
21
- locales: { en: {} },
22
13
  autodetect: true
23
14
  };
24
15
  var globalSettings = defaultSettings;
25
- var init = (options = {}) => {
16
+ function init(options = {}) {
26
17
  globalSettings = { ...defaultSettings, ...options };
27
18
  if (isBrowser) {
28
19
  window.__LOCON_SETTINGS__ = globalSettings;
@@ -30,9 +21,53 @@ var init = (options = {}) => {
30
21
  global.__LOCON_SETTINGS__ = globalSettings;
31
22
  }
32
23
  return globalSettings;
33
- };
24
+ }
25
+ function getSettings() {
26
+ if (isBrowser) {
27
+ return window.__LOCON_SETTINGS__ || globalSettings;
28
+ } else {
29
+ return global.__LOCON_SETTINGS__ || globalSettings;
30
+ }
31
+ }
32
+ function getAssets() {
33
+ const settings = getSettings();
34
+ const { currentLocale, defaultLocale } = settings;
35
+ const assets = settings.assets[currentLocale] ?? settings.assets[defaultLocale];
36
+ return assets ?? {};
37
+ }
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);
64
+ }
65
+ var L_default = L;
34
66
 
35
67
  // src/index.ts
36
- var src_default = { L: L_default, init, l: l_default };
68
+ var src_default = { init, setLocale };
37
69
 
38
- module.exports = src_default;
70
+ exports.L = L_default;
71
+ exports.default = src_default;
72
+ exports.l = l_default;
73
+ exports.setLocale = setLocale;
package/dist/index.mjs CHANGED
@@ -1,26 +1,15 @@
1
- // src/functions/l.ts
2
- function l(assetKey) {
3
- return assetKey;
4
- }
5
- var l_default = l;
6
-
7
- // src/components/L.ts
8
- function L({ children }) {
9
- return l_default(children);
10
- }
11
- var L_default = L;
12
-
13
1
  // src/helpers/environment.ts
14
2
  var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
15
3
 
16
4
  // src/settings/index.ts
17
5
  var defaultSettings = {
6
+ assets: { en: {} },
7
+ currentLocale: "en",
18
8
  defaultLocale: "en",
19
- locales: { en: {} },
20
9
  autodetect: true
21
10
  };
22
11
  var globalSettings = defaultSettings;
23
- var init = (options = {}) => {
12
+ function init(options = {}) {
24
13
  globalSettings = { ...defaultSettings, ...options };
25
14
  if (isBrowser) {
26
15
  window.__LOCON_SETTINGS__ = globalSettings;
@@ -28,9 +17,50 @@ var init = (options = {}) => {
28
17
  global.__LOCON_SETTINGS__ = globalSettings;
29
18
  }
30
19
  return globalSettings;
31
- };
20
+ }
21
+ function getSettings() {
22
+ if (isBrowser) {
23
+ return window.__LOCON_SETTINGS__ || globalSettings;
24
+ } else {
25
+ return global.__LOCON_SETTINGS__ || globalSettings;
26
+ }
27
+ }
28
+ function getAssets() {
29
+ const settings = getSettings();
30
+ const { currentLocale, defaultLocale } = settings;
31
+ const assets = settings.assets[currentLocale] ?? settings.assets[defaultLocale];
32
+ return assets ?? {};
33
+ }
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);
60
+ }
61
+ var L_default = L;
32
62
 
33
63
  // src/index.ts
34
- var src_default = { L: L_default, init, l: l_default };
64
+ var src_default = { init, setLocale };
35
65
 
36
- export { src_default as default };
66
+ export { L_default as L, src_default as default, l_default as l, setLocale };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locon",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
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.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -10,7 +10,8 @@
10
10
  ],
11
11
  "scripts": {
12
12
  "build": "tsup",
13
- "test": "jest"
13
+ "test": "jest",
14
+ "publish": "npm publish"
14
15
  },
15
16
  "keywords": [
16
17
  "i18n",