@gravito/cosmos 2.0.0 → 3.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/.turbo/turbo-build.log +20 -0
- package/.turbo/turbo-test$colon$ci.log +35 -0
- package/.turbo/turbo-test$colon$coverage.log +35 -0
- package/.turbo/turbo-test.log +27 -0
- package/.turbo/turbo-typecheck.log +2 -0
- package/CHANGELOG.md +14 -0
- package/build.ts +35 -0
- package/dist/index.cjs +309 -0
- package/dist/index.d.cts +274 -0
- package/dist/index.d.ts +274 -0
- package/dist/index.js +140 -31
- package/package.json +18 -6
- package/src/I18nService.ts +95 -16
- package/src/index.ts +32 -6
- package/tests/manager.test.ts +0 -1
- package/tests/service.test.ts +5 -0
- package/dist/src/index.js +0 -173
package/dist/src/index.js
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
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
|
-
|
|
48
|
-
// src/I18nService.ts
|
|
49
|
-
class I18nManager {
|
|
50
|
-
config;
|
|
51
|
-
_locale;
|
|
52
|
-
translations = {};
|
|
53
|
-
constructor(config) {
|
|
54
|
-
this.config = config;
|
|
55
|
-
this._locale = config.defaultLocale;
|
|
56
|
-
if (config.translations) {
|
|
57
|
-
this.translations = config.translations;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
get locale() {
|
|
61
|
-
return this._locale;
|
|
62
|
-
}
|
|
63
|
-
set locale(value) {
|
|
64
|
-
this.setLocale(value);
|
|
65
|
-
}
|
|
66
|
-
setLocale(locale) {
|
|
67
|
-
if (this.config.supportedLocales.includes(locale)) {
|
|
68
|
-
this._locale = locale;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
getLocale() {
|
|
72
|
-
return this._locale;
|
|
73
|
-
}
|
|
74
|
-
addResource(locale, translations) {
|
|
75
|
-
this.translations[locale] = {
|
|
76
|
-
...this.translations[locale] || {},
|
|
77
|
-
...translations
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
t(key, replacements) {
|
|
81
|
-
const keys = key.split(".");
|
|
82
|
-
let value = this.translations[this._locale];
|
|
83
|
-
for (const k of keys) {
|
|
84
|
-
if (value && typeof value === "object" && k in value) {
|
|
85
|
-
value = value[k];
|
|
86
|
-
} else {
|
|
87
|
-
value = undefined;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (value === undefined && this._locale !== this.config.defaultLocale) {
|
|
92
|
-
let fallbackValue = this.translations[this.config.defaultLocale];
|
|
93
|
-
for (const k of keys) {
|
|
94
|
-
if (fallbackValue && typeof fallbackValue === "object" && k in fallbackValue) {
|
|
95
|
-
fallbackValue = fallbackValue[k];
|
|
96
|
-
} else {
|
|
97
|
-
fallbackValue = undefined;
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
value = fallbackValue;
|
|
102
|
-
}
|
|
103
|
-
if (value === undefined || typeof value !== "string") {
|
|
104
|
-
return key;
|
|
105
|
-
}
|
|
106
|
-
if (replacements) {
|
|
107
|
-
for (const [search, replace] of Object.entries(replacements)) {
|
|
108
|
-
value = value.replace(new RegExp(`:${search}`, "g"), String(replace));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return value;
|
|
112
|
-
}
|
|
113
|
-
has(key) {
|
|
114
|
-
return this.t(key) !== key;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
var localeMiddleware = (i18n) => {
|
|
118
|
-
return async (c, next) => {
|
|
119
|
-
const paramLocale = c.req.param("locale");
|
|
120
|
-
if (paramLocale) {
|
|
121
|
-
i18n.setLocale(paramLocale);
|
|
122
|
-
}
|
|
123
|
-
c.set("i18n", i18n);
|
|
124
|
-
await next();
|
|
125
|
-
};
|
|
126
|
-
};
|
|
127
|
-
// src/loader.ts
|
|
128
|
-
import { readdir, readFile } from "node:fs/promises";
|
|
129
|
-
import { join, parse } from "node:path";
|
|
130
|
-
async function loadTranslations(directory) {
|
|
131
|
-
const translations = {};
|
|
132
|
-
try {
|
|
133
|
-
const files = await readdir(directory);
|
|
134
|
-
for (const file of files) {
|
|
135
|
-
if (!file.endsWith(".json")) {
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
const locale = parse(file).name;
|
|
139
|
-
const content = await readFile(join(directory, file), "utf-8");
|
|
140
|
-
try {
|
|
141
|
-
translations[locale] = JSON.parse(content);
|
|
142
|
-
} catch (e) {
|
|
143
|
-
console.error(`[Orbit-I18n] Failed to parse translation file: ${file}`, e);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
} catch (_e) {
|
|
147
|
-
console.warn(`[Orbit-I18n] Could not load translations from ${directory}. Directory might not exist.`);
|
|
148
|
-
}
|
|
149
|
-
return translations;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// src/index.ts
|
|
153
|
-
class I18nOrbit {
|
|
154
|
-
config;
|
|
155
|
-
constructor(config) {
|
|
156
|
-
this.config = config;
|
|
157
|
-
}
|
|
158
|
-
install(core) {
|
|
159
|
-
const i18n = new I18nManager(this.config);
|
|
160
|
-
core.adapter.use("*", async (c, next) => {
|
|
161
|
-
c.set("i18n", i18n);
|
|
162
|
-
await next();
|
|
163
|
-
return;
|
|
164
|
-
});
|
|
165
|
-
core.logger.info(`I18n Orbit initialized with locale: ${this.config.defaultLocale}`);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
export {
|
|
169
|
-
localeMiddleware,
|
|
170
|
-
loadTranslations,
|
|
171
|
-
I18nOrbit,
|
|
172
|
-
I18nManager
|
|
173
|
-
};
|