@i18n-micro/node 1.2.9 → 1.2.15
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.cjs +1 -1
- package/dist/index.mjs +90 -96
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@i18n-micro/core"),t=require("@i18n-micro/utils/parse-path"),n=require("node:fs/promises"),r=require("node:path");async function i(e,i=!1){let a={root:{},routes:{}};try{let o=(await(0,n.readdir)(e,{recursive:!0,withFileTypes:!0})).filter(e=>e.isFile()&&e.name.endsWith(`.json`)),s=await Promise.all(o.map(async t=>{let i=(0,r.join)(t.path,t.name),a=(0,r.relative)(e,i).split(r.sep).join(`/`);try{let e=await(0,n.readFile)(i,`utf-8`);return{relativePath:a,translations:JSON.parse(e)}}catch(e){return console.error(`Failed to load translation file ${i}:`,e),null}}));for(let e of s)e&&(0,t.storeLoadedTranslationFile)(a,e.relativePath,e.translations,i)}catch(t){t&&typeof t==`object`&&`code`in t&&t.code!==`ENOENT`&&console.error(`Failed to read directory ${e}:`,t)}return a}async function a(e,t=!1){let{root:n}=await i(e,t);return n}var o=class extends e.BaseI18n{constructor(e){super({storage:{translations:new Map},plural:e.plural,missingWarn:e.missingWarn,missingHandler:e.missingHandler}),this.currentRoute=`index`,this.locale=e.locale,this.fallbackLocale=e.fallbackLocale||e.locale,this.translationDir=e.translationDir,this.disablePageLocales=e.disablePageLocales??!1}setRoute(e){this.currentRoute=e}getLocale(){return this.locale}getFallbackLocale(){return this.fallbackLocale}getRoute(){return this.currentRoute}async loadTranslations(e){let n=e||this.translationDir;if(!n){console.warn(`[i18n-node] No translation directory specified`);return}let{root:r,routes:a}=await i(n,this.disablePageLocales);for(let[e,t]of Object.entries(r))this.helper.mergeTranslation(e,`index`,t,!0);for(let[e,n]of Object.entries(a))for(let[r,i]of Object.entries(n)){let n=this.helper.getCache(r,`index`);this.helper.loadPageTranslations(r,e,(0,t.mergeRouteTranslationsWithRoot)(n,i))}}async reload(){this.helper.clearCache(),await this.loadTranslations(),console.log(`[i18n-node] Cache cleared and translations reloaded.`)}addTranslations(e,t,n=!0){super.loadTranslationsCore(e,t,n)}addRouteTranslations(e,t,n,r=!0){super.loadRouteTranslationsCore(e,t,n,r)}hasTranslation(e){return this.helper.hasTranslation(this.locale,e)}clear(){super.clearCache()}};function s(e){return new o(e)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:function(){return e.FormatService}}),exports.I18n=o,exports.createI18n=s,Object.defineProperty(exports,"interpolate",{enumerable:!0,get:function(){return e.interpolate}}),exports.loadRootTranslations=a,exports.loadTranslations=i;
|
package/dist/index.mjs
CHANGED
|
@@ -1,99 +1,93 @@
|
|
|
1
|
-
import { BaseI18n as
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
async function u(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
import { BaseI18n as e, FormatService as t, interpolate as n } from "@i18n-micro/core";
|
|
2
|
+
import { mergeRouteTranslationsWithRoot as r, storeLoadedTranslationFile as i } from "@i18n-micro/utils/parse-path";
|
|
3
|
+
import { readFile as a, readdir as o } from "node:fs/promises";
|
|
4
|
+
import { join as s, relative as c, sep as l } from "node:path";
|
|
5
|
+
//#region src/loader.ts
|
|
6
|
+
async function u(e, t = !1) {
|
|
7
|
+
let n = {
|
|
8
|
+
root: {},
|
|
9
|
+
routes: {}
|
|
10
|
+
};
|
|
11
|
+
try {
|
|
12
|
+
let r = (await o(e, {
|
|
13
|
+
recursive: !0,
|
|
14
|
+
withFileTypes: !0
|
|
15
|
+
})).filter((e) => e.isFile() && e.name.endsWith(".json")), u = await Promise.all(r.map(async (t) => {
|
|
16
|
+
let n = s(t.path, t.name), r = c(e, n).split(l).join("/");
|
|
17
|
+
try {
|
|
18
|
+
let e = await a(n, "utf-8");
|
|
19
|
+
return {
|
|
20
|
+
relativePath: r,
|
|
21
|
+
translations: JSON.parse(e)
|
|
22
|
+
};
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return console.error(`Failed to load translation file ${n}:`, e), null;
|
|
25
|
+
}
|
|
26
|
+
}));
|
|
27
|
+
for (let e of u) e && i(n, e.relativePath, e.translations, t);
|
|
28
|
+
} catch (t) {
|
|
29
|
+
t && typeof t == "object" && "code" in t && t.code !== "ENOENT" && console.error(`Failed to read directory ${e}:`, t);
|
|
30
|
+
}
|
|
31
|
+
return n;
|
|
29
32
|
}
|
|
30
|
-
async function
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
async function d(e, t = !1) {
|
|
34
|
+
let { root: n } = await u(e, t);
|
|
35
|
+
return n;
|
|
33
36
|
}
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
clear() {
|
|
86
|
-
super.clearCache();
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function x(o) {
|
|
90
|
-
return new w(o);
|
|
91
|
-
}
|
|
92
|
-
export {
|
|
93
|
-
N as FormatService,
|
|
94
|
-
w as I18n,
|
|
95
|
-
x as createI18n,
|
|
96
|
-
O as interpolate,
|
|
97
|
-
P as loadRootTranslations,
|
|
98
|
-
u as loadTranslations
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/i18n.ts
|
|
39
|
+
var f = class extends e {
|
|
40
|
+
constructor(e) {
|
|
41
|
+
super({
|
|
42
|
+
storage: { translations: /* @__PURE__ */ new Map() },
|
|
43
|
+
plural: e.plural,
|
|
44
|
+
missingWarn: e.missingWarn,
|
|
45
|
+
missingHandler: e.missingHandler
|
|
46
|
+
}), this.currentRoute = "index", this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1;
|
|
47
|
+
}
|
|
48
|
+
setRoute(e) {
|
|
49
|
+
this.currentRoute = e;
|
|
50
|
+
}
|
|
51
|
+
getLocale() {
|
|
52
|
+
return this.locale;
|
|
53
|
+
}
|
|
54
|
+
getFallbackLocale() {
|
|
55
|
+
return this.fallbackLocale;
|
|
56
|
+
}
|
|
57
|
+
getRoute() {
|
|
58
|
+
return this.currentRoute;
|
|
59
|
+
}
|
|
60
|
+
async loadTranslations(e) {
|
|
61
|
+
let t = e || this.translationDir;
|
|
62
|
+
if (!t) {
|
|
63
|
+
console.warn("[i18n-node] No translation directory specified");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
let { root: n, routes: i } = await u(t, this.disablePageLocales);
|
|
67
|
+
for (let [e, t] of Object.entries(n)) this.helper.mergeTranslation(e, "index", t, !0);
|
|
68
|
+
for (let [e, t] of Object.entries(i)) for (let [n, i] of Object.entries(t)) {
|
|
69
|
+
let t = this.helper.getCache(n, "index");
|
|
70
|
+
this.helper.loadPageTranslations(n, e, r(t, i));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async reload() {
|
|
74
|
+
this.helper.clearCache(), await this.loadTranslations(), console.log("[i18n-node] Cache cleared and translations reloaded.");
|
|
75
|
+
}
|
|
76
|
+
addTranslations(e, t, n = !0) {
|
|
77
|
+
super.loadTranslationsCore(e, t, n);
|
|
78
|
+
}
|
|
79
|
+
addRouteTranslations(e, t, n, r = !0) {
|
|
80
|
+
super.loadRouteTranslationsCore(e, t, n, r);
|
|
81
|
+
}
|
|
82
|
+
hasTranslation(e) {
|
|
83
|
+
return this.helper.hasTranslation(this.locale, e);
|
|
84
|
+
}
|
|
85
|
+
clear() {
|
|
86
|
+
super.clearCache();
|
|
87
|
+
}
|
|
99
88
|
};
|
|
89
|
+
function p(e) {
|
|
90
|
+
return new f(e);
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
export { t as FormatService, f as I18n, p as createI18n, n as interpolate, d as loadRootTranslations, u as loadTranslations };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/node",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.15",
|
|
4
4
|
"description": "Node.js utilities for loading translations and locale data in Nuxt I18n Micro.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -45,16 +45,16 @@
|
|
|
45
45
|
"access": "public"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@i18n-micro/
|
|
49
|
-
"@i18n-micro/
|
|
50
|
-
"@i18n-micro/
|
|
48
|
+
"@i18n-micro/types": "^1.2.14",
|
|
49
|
+
"@i18n-micro/utils": "^1.0.22",
|
|
50
|
+
"@i18n-micro/core": "^1.3.17"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/node": "^20.19.26",
|
|
54
|
-
"publint": "^0.3.
|
|
55
|
-
"vite": "^
|
|
56
|
-
"vite-plugin-dts": "^
|
|
57
|
-
"vitest": "^4.1.
|
|
54
|
+
"publint": "^0.3.24",
|
|
55
|
+
"vite": "^8.2.2",
|
|
56
|
+
"vite-plugin-dts": "^5.1.0",
|
|
57
|
+
"vitest": "^4.1.11"
|
|
58
58
|
},
|
|
59
59
|
"engines": {
|
|
60
60
|
"node": ">=20.0.0"
|