@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 CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@i18n-micro/core"),g=require("@i18n-micro/utils/parse-path"),f=require("node:fs/promises"),u=require("node:path");async function h(o,e=!1){const t={root:{},routes:{}};try{const s=(await f.readdir(o,{recursive:!0,withFileTypes:!0})).filter(a=>a.isFile()&&a.name.endsWith(".json")),l=await Promise.all(s.map(async a=>{const n=u.join(a.path,a.name),c=u.relative(o,n).split(u.sep).join("/");try{const i=await f.readFile(n,"utf-8"),T=JSON.parse(i);return{relativePath:c,translations:T}}catch(i){return console.error(`Failed to load translation file ${n}:`,i),null}}));for(const a of l)a&&g.storeLoadedTranslationFile(t,a.relativePath,a.translations,e)}catch(r){r&&typeof r=="object"&&"code"in r&&r.code!=="ENOENT"&&console.error(`Failed to read directory ${o}:`,r)}return t}async function b(o,e=!1){const{root:t}=await h(o,e);return t}class p extends d.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:r,routes:s}=await h(t,this.disablePageLocales);for(const[l,a]of Object.entries(r))this.helper.mergeTranslation(l,"index",a,!0);for(const[l,a]of Object.entries(s))for(const[n,c]of Object.entries(a)){const i=this.helper.getCache(n,"index");this.helper.loadPageTranslations(n,l,g.mergeRouteTranslationsWithRoot(i,c))}}async reload(){this.helper.clearCache(),await this.loadTranslations(),console.log("[i18n-node] Cache cleared and translations reloaded.")}addTranslations(e,t,r=!0){super.loadTranslationsCore(e,t,r)}addRouteTranslations(e,t,r,s=!0){super.loadRouteTranslationsCore(e,t,r,s)}hasTranslation(e){return this.helper.hasTranslation(this.locale,e)}clear(){super.clearCache()}}function m(o){return new p(o)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>d.FormatService});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>d.interpolate});exports.I18n=p;exports.createI18n=m;exports.loadRootTranslations=b;exports.loadTranslations=h;
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 h } from "@i18n-micro/core";
2
- import { FormatService as N, interpolate as O } from "@i18n-micro/core";
3
- import { storeLoadedTranslationFile as f, mergeRouteTranslationsWithRoot as m } from "@i18n-micro/utils/parse-path";
4
- import { readdir as p, readFile as g } from "node:fs/promises";
5
- import { join as T, relative as b, sep as y } from "node:path";
6
- async function u(o, e = !1) {
7
- const t = {
8
- root: {},
9
- routes: {}
10
- };
11
- try {
12
- const n = (await p(o, { recursive: !0, withFileTypes: !0 })).filter((a) => a.isFile() && a.name.endsWith(".json")), l = await Promise.all(
13
- n.map(async (a) => {
14
- const s = T(a.path, a.name), c = b(o, s).split(y).join("/");
15
- try {
16
- const i = await g(s, "utf-8"), d = JSON.parse(i);
17
- return { relativePath: c, translations: d };
18
- } catch (i) {
19
- return console.error(`Failed to load translation file ${s}:`, i), null;
20
- }
21
- })
22
- );
23
- for (const a of l)
24
- a && f(t, a.relativePath, a.translations, e);
25
- } catch (r) {
26
- r && typeof r == "object" && "code" in r && r.code !== "ENOENT" && console.error(`Failed to read directory ${o}:`, r);
27
- }
28
- return t;
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 P(o, e = !1) {
31
- const { root: t } = await u(o, e);
32
- return t;
33
+ async function d(e, t = !1) {
34
+ let { root: n } = await u(e, t);
35
+ return n;
33
36
  }
34
- class w extends h {
35
- constructor(e) {
36
- const t = {
37
- translations: /* @__PURE__ */ new Map()
38
- };
39
- super({
40
- storage: t,
41
- plural: e.plural,
42
- missingWarn: e.missingWarn,
43
- missingHandler: e.missingHandler
44
- }), this.currentRoute = "index", this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1;
45
- }
46
- setRoute(e) {
47
- this.currentRoute = e;
48
- }
49
- getLocale() {
50
- return this.locale;
51
- }
52
- getFallbackLocale() {
53
- return this.fallbackLocale;
54
- }
55
- getRoute() {
56
- return this.currentRoute;
57
- }
58
- async loadTranslations(e) {
59
- const t = e || this.translationDir;
60
- if (!t) {
61
- console.warn("[i18n-node] No translation directory specified");
62
- return;
63
- }
64
- const { root: r, routes: n } = await u(t, this.disablePageLocales);
65
- for (const [l, a] of Object.entries(r))
66
- this.helper.mergeTranslation(l, "index", a, !0);
67
- for (const [l, a] of Object.entries(n))
68
- for (const [s, c] of Object.entries(a)) {
69
- const i = this.helper.getCache(s, "index");
70
- this.helper.loadPageTranslations(s, l, m(i, c));
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, r = !0) {
77
- super.loadTranslationsCore(e, t, r);
78
- }
79
- addRouteTranslations(e, t, r, n = !0) {
80
- super.loadRouteTranslationsCore(e, t, r, n);
81
- }
82
- hasTranslation(e) {
83
- return this.helper.hasTranslation(this.locale, e);
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.9",
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/core": "^1.3.15",
49
- "@i18n-micro/types": "^1.2.12",
50
- "@i18n-micro/utils": "^1.0.15"
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.17",
55
- "vite": "^7.3.6",
56
- "vite-plugin-dts": "^4.5.4",
57
- "vitest": "^4.1.10"
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"