@i18n-micro/node 1.2.1 → 1.2.2

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 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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@i18n-micro/core"),f=require("@i18n-micro/utils/parse-path"),h=require("node:fs/promises"),c=require("node:path");async function d(r,e=!1){const t={root:{},routes:{}};try{const a=await h.readdir(r,{recursive:!0,withFileTypes:!0});for(const o of a){if(!o.isFile()||!o.name.endsWith(".json"))continue;const n=c.join(o.path,o.name),l=c.relative(r,n).split(c.sep).join("/");try{const s=await h.readFile(n,"utf-8"),i=JSON.parse(s);f.storeLoadedTranslationFile(t,l,i,e)}catch(s){console.error(`Failed to load translation file ${n}:`,s)}}}catch(a){a&&typeof a=="object"&&"code"in a&&a.code!=="ENOENT"&&console.error(`Failed to read directory ${r}:`,a)}return t}async function T(r,e=!1){const{root:t}=await d(r,e);return t}class g extends u.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:o}=await d(t,this.disablePageLocales);for(const[n,l]of Object.entries(a))this.helper.mergeTranslation(n,"index",l,!0);for(const[n,l]of Object.entries(o))for(const[s,i]of Object.entries(l)){const p=this.helper.getCache(s,"index");this.helper.loadPageTranslations(s,n,f.mergeRouteTranslationsWithRoot(p,i))}}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,o=!0){super.loadRouteTranslationsCore(e,t,a,o)}hasTranslation(e){return this.helper.hasTranslation(this.locale,e)}clear(){super.clearCache()}}function b(r){return new g(r)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>u.FormatService});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>u.interpolate});exports.I18n=g;exports.createI18n=b;exports.loadRootTranslations=T;exports.loadTranslations=d;
package/dist/index.d.cts CHANGED
@@ -7,6 +7,7 @@ import { Locale } from '@i18n-micro/types';
7
7
  import { LocaleCode } from '@i18n-micro/types';
8
8
  import { Params } from '@i18n-micro/types';
9
9
  import { PluralFunc } from '@i18n-micro/types';
10
+ import { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
10
11
  import { TranslationKey } from '@i18n-micro/types';
11
12
  import { Translations } from '@i18n-micro/types';
12
13
 
@@ -49,9 +50,7 @@ export declare interface I18nOptions {
49
50
 
50
51
  export { interpolate }
51
52
 
52
- export declare interface LoadedTranslations {
53
- root: Record<string, Translations>;
54
- routes: Record<string, Record<string, Translations>>;
53
+ export declare interface LoadedTranslations extends TranslationFileBuckets<Translations> {
55
54
  }
56
55
 
57
56
  /**
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ import { Locale } from '@i18n-micro/types';
7
7
  import { LocaleCode } from '@i18n-micro/types';
8
8
  import { Params } from '@i18n-micro/types';
9
9
  import { PluralFunc } from '@i18n-micro/types';
10
+ import { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
10
11
  import { TranslationKey } from '@i18n-micro/types';
11
12
  import { Translations } from '@i18n-micro/types';
12
13
 
@@ -49,9 +50,7 @@ export declare interface I18nOptions {
49
50
 
50
51
  export { interpolate }
51
52
 
52
- export declare interface LoadedTranslations {
53
- root: Record<string, Translations>;
54
- routes: Record<string, Record<string, Translations>>;
53
+ export declare interface LoadedTranslations extends TranslationFileBuckets<Translations> {
55
54
  }
56
55
 
57
56
  /**
package/dist/index.mjs CHANGED
@@ -1,42 +1,36 @@
1
- import { BaseI18n as g } from "@i18n-micro/core";
2
- import { FormatService as v, interpolate as O } from "@i18n-micro/core";
1
+ import { BaseI18n as d } from "@i18n-micro/core";
2
+ import { FormatService as N, interpolate as O } from "@i18n-micro/core";
3
+ import { storeLoadedTranslationFile as h, mergeRouteTranslationsWithRoot as f } from "@i18n-micro/utils/parse-path";
3
4
  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
- async function f(o, e = !1) {
5
+ import { join as g, relative as T, sep as b } from "node:path";
6
+ async function c(r, e = !1) {
6
7
  const t = {
7
8
  root: {},
8
9
  routes: {}
9
10
  };
10
11
  try {
11
- const a = await m(o, { recursive: !0, withFileTypes: !0 });
12
- for (const r of a) {
13
- if (!r.isFile() || !r.name.endsWith(".json"))
12
+ const a = await m(r, { recursive: !0, withFileTypes: !0 });
13
+ for (const o of a) {
14
+ if (!o.isFile() || !o.name.endsWith(".json"))
14
15
  continue;
15
- const s = T(r.path, r.name), n = b(o, s).split(y), i = w(r.name, ".json");
16
+ const s = g(o.path, o.name), l = T(r, s).split(b).join("/");
16
17
  try {
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
- }
24
- } else
25
- t.root[i] = u;
26
- } catch (l) {
27
- console.error(`Failed to load translation file ${s}:`, l);
18
+ const n = await p(s, "utf-8"), i = JSON.parse(n);
19
+ h(t, l, i, e);
20
+ } catch (n) {
21
+ console.error(`Failed to load translation file ${s}:`, n);
28
22
  }
29
23
  }
30
24
  } catch (a) {
31
- a && typeof a == "object" && "code" in a && a.code !== "ENOENT" && console.error(`Failed to read directory ${o}:`, a);
25
+ a && typeof a == "object" && "code" in a && a.code !== "ENOENT" && console.error(`Failed to read directory ${r}:`, a);
32
26
  }
33
27
  return t;
34
28
  }
35
- async function F(o, e = !1) {
36
- const { root: t } = await f(o, e);
29
+ async function j(r, e = !1) {
30
+ const { root: t } = await c(r, e);
37
31
  return t;
38
32
  }
39
- class L extends g {
33
+ class y extends d {
40
34
  constructor(e) {
41
35
  const t = {
42
36
  translations: /* @__PURE__ */ new Map()
@@ -66,13 +60,13 @@ class L extends g {
66
60
  console.warn("[i18n-node] No translation directory specified");
67
61
  return;
68
62
  }
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);
63
+ const { root: a, routes: o } = await c(t, this.disablePageLocales);
64
+ for (const [s, l] of Object.entries(a))
65
+ this.helper.mergeTranslation(s, "index", l, !0);
66
+ for (const [s, l] of Object.entries(o))
67
+ for (const [n, i] of Object.entries(l)) {
68
+ const u = this.helper.getCache(n, "index");
69
+ this.helper.loadPageTranslations(n, s, f(u, i));
76
70
  }
77
71
  }
78
72
  async reload() {
@@ -81,8 +75,8 @@ class L extends g {
81
75
  addTranslations(e, t, a = !0) {
82
76
  super.loadTranslationsCore(e, t, a);
83
77
  }
84
- addRouteTranslations(e, t, a, r = !0) {
85
- super.loadRouteTranslationsCore(e, t, a, r);
78
+ addRouteTranslations(e, t, a, o = !0) {
79
+ super.loadRouteTranslationsCore(e, t, a, o);
86
80
  }
87
81
  hasTranslation(e) {
88
82
  return this.helper.hasTranslation(this.locale, e);
@@ -91,14 +85,14 @@ class L extends g {
91
85
  super.clearCache();
92
86
  }
93
87
  }
94
- function x(o) {
95
- return new L(o);
88
+ function x(r) {
89
+ return new y(r);
96
90
  }
97
91
  export {
98
- v as FormatService,
99
- L as I18n,
92
+ N as FormatService,
93
+ y as I18n,
100
94
  x as createI18n,
101
95
  O as interpolate,
102
- F as loadRootTranslations,
103
- f as loadTranslations
96
+ j as loadRootTranslations,
97
+ c as loadTranslations
104
98
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@i18n-micro/node",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Node.js utilities for loading translations and locale data in Nuxt I18n Micro.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -49,7 +49,8 @@
49
49
  ],
50
50
  "dependencies": {
51
51
  "@i18n-micro/core": "1.3.1",
52
- "@i18n-micro/types": "1.2.2"
52
+ "@i18n-micro/utils": "1.0.0",
53
+ "@i18n-micro/types": "1.2.3"
53
54
  },
54
55
  "devDependencies": {
55
56
  "@types/jest": "^29.5.14",