@i18n-micro/node 1.0.0 → 1.0.1

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 b=require("node:fs/promises"),f=require("node:path"),c=require("@i18n-micro/core");async function g(l,e=!1){const t={global:{},routes:{}};try{const r=await b.readdir(l,{recursive:!0,withFileTypes:!0});for(const a of r){if(!a.isFile()||!a.name.endsWith(".json"))continue;const s=f.join(a.path,a.name),o=f.relative(l,s).split(f.sep),i=f.basename(a.name,".json");try{const h=await b.readFile(s,"utf-8"),u=JSON.parse(h);if(!e&&o[0]==="pages"&&o.length>=2){const m=o.slice(1,-1);if(m.length>0){const d=m.join("-");t.routes[d]||(t.routes[d]={}),t.routes[d][i]=u}}else t.global[i]=u}catch(h){console.error(`Failed to load translation file ${s}:`,h)}}}catch(r){r&&typeof r=="object"&&"code"in r&&r.code!=="ENOENT"&&console.error(`Failed to read directory ${l}:`,r)}return t}async function T(l,e=!1){const{global:t}=await g(l,e);return t}class p{constructor(e){this.currentRoute="general",this.cache={generalLocaleCache:{},routeLocaleCache:{},dynamicTranslationsCaches:[],serverTranslationCache:{}},this.helper=c.useTranslationHelper(this.cache),this.formatter=new c.FormatService,this.locale=e.locale,this.fallbackLocale=e.fallbackLocale||e.locale,this.translationDir=e.translationDir,this.disablePageLocales=e.disablePageLocales??!1,this.pluralFunc=e.plural||c.defaultPlural,this.missingWarn=e.missingWarn??!1,this.missingHandler=e.missingHandler}setRoute(e){this.currentRoute=e}getRoute(){return this.currentRoute}t(e,t,r,a){if(!e)return"";const s=a||this.currentRoute;let n=this.helper.getTranslation(this.locale,s,e);return!n&&this.locale!==this.fallbackLocale&&(n=this.helper.getTranslation(this.fallbackLocale,s,e)),n||(this.missingHandler?this.missingHandler(this.locale,e,s):this.missingWarn&&console.warn(`[i18n] Translation key '${e}' not found for locale '${this.locale}' (route: '${s}').`),n=r===void 0?e:r||e),typeof n=="string"&&t?c.interpolate(n,t):n||e}ts(e,t,r,a){return this.t(e,t,r,a)?.toString()??r??e}tc(e,t,r){const{count:a,...s}=typeof t=="number"?{count:t}:t;if(a===void 0)return r??e;const n=(i,h,u)=>this.t(i,h,u);return this.pluralFunc(e,Number.parseInt(a.toString()),s,this.locale,n)??r??e}tn(e,t){return this.formatter.formatNumber(e,this.locale,t)}td(e,t){return this.formatter.formatDate(e,this.locale,t)}tdr(e,t){return this.formatter.formatRelativeTime(e,this.locale,t)}async loadTranslations(e){const t=e||this.translationDir;if(!t){console.warn("[i18n-node] No translation directory specified");return}const{global:r,routes:a}=await g(t,this.disablePageLocales);for(const[s,n]of Object.entries(r))this.helper.mergeGlobalTranslation(s,n,!0);for(const[s,n]of Object.entries(a))for(const[o,i]of Object.entries(n))await this.helper.loadPageTranslations(o,s,i)}async reload(){this.helper.clearCache(),await this.loadTranslations(),console.log("[i18n-node] Cache cleared and translations reloaded.")}addTranslations(e,t,r=!0){r?this.helper.mergeGlobalTranslation(e,t,!0):this.helper.loadTranslations(e,t)}addRouteTranslations(e,t,r,a=!0){a?this.helper.mergeTranslation(e,t,r,!0):this.helper.loadPageTranslations(e,t,r)}hasTranslation(e){return this.helper.hasTranslation(this.locale,e)}clear(){this.helper.clearCache()}}function v(l){return new p(l)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>c.FormatService});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>c.interpolate});exports.I18n=p;exports.createI18n=v;exports.loadGlobalTranslations=T;exports.loadTranslations=g;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=require("node:fs/promises"),c=require("node:path"),d=require("@i18n-micro/core");async function f(o,e=!1){const a={global:{},routes:{}};try{const t=await T.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 T.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 h=b.join("-");a.routes[h]||(a.routes[h]={}),a.routes[h][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 p(o,e=!1){const{global:a}=await f(o,e);return a}class m extends d.BaseI18n{constructor(e){const a={generalLocaleCache:{},routeLocaleCache:{},dynamicTranslationsCaches:[],serverTranslationCache:{}};super({cache:a,plural:e.plural,missingWarn:e.missingWarn,missingHandler:e.missingHandler}),this.currentRoute="general",this.cache={generalLocaleCache:{},routeLocaleCache:{},dynamicTranslationsCaches:[],serverTranslationCache:{}},this.cache=a,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))await 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:()=>d.FormatService});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>d.interpolate});exports.I18n=m;exports.createI18n=y;exports.loadGlobalTranslations=p;exports.loadTranslations=f;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { BaseI18n } from '@i18n-micro/core';
1
2
  import { CleanTranslation } from '@i18n-micro/types';
2
3
  import { FormatService } from '@i18n-micro/core';
3
4
  import { Getter } from '@i18n-micro/types';
@@ -26,7 +27,7 @@ export { Getter }
26
27
  * Provides a standalone i18n instance that uses the shared core logic from nuxt-i18n-micro.
27
28
  * Fully compatible with the file structure and caching mechanisms of the Nuxt module.
28
29
  */
29
- export declare class I18n {
30
+ export declare class I18n extends BaseI18n {
30
31
  locale: string;
31
32
  fallbackLocale: string;
32
33
  translationDir?: string;
@@ -38,52 +39,15 @@ export declare class I18n {
38
39
  */
39
40
  currentRoute: string;
40
41
  private cache;
41
- readonly helper: {
42
- hasCache(locale: string, page: string): boolean;
43
- getCache(locale: string, routeName: string): Map<string, unknown> | undefined;
44
- setCache(locale: string, routeName: string, cache: Map<string, Translations | unknown>): void;
45
- mergeTranslation(locale: string, routeName: string, newTranslations: Translations, force?: boolean): void;
46
- mergeGlobalTranslation(locale: string, newTranslations: Translations, force?: boolean): void;
47
- hasGeneralTranslation(locale: string): boolean;
48
- hasPageTranslation(locale: string, routeName: string): boolean;
49
- hasTranslation: (locale: string, key: TranslationKey) => boolean;
50
- getTranslation: <T = unknown>(locale: string, routeName: string, key: TranslationKey) => T | null;
51
- loadPageTranslations(locale: string, routeName: string, translations: Translations): Promise<void>;
52
- loadTranslations(locale: string, translations: Translations): Promise<void>;
53
- clearCache(): void;
54
- };
55
- private formatter;
56
- private pluralFunc;
57
- private missingWarn;
58
- private missingHandler?;
59
42
  constructor(options: I18nOptions);
60
43
  /**
61
44
  * Set the current route name context.
62
45
  * Useful when processing a specific page request in Node.
63
46
  */
64
47
  setRoute(routeName: string): void;
48
+ getLocale(): string;
49
+ getFallbackLocale(): string;
65
50
  getRoute(): string;
66
- /**
67
- * Get translation for a key
68
- *
69
- * Search order:
70
- * 1. Current Locale + Current Route (Specific)
71
- * 2. Current Locale + General (Global)
72
- * 3. Fallback Locale + Current Route
73
- * 4. Fallback Locale + General
74
- */
75
- t(key: TranslationKey, params?: Params, defaultValue?: string | null, routeName?: string): string;
76
- /**
77
- * Get translation as string
78
- */
79
- ts(key: TranslationKey, params?: Params, defaultValue?: string, routeName?: string): string;
80
- /**
81
- * Plural translation
82
- */
83
- tc(key: TranslationKey, count: number | Params, defaultValue?: string): string;
84
- tn(value: number, options?: Intl.NumberFormatOptions): string;
85
- td(value: Date | number | string, options?: Intl.DateTimeFormatOptions): string;
86
- tdr(value: Date | number | string, options?: Intl.RelativeTimeFormatOptions): string;
87
51
  /**
88
52
  * Load translations from directory.
89
53
  */
package/dist/index.mjs CHANGED
@@ -1,49 +1,60 @@
1
- import { readdir as m, readFile as g } from "node:fs/promises";
2
- import { join as p, relative as T, sep as b, basename as v } from "node:path";
3
- import { useTranslationHelper as w, FormatService as L, defaultPlural as P, interpolate as F } from "@i18n-micro/core";
4
- import { FormatService as $, interpolate as y } from "@i18n-micro/core";
5
- async function d(o, e = !1) {
6
- const t = {
1
+ import { readdir as g, readFile as m } from "node:fs/promises";
2
+ import { join as b, relative as p, sep as T, basename as C } from "node:path";
3
+ import { BaseI18n as L } from "@i18n-micro/core";
4
+ import { FormatService as O, interpolate as k } from "@i18n-micro/core";
5
+ async function f(o, e = !1) {
6
+ const a = {
7
7
  global: {},
8
8
  routes: {}
9
9
  };
10
10
  try {
11
- const r = await m(o, { recursive: !0, withFileTypes: !0 });
12
- for (const a of r) {
13
- if (!a.isFile() || !a.name.endsWith(".json"))
11
+ const t = await g(o, { recursive: !0, withFileTypes: !0 });
12
+ for (const r of t) {
13
+ if (!r.isFile() || !r.name.endsWith(".json"))
14
14
  continue;
15
- const s = p(a.path, a.name), n = T(o, s).split(b), i = v(a.name, ".json");
15
+ const s = b(r.path, r.name), l = p(o, s).split(T), i = C(r.name, ".json");
16
16
  try {
17
- const c = await g(s, "utf-8"), h = JSON.parse(c);
18
- if (!e && n[0] === "pages" && n.length >= 2) {
19
- const f = n.slice(1, -1);
20
- if (f.length > 0) {
21
- const u = f.join("-");
22
- t.routes[u] || (t.routes[u] = {}), t.routes[u][i] = h;
17
+ const c = await m(s, "utf-8"), u = 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 h = d.join("-");
22
+ a.routes[h] || (a.routes[h] = {}), a.routes[h][i] = u;
23
23
  }
24
24
  } else
25
- t.global[i] = h;
25
+ a.global[i] = u;
26
26
  } catch (c) {
27
27
  console.error(`Failed to load translation file ${s}:`, c);
28
28
  }
29
29
  }
30
- } catch (r) {
31
- r && typeof r == "object" && "code" in r && r.code !== "ENOENT" && console.error(`Failed to read directory ${o}:`, r);
30
+ } catch (t) {
31
+ t && typeof t == "object" && "code" in t && t.code !== "ENOENT" && console.error(`Failed to read directory ${o}:`, t);
32
32
  }
33
- return t;
33
+ return a;
34
34
  }
35
- async function D(o, e = !1) {
36
- const { global: t } = await d(o, e);
37
- return t;
35
+ async function v(o, e = !1) {
36
+ const { global: a } = await f(o, e);
37
+ return a;
38
38
  }
39
- class j {
39
+ class y extends L {
40
40
  constructor(e) {
41
- this.currentRoute = "general", this.cache = {
41
+ const a = {
42
42
  generalLocaleCache: {},
43
43
  routeLocaleCache: {},
44
44
  dynamicTranslationsCaches: [],
45
45
  serverTranslationCache: {}
46
- }, this.helper = w(this.cache), this.formatter = new L(), this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1, this.pluralFunc = e.plural || P, this.missingWarn = e.missingWarn ?? !1, this.missingHandler = e.missingHandler;
46
+ };
47
+ super({
48
+ cache: a,
49
+ plural: e.plural,
50
+ missingWarn: e.missingWarn,
51
+ missingHandler: e.missingHandler
52
+ }), this.currentRoute = "general", this.cache = {
53
+ generalLocaleCache: {},
54
+ routeLocaleCache: {},
55
+ dynamicTranslationsCaches: [],
56
+ serverTranslationCache: {}
57
+ }, this.cache = a, this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1;
47
58
  }
48
59
  /**
49
60
  * Set the current route name context.
@@ -52,74 +63,32 @@ class j {
52
63
  setRoute(e) {
53
64
  this.currentRoute = e;
54
65
  }
55
- getRoute() {
56
- return this.currentRoute;
57
- }
58
- /**
59
- * Get translation for a key
60
- *
61
- * Search order:
62
- * 1. Current Locale + Current Route (Specific)
63
- * 2. Current Locale + General (Global)
64
- * 3. Fallback Locale + Current Route
65
- * 4. Fallback Locale + General
66
- */
67
- t(e, t, r, a) {
68
- if (!e) return "";
69
- const s = a || this.currentRoute;
70
- let l = this.helper.getTranslation(this.locale, s, e);
71
- return !l && this.locale !== this.fallbackLocale && (l = this.helper.getTranslation(this.fallbackLocale, s, e)), l || (this.missingHandler ? this.missingHandler(this.locale, e, s) : this.missingWarn && console.warn(
72
- `[i18n] Translation key '${e}' not found for locale '${this.locale}' (route: '${s}').`
73
- ), l = r === void 0 ? e : r || e), typeof l == "string" && t ? F(l, t) : l || e;
74
- }
75
- /**
76
- * Get translation as string
77
- */
78
- ts(e, t, r, a) {
79
- return this.t(e, t, r, a)?.toString() ?? r ?? e;
66
+ // --- Implementation of abstract methods ---
67
+ getLocale() {
68
+ return this.locale;
80
69
  }
81
- /**
82
- * Plural translation
83
- */
84
- tc(e, t, r) {
85
- const { count: a, ...s } = typeof t == "number" ? { count: t } : t;
86
- if (a === void 0)
87
- return r ?? e;
88
- const l = (i, c, h) => this.t(i, c, h);
89
- return this.pluralFunc(
90
- e,
91
- Number.parseInt(a.toString()),
92
- s,
93
- this.locale,
94
- l
95
- ) ?? r ?? e;
70
+ getFallbackLocale() {
71
+ return this.fallbackLocale;
96
72
  }
97
- // --- Formatters ---
98
- tn(e, t) {
99
- return this.formatter.formatNumber(e, this.locale, t);
100
- }
101
- td(e, t) {
102
- return this.formatter.formatDate(e, this.locale, t);
103
- }
104
- tdr(e, t) {
105
- return this.formatter.formatRelativeTime(e, this.locale, t);
73
+ getRoute() {
74
+ return this.currentRoute;
106
75
  }
107
76
  // --- Loader & Cache Management ---
108
77
  /**
109
78
  * Load translations from directory.
110
79
  */
111
80
  async loadTranslations(e) {
112
- const t = e || this.translationDir;
113
- if (!t) {
81
+ const a = e || this.translationDir;
82
+ if (!a) {
114
83
  console.warn("[i18n-node] No translation directory specified");
115
84
  return;
116
85
  }
117
- const { global: r, routes: a } = await d(t, this.disablePageLocales);
118
- for (const [s, l] of Object.entries(r))
119
- this.helper.mergeGlobalTranslation(s, l, !0);
120
- for (const [s, l] of Object.entries(a))
121
- for (const [n, i] of Object.entries(l))
122
- await this.helper.loadPageTranslations(n, s, i);
86
+ const { global: t, routes: r } = await f(a, this.disablePageLocales);
87
+ for (const [s, n] of Object.entries(t))
88
+ this.helper.mergeGlobalTranslation(s, n, !0);
89
+ for (const [s, n] of Object.entries(r))
90
+ for (const [l, i] of Object.entries(n))
91
+ await this.helper.loadPageTranslations(l, s, i);
123
92
  }
124
93
  /**
125
94
  * Clear cache and reload translations from disk
@@ -128,27 +97,27 @@ class j {
128
97
  this.helper.clearCache(), await this.loadTranslations(), console.log("[i18n-node] Cache cleared and translations reloaded.");
129
98
  }
130
99
  // --- Manual Manipulation ---
131
- addTranslations(e, t, r = !0) {
132
- r ? this.helper.mergeGlobalTranslation(e, t, !0) : this.helper.loadTranslations(e, t);
100
+ addTranslations(e, a, t = !0) {
101
+ super.loadTranslationsCore(e, a, t);
133
102
  }
134
- addRouteTranslations(e, t, r, a = !0) {
135
- a ? this.helper.mergeTranslation(e, t, r, !0) : this.helper.loadPageTranslations(e, t, r);
103
+ addRouteTranslations(e, a, t, r = !0) {
104
+ super.loadRouteTranslationsCore(e, a, t, r);
136
105
  }
137
106
  hasTranslation(e) {
138
107
  return this.helper.hasTranslation(this.locale, e);
139
108
  }
140
109
  clear() {
141
- this.helper.clearCache();
110
+ super.clearCache();
142
111
  }
143
112
  }
144
- function H(o) {
145
- return new j(o);
113
+ function F(o) {
114
+ return new y(o);
146
115
  }
147
116
  export {
148
- $ as FormatService,
149
- j as I18n,
150
- H as createI18n,
151
- y as interpolate,
152
- D as loadGlobalTranslations,
153
- d as loadTranslations
117
+ O as FormatService,
118
+ y as I18n,
119
+ F as createI18n,
120
+ k as interpolate,
121
+ v as loadGlobalTranslations,
122
+ f as loadTranslations
154
123
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@i18n-micro/node",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -22,16 +22,16 @@
22
22
  "access": "public"
23
23
  },
24
24
  "dependencies": {
25
- "@i18n-micro/core": "1.0.27",
26
- "@i18n-micro/types": "1.0.15"
25
+ "@i18n-micro/types": "1.0.16",
26
+ "@i18n-micro/core": "1.0.28"
27
27
  },
28
28
  "devDependencies": {
29
- "@types/node": "^20.17.10",
30
- "vite": "^5.0.0",
31
- "vite-plugin-dts": "^4.3.0",
29
+ "@types/node": "^20.19.26",
30
+ "vite": "^7.2.7",
31
+ "vite-plugin-dts": "^4.5.4",
32
32
  "jest": "^29.7.0",
33
- "ts-jest": "^29.1.2",
34
- "@types/jest": "^29.5.0"
33
+ "ts-jest": "^29.4.6",
34
+ "@types/jest": "^29.5.14"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "vite build",