@i18n-micro/node 1.1.0 → 1.2.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.cjs +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +47 -45
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@i18n-micro/core"),b=require("node:fs/promises"),d=require("node:path");async function g(o,e=!1){const t={root:{},routes:{}};try{const a=await b.readdir(o,{recursive:!0,withFileTypes:!0});for(const r of a){if(!r.isFile()||!r.name.endsWith(".json"))continue;const s=d.join(r.path,r.name),n=d.relative(o,s).split(d.sep),i=d.basename(r.name,".json");try{const l=await b.readFile(s,"utf-8"),u=JSON.parse(l);if(!e&&n[0]==="pages"&&n.length>=2){const p=n.slice(1,-1);if(p.length>0){const h=p.join("-");t.routes[h]||(t.routes[h]={}),t.routes[h][i]=u}}else t.root[i]=u}catch(l){console.error(`Failed to load translation file ${s}:`,l)}}}catch(a){a&&typeof a=="object"&&"code"in a&&a.code!=="ENOENT"&&console.error(`Failed to read directory ${o}:`,a)}return t}async function T(o,e=!1){const{root:t}=await g(o,e);return t}class m extends f.BaseI18n{constructor(e){const t={translations:new Map};super({storage:t,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){const t=e||this.translationDir;if(!t){console.warn("[i18n-node] No translation directory specified");return}const{root:a,routes:r}=await g(t,this.disablePageLocales);for(const[s,c]of Object.entries(a))this.helper.mergeTranslation(s,"index",c,!0);for(const[s,c]of Object.entries(r))for(const[n,i]of Object.entries(c)){const l=this.helper.getCache(n,"index"),u=l?{...l,...i}:i;this.helper.loadPageTranslations(n,s,u)}}async reload(){this.helper.clearCache(),await this.loadTranslations(),console.log("[i18n-node] Cache cleared and translations reloaded.")}addTranslations(e,t,a=!0){super.loadTranslationsCore(e,t,a)}addRouteTranslations(e,t,a,r=!0){super.loadRouteTranslationsCore(e,t,a,r)}hasTranslation(e){return this.helper.hasTranslation(this.locale,e)}clear(){super.clearCache()}}function y(o){return new m(o)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>f.FormatService});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>f.interpolate});exports.I18n=m;exports.createI18n=y;exports.loadRootTranslations=T;exports.loadTranslations=g;
|
package/dist/index.d.ts
CHANGED
|
@@ -50,16 +50,16 @@ export declare interface I18nOptions {
|
|
|
50
50
|
export { interpolate }
|
|
51
51
|
|
|
52
52
|
export declare interface LoadedTranslations {
|
|
53
|
-
|
|
53
|
+
root: Record<string, Translations>;
|
|
54
54
|
routes: Record<string, Record<string, Translations>>;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
* Load only
|
|
58
|
+
* Load only root-level translations from a directory
|
|
59
59
|
* @param dir - Directory path containing translation files
|
|
60
|
-
* @param disablePageLocales - If true, ignores pages/ folder and treats all files as
|
|
60
|
+
* @param disablePageLocales - If true, ignores pages/ folder and treats all files as root translations
|
|
61
61
|
*/
|
|
62
|
-
export declare function
|
|
62
|
+
export declare function loadRootTranslations(dir: string, disablePageLocales?: boolean): Promise<Record<string, Translations>>;
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* Load translations from a directory
|
package/dist/index.mjs
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { BaseI18n as g } from "@i18n-micro/core";
|
|
2
|
+
import { FormatService as v, interpolate as O } from "@i18n-micro/core";
|
|
3
|
+
import { readdir as m, readFile as p } from "node:fs/promises";
|
|
4
|
+
import { join as T, relative as b, sep as y, basename as w } from "node:path";
|
|
5
5
|
async function f(o, e = !1) {
|
|
6
|
-
const
|
|
7
|
-
|
|
6
|
+
const t = {
|
|
7
|
+
root: {},
|
|
8
8
|
routes: {}
|
|
9
9
|
};
|
|
10
10
|
try {
|
|
11
|
-
const
|
|
12
|
-
for (const r of
|
|
11
|
+
const a = await m(o, { recursive: !0, withFileTypes: !0 });
|
|
12
|
+
for (const r of a) {
|
|
13
13
|
if (!r.isFile() || !r.name.endsWith(".json"))
|
|
14
14
|
continue;
|
|
15
|
-
const s =
|
|
15
|
+
const s = T(r.path, r.name), n = b(o, s).split(y), i = w(r.name, ".json");
|
|
16
16
|
try {
|
|
17
|
-
const
|
|
18
|
-
if (!e &&
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
21
|
-
const
|
|
22
|
-
|
|
17
|
+
const l = await p(s, "utf-8"), u = JSON.parse(l);
|
|
18
|
+
if (!e && n[0] === "pages" && n.length >= 2) {
|
|
19
|
+
const h = n.slice(1, -1);
|
|
20
|
+
if (h.length > 0) {
|
|
21
|
+
const d = h.join("-");
|
|
22
|
+
t.routes[d] || (t.routes[d] = {}), t.routes[d][i] = u;
|
|
23
23
|
}
|
|
24
24
|
} else
|
|
25
|
-
|
|
26
|
-
} catch (
|
|
27
|
-
console.error(`Failed to load translation file ${s}:`,
|
|
25
|
+
t.root[i] = u;
|
|
26
|
+
} catch (l) {
|
|
27
|
+
console.error(`Failed to load translation file ${s}:`, l);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
} catch (
|
|
31
|
-
|
|
30
|
+
} catch (a) {
|
|
31
|
+
a && typeof a == "object" && "code" in a && a.code !== "ENOENT" && console.error(`Failed to read directory ${o}:`, a);
|
|
32
32
|
}
|
|
33
|
-
return
|
|
33
|
+
return t;
|
|
34
34
|
}
|
|
35
|
-
async function
|
|
36
|
-
const {
|
|
37
|
-
return
|
|
35
|
+
async function F(o, e = !1) {
|
|
36
|
+
const { root: t } = await f(o, e);
|
|
37
|
+
return t;
|
|
38
38
|
}
|
|
39
|
-
class L extends
|
|
39
|
+
class L extends g {
|
|
40
40
|
constructor(e) {
|
|
41
|
-
const
|
|
41
|
+
const t = {
|
|
42
42
|
translations: /* @__PURE__ */ new Map()
|
|
43
43
|
};
|
|
44
44
|
super({
|
|
45
|
-
storage:
|
|
45
|
+
storage: t,
|
|
46
46
|
plural: e.plural,
|
|
47
47
|
missingWarn: e.missingWarn,
|
|
48
48
|
missingHandler: e.missingHandler
|
|
49
|
-
}), this.currentRoute = "
|
|
49
|
+
}), this.currentRoute = "index", this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1;
|
|
50
50
|
}
|
|
51
51
|
setRoute(e) {
|
|
52
52
|
this.currentRoute = e;
|
|
@@ -61,26 +61,28 @@ class L extends w {
|
|
|
61
61
|
return this.currentRoute;
|
|
62
62
|
}
|
|
63
63
|
async loadTranslations(e) {
|
|
64
|
-
const
|
|
65
|
-
if (!
|
|
64
|
+
const t = e || this.translationDir;
|
|
65
|
+
if (!t) {
|
|
66
66
|
console.warn("[i18n-node] No translation directory specified");
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
|
-
const {
|
|
70
|
-
for (const [s,
|
|
71
|
-
this.helper.
|
|
72
|
-
for (const [s,
|
|
73
|
-
for (const [
|
|
74
|
-
this.helper.
|
|
69
|
+
const { root: a, routes: r } = await f(t, this.disablePageLocales);
|
|
70
|
+
for (const [s, c] of Object.entries(a))
|
|
71
|
+
this.helper.mergeTranslation(s, "index", c, !0);
|
|
72
|
+
for (const [s, c] of Object.entries(r))
|
|
73
|
+
for (const [n, i] of Object.entries(c)) {
|
|
74
|
+
const l = this.helper.getCache(n, "index"), u = l ? { ...l, ...i } : i;
|
|
75
|
+
this.helper.loadPageTranslations(n, s, u);
|
|
76
|
+
}
|
|
75
77
|
}
|
|
76
78
|
async reload() {
|
|
77
79
|
this.helper.clearCache(), await this.loadTranslations(), console.log("[i18n-node] Cache cleared and translations reloaded.");
|
|
78
80
|
}
|
|
79
|
-
addTranslations(e,
|
|
80
|
-
super.loadTranslationsCore(e,
|
|
81
|
+
addTranslations(e, t, a = !0) {
|
|
82
|
+
super.loadTranslationsCore(e, t, a);
|
|
81
83
|
}
|
|
82
|
-
addRouteTranslations(e,
|
|
83
|
-
super.loadRouteTranslationsCore(e,
|
|
84
|
+
addRouteTranslations(e, t, a, r = !0) {
|
|
85
|
+
super.loadRouteTranslationsCore(e, t, a, r);
|
|
84
86
|
}
|
|
85
87
|
hasTranslation(e) {
|
|
86
88
|
return this.helper.hasTranslation(this.locale, e);
|
|
@@ -89,14 +91,14 @@ class L extends w {
|
|
|
89
91
|
super.clearCache();
|
|
90
92
|
}
|
|
91
93
|
}
|
|
92
|
-
function
|
|
94
|
+
function x(o) {
|
|
93
95
|
return new L(o);
|
|
94
96
|
}
|
|
95
97
|
export {
|
|
96
|
-
|
|
98
|
+
v as FormatService,
|
|
97
99
|
L as I18n,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
x as createI18n,
|
|
101
|
+
O as interpolate,
|
|
102
|
+
F as loadRootTranslations,
|
|
101
103
|
f as loadTranslations
|
|
102
104
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@i18n-micro/
|
|
26
|
-
"@i18n-micro/
|
|
25
|
+
"@i18n-micro/core": "1.2.0",
|
|
26
|
+
"@i18n-micro/types": "1.1.5"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^20.19.26",
|