@gravito/cosmos 1.0.0-alpha.2 → 1.0.0-alpha.3

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 CHANGED
@@ -1,28 +1,70 @@
1
1
  // src/I18nService.ts
2
+ class I18nInstance {
3
+ manager;
4
+ _locale;
5
+ constructor(manager, initialLocale) {
6
+ this.manager = manager;
7
+ this._locale = initialLocale;
8
+ }
9
+ get locale() {
10
+ return this._locale;
11
+ }
12
+ set locale(value) {
13
+ this.setLocale(value);
14
+ }
15
+ setLocale(locale) {
16
+ if (this.manager.getConfig().supportedLocales.includes(locale)) {
17
+ this._locale = locale;
18
+ }
19
+ }
20
+ getLocale() {
21
+ return this._locale;
22
+ }
23
+ t(key, replacements) {
24
+ return this.manager.translate(this._locale, key, replacements);
25
+ }
26
+ has(key) {
27
+ return this.t(key) !== key;
28
+ }
29
+ clone(locale) {
30
+ return new I18nInstance(this.manager, locale || this._locale);
31
+ }
32
+ }
33
+
2
34
  class I18nManager {
3
35
  config;
4
- _locale;
5
36
  translations = {};
37
+ globalInstance;
6
38
  constructor(config) {
7
39
  this.config = config;
8
- this._locale = config.defaultLocale;
9
40
  if (config.translations) {
10
41
  this.translations = config.translations;
11
42
  }
43
+ this.globalInstance = new I18nInstance(this, config.defaultLocale);
12
44
  }
13
45
  get locale() {
14
- return this._locale;
46
+ return this.globalInstance.locale;
15
47
  }
16
48
  set locale(value) {
17
- this.setLocale(value);
49
+ this.globalInstance.locale = value;
18
50
  }
19
51
  setLocale(locale) {
20
- if (this.config.supportedLocales.includes(locale)) {
21
- this._locale = locale;
22
- }
52
+ this.globalInstance.setLocale(locale);
23
53
  }
24
54
  getLocale() {
25
- return this._locale;
55
+ return this.globalInstance.getLocale();
56
+ }
57
+ t(key, replacements) {
58
+ return this.globalInstance.t(key, replacements);
59
+ }
60
+ has(key) {
61
+ return this.globalInstance.has(key);
62
+ }
63
+ clone(locale) {
64
+ return new I18nInstance(this, locale || this.config.defaultLocale);
65
+ }
66
+ getConfig() {
67
+ return this.config;
26
68
  }
27
69
  addResource(locale, translations) {
28
70
  this.translations[locale] = {
@@ -30,9 +72,9 @@ class I18nManager {
30
72
  ...translations
31
73
  };
32
74
  }
33
- t(key, replacements) {
75
+ translate(locale, key, replacements) {
34
76
  const keys = key.split(".");
35
- let value = this.translations[this._locale];
77
+ let value = this.translations[locale];
36
78
  for (const k of keys) {
37
79
  if (value && typeof value === "object" && k in value) {
38
80
  value = value[k];
@@ -41,7 +83,7 @@ class I18nManager {
41
83
  break;
42
84
  }
43
85
  }
44
- if (value === undefined && this._locale !== this.config.defaultLocale) {
86
+ if (value === undefined && locale !== this.config.defaultLocale) {
45
87
  let fallbackValue = this.translations[this.config.defaultLocale];
46
88
  for (const k of keys) {
47
89
  if (fallbackValue && typeof fallbackValue === "object" && k in fallbackValue) {
@@ -63,16 +105,17 @@ class I18nManager {
63
105
  }
64
106
  return value;
65
107
  }
66
- has(key) {
67
- return this.t(key) !== key;
68
- }
69
108
  }
70
- var localeMiddleware = (i18n) => {
109
+ var localeMiddleware2 = (i18nManager) => {
71
110
  return async (c, next) => {
72
- const paramLocale = c.req.param("locale");
73
- if (paramLocale) {
74
- i18n.setLocale(paramLocale);
111
+ let locale = c.req.param("locale") || c.req.query("lang");
112
+ if (!locale) {
113
+ const acceptLang = c.req.header("Accept-Language");
114
+ if (acceptLang) {
115
+ locale = acceptLang.split(",")[0]?.trim();
116
+ }
75
117
  }
118
+ const i18n = i18nManager.clone(locale);
76
119
  c.set("i18n", i18n);
77
120
  await next();
78
121
  };
@@ -109,17 +152,15 @@ class I18nOrbit {
109
152
  this.config = config;
110
153
  }
111
154
  install(core) {
112
- const i18n = new I18nManager(this.config);
113
- core.adapter.use("*", async (c, next) => {
114
- c.set("i18n", i18n);
115
- await next();
116
- });
155
+ const i18nManager = new I18nManager(this.config);
156
+ core.adapter.use("*", localeMiddleware(i18nManager));
117
157
  core.logger.info(`I18n Orbit initialized with locale: ${this.config.defaultLocale}`);
118
158
  }
119
159
  }
120
160
  export {
121
- localeMiddleware,
161
+ localeMiddleware2 as localeMiddleware,
122
162
  loadTranslations,
123
163
  I18nOrbit,
124
- I18nManager
164
+ I18nManager,
165
+ I18nInstance
125
166
  };
package/dist/src/index.js CHANGED
@@ -1,3 +1,50 @@
1
+ import { createRequire } from "node:module";
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __reExport = (target, mod, secondTarget) => {
8
+ for (let key of __getOwnPropNames(mod))
9
+ if (!__hasOwnProp.call(target, key) && key !== "default")
10
+ __defProp(target, key, {
11
+ get: () => mod[key],
12
+ enumerable: true
13
+ });
14
+ if (secondTarget) {
15
+ for (let key of __getOwnPropNames(mod))
16
+ if (!__hasOwnProp.call(secondTarget, key) && key !== "default")
17
+ __defProp(secondTarget, key, {
18
+ get: () => mod[key],
19
+ enumerable: true
20
+ });
21
+ return secondTarget;
22
+ }
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => {
25
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
26
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
27
+ for (let key of __getOwnPropNames(mod))
28
+ if (!__hasOwnProp.call(to, key))
29
+ __defProp(to, key, {
30
+ get: () => mod[key],
31
+ enumerable: true
32
+ });
33
+ return to;
34
+ };
35
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
36
+ var __export = (target, all) => {
37
+ for (var name in all)
38
+ __defProp(target, name, {
39
+ get: all[name],
40
+ enumerable: true,
41
+ configurable: true,
42
+ set: (newValue) => all[name] = () => newValue
43
+ });
44
+ };
45
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
46
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
47
+
1
48
  // src/I18nService.ts
2
49
  class I18nManager {
3
50
  config;
@@ -110,9 +157,10 @@ class I18nOrbit {
110
157
  }
111
158
  install(core) {
112
159
  const i18n = new I18nManager(this.config);
113
- core.app.use("*", async (c, next) => {
160
+ core.adapter.use("*", async (c, next) => {
114
161
  c.set("i18n", i18n);
115
162
  await next();
163
+ return;
116
164
  });
117
165
  core.logger.info(`I18n Orbit initialized with locale: ${this.config.defaultLocale}`);
118
166
  }