@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 CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("node:fs/promises"),c=require("node:path"),h=require("@i18n-micro/core");async function f(o,e=!1){const a={global:{},routes:{}};try{const t=await p.readdir(o,{recursive:!0,withFileTypes:!0});for(const r of t){if(!r.isFile()||!r.name.endsWith(".json"))continue;const s=c.join(r.path,r.name),n=c.relative(o,s).split(c.sep),i=c.basename(r.name,".json");try{const u=await p.readFile(s,"utf-8"),g=JSON.parse(u);if(!e&&n[0]==="pages"&&n.length>=2){const b=n.slice(1,-1);if(b.length>0){const d=b.join("-");a.routes[d]||(a.routes[d]={}),a.routes[d][i]=g}}else a.global[i]=g}catch(u){console.error(`Failed to load translation file ${s}:`,u)}}}catch(t){t&&typeof t=="object"&&"code"in t&&t.code!=="ENOENT"&&console.error(`Failed to read directory ${o}:`,t)}return a}async function T(o,e=!1){const{global:a}=await f(o,e);return a}class m extends h.BaseI18n{constructor(e){const a={translations:new Map};super({storage:a,plural:e.plural,missingWarn:e.missingWarn,missingHandler:e.missingHandler}),this.currentRoute="general",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 a=e||this.translationDir;if(!a){console.warn("[i18n-node] No translation directory specified");return}const{global:t,routes:r}=await f(a,this.disablePageLocales);for(const[s,l]of Object.entries(t))this.helper.mergeGlobalTranslation(s,l,!0);for(const[s,l]of Object.entries(r))for(const[n,i]of Object.entries(l))this.helper.loadPageTranslations(n,s,i)}async reload(){this.helper.clearCache(),await this.loadTranslations(),console.log("[i18n-node] Cache cleared and translations reloaded.")}addTranslations(e,a,t=!0){super.loadTranslationsCore(e,a,t)}addRouteTranslations(e,a,t,r=!0){super.loadRouteTranslationsCore(e,a,t,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:()=>h.FormatService});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>h.interpolate});exports.I18n=m;exports.createI18n=y;exports.loadGlobalTranslations=T;exports.loadTranslations=f;
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
- global: Record<string, Translations>;
53
+ root: Record<string, Translations>;
54
54
  routes: Record<string, Record<string, Translations>>;
55
55
  }
56
56
 
57
57
  /**
58
- * Load only global translations from a directory
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 global translations
60
+ * @param disablePageLocales - If true, ignores pages/ folder and treats all files as root translations
61
61
  */
62
- export declare function loadGlobalTranslations(dir: string, disablePageLocales?: boolean): Promise<Record<string, Translations>>;
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 { readdir as g, readFile as m } from "node:fs/promises";
2
- import { join as p, relative as b, sep as T, basename as y } from "node:path";
3
- import { BaseI18n as w } from "@i18n-micro/core";
4
- import { FormatService as O, interpolate as k } from "@i18n-micro/core";
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 a = {
7
- global: {},
6
+ const t = {
7
+ root: {},
8
8
  routes: {}
9
9
  };
10
10
  try {
11
- const t = await g(o, { recursive: !0, withFileTypes: !0 });
12
- for (const r of t) {
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 = p(r.path, r.name), l = b(o, s).split(T), i = y(r.name, ".json");
15
+ const s = T(r.path, r.name), n = b(o, s).split(y), i = w(r.name, ".json");
16
16
  try {
17
- const c = await m(s, "utf-8"), h = JSON.parse(c);
18
- if (!e && l[0] === "pages" && l.length >= 2) {
19
- const d = l.slice(1, -1);
20
- if (d.length > 0) {
21
- const u = d.join("-");
22
- a.routes[u] || (a.routes[u] = {}), a.routes[u][i] = h;
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
- a.global[i] = h;
26
- } catch (c) {
27
- console.error(`Failed to load translation file ${s}:`, c);
25
+ t.root[i] = u;
26
+ } catch (l) {
27
+ console.error(`Failed to load translation file ${s}:`, l);
28
28
  }
29
29
  }
30
- } catch (t) {
31
- t && typeof t == "object" && "code" in t && t.code !== "ENOENT" && console.error(`Failed to read directory ${o}:`, t);
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 a;
33
+ return t;
34
34
  }
35
- async function R(o, e = !1) {
36
- const { global: a } = await f(o, e);
37
- return a;
35
+ async function F(o, e = !1) {
36
+ const { root: t } = await f(o, e);
37
+ return t;
38
38
  }
39
- class L extends w {
39
+ class L extends g {
40
40
  constructor(e) {
41
- const a = {
41
+ const t = {
42
42
  translations: /* @__PURE__ */ new Map()
43
43
  };
44
44
  super({
45
- storage: a,
45
+ storage: t,
46
46
  plural: e.plural,
47
47
  missingWarn: e.missingWarn,
48
48
  missingHandler: e.missingHandler
49
- }), this.currentRoute = "general", this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1;
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 a = e || this.translationDir;
65
- if (!a) {
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 { global: t, routes: r } = await f(a, this.disablePageLocales);
70
- for (const [s, n] of Object.entries(t))
71
- this.helper.mergeGlobalTranslation(s, n, !0);
72
- for (const [s, n] of Object.entries(r))
73
- for (const [l, i] of Object.entries(n))
74
- this.helper.loadPageTranslations(l, s, i);
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, a, t = !0) {
80
- super.loadTranslationsCore(e, a, t);
81
+ addTranslations(e, t, a = !0) {
82
+ super.loadTranslationsCore(e, t, a);
81
83
  }
82
- addRouteTranslations(e, a, t, r = !0) {
83
- super.loadRouteTranslationsCore(e, a, t, r);
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 N(o) {
94
+ function x(o) {
93
95
  return new L(o);
94
96
  }
95
97
  export {
96
- O as FormatService,
98
+ v as FormatService,
97
99
  L as I18n,
98
- N as createI18n,
99
- k as interpolate,
100
- R as loadGlobalTranslations,
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.1.0",
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/types": "1.1.0",
26
- "@i18n-micro/core": "1.1.0"
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",