@i18n-micro/node 1.1.1 → 1.2.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 h=require("@i18n-micro/core"),p=require("node:fs/promises"),c=require("node:path");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;
@@ -0,0 +1,83 @@
1
+ import { BaseI18n } from '@i18n-micro/core';
2
+ import { CleanTranslation } from '@i18n-micro/types';
3
+ import { FormatService } from '@i18n-micro/core';
4
+ import { Getter } from '@i18n-micro/types';
5
+ import { interpolate } from '@i18n-micro/core';
6
+ import { Locale } from '@i18n-micro/types';
7
+ import { LocaleCode } from '@i18n-micro/types';
8
+ import { Params } from '@i18n-micro/types';
9
+ import { PluralFunc } from '@i18n-micro/types';
10
+ import { TranslationKey } from '@i18n-micro/types';
11
+ import { Translations } from '@i18n-micro/types';
12
+
13
+ export { CleanTranslation }
14
+
15
+ export declare function createI18n(options: I18nOptions): I18n;
16
+
17
+ export { FormatService }
18
+
19
+ export { Getter }
20
+
21
+ export declare class I18n extends BaseI18n {
22
+ locale: string;
23
+ fallbackLocale: string;
24
+ translationDir?: string;
25
+ private disablePageLocales;
26
+ currentRoute: string;
27
+ constructor(options: I18nOptions);
28
+ setRoute(routeName: string): void;
29
+ getLocale(): string;
30
+ getFallbackLocale(): string;
31
+ getRoute(): string;
32
+ loadTranslations(dir?: string): Promise<void>;
33
+ reload(): Promise<void>;
34
+ addTranslations(locale: string, translations: Translations, merge?: boolean): void;
35
+ addRouteTranslations(locale: string, routeName: string, translations: Translations, merge?: boolean): void;
36
+ hasTranslation(key: TranslationKey): boolean;
37
+ clear(): void;
38
+ }
39
+
40
+ export declare interface I18nOptions {
41
+ locale: string;
42
+ fallbackLocale?: string;
43
+ translationDir?: string;
44
+ plural?: PluralFunc;
45
+ missingWarn?: boolean;
46
+ missingHandler?: (locale: string, key: string, routeName: string) => void;
47
+ disablePageLocales?: boolean;
48
+ }
49
+
50
+ export { interpolate }
51
+
52
+ export declare interface LoadedTranslations {
53
+ root: Record<string, Translations>;
54
+ routes: Record<string, Record<string, Translations>>;
55
+ }
56
+
57
+ /**
58
+ * Load only root-level translations from a directory
59
+ * @param dir - Directory path containing translation files
60
+ * @param disablePageLocales - If true, ignores pages/ folder and treats all files as root translations
61
+ */
62
+ export declare function loadRootTranslations(dir: string, disablePageLocales?: boolean): Promise<Record<string, Translations>>;
63
+
64
+ /**
65
+ * Load translations from a directory
66
+ * Supports both flat structure (locales/{lang}.json) and nested structure (locales/pages/{page}/{lang}.json)
67
+ * @param dir - Directory path containing translation files
68
+ * @param disablePageLocales - If true, ignores pages/ folder and treats all files as global translations
69
+ * @returns Object with global and route-specific translations
70
+ */
71
+ export declare function loadTranslations(dir: string, disablePageLocales?: boolean): Promise<LoadedTranslations>;
72
+
73
+ export { Locale }
74
+
75
+ export { LocaleCode }
76
+
77
+ export { Params }
78
+
79
+ export { PluralFunc }
80
+
81
+ export { Translations }
82
+
83
+ export { }
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
1
  import { BaseI18n as g } from "@i18n-micro/core";
2
- import { FormatService as O, interpolate as k } from "@i18n-micro/core";
2
+ import { FormatService as v, interpolate as O } from "@i18n-micro/core";
3
3
  import { readdir as m, readFile as p } from "node:fs/promises";
4
- import { join as b, relative as T, sep as y, basename as w } from "node:path";
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 m(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 = b(r.path, r.name), l = T(o, s).split(y), i = w(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 p(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
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 g {
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 g {
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,40 +1,68 @@
1
1
  {
2
2
  "name": "@i18n-micro/node",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
+ "description": "Node.js utilities for loading translations and locale data in Nuxt I18n Micro.",
4
5
  "type": "module",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/s00d/nuxt-i18n-micro.git",
11
+ "directory": "packages/node"
12
+ },
13
+ "homepage": "https://github.com/s00d/nuxt-i18n-micro/tree/main/packages/node#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/s00d/nuxt-i18n-micro/issues"
16
+ },
17
+ "engines": {
18
+ "node": ">=20.0.0"
19
+ },
5
20
  "main": "./dist/index.cjs",
6
- "module": "./dist/index.mjs",
7
21
  "types": "./dist/index.d.ts",
8
22
  "exports": {
9
23
  ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.mjs",
12
- "require": "./dist/index.cjs"
13
- }
24
+ "import": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.mjs"
27
+ },
28
+ "require": {
29
+ "types": "./dist/index.d.cts",
30
+ "default": "./dist/index.cjs"
31
+ },
32
+ "default": "./dist/index.mjs"
33
+ },
34
+ "./package.json": "./package.json"
14
35
  },
15
36
  "files": [
16
- "dist"
37
+ "dist",
38
+ "README.md",
39
+ "LICENSE"
17
40
  ],
18
- "engines": {
19
- "node": ">=20.0.0"
20
- },
21
41
  "publishConfig": {
22
42
  "access": "public"
23
43
  },
44
+ "keywords": [
45
+ "nuxt",
46
+ "i18n",
47
+ "node",
48
+ "translations"
49
+ ],
24
50
  "dependencies": {
25
- "@i18n-micro/core": "1.1.2",
26
- "@i18n-micro/types": "1.1.1"
51
+ "@i18n-micro/core": "1.3.1",
52
+ "@i18n-micro/types": "1.2.2"
27
53
  },
28
54
  "devDependencies": {
55
+ "@types/jest": "^29.5.14",
29
56
  "@types/node": "^20.19.26",
30
- "vite": "^7.3.1",
31
- "vite-plugin-dts": "^4.5.4",
32
57
  "jest": "^29.7.0",
58
+ "publint": "^0.3.17",
33
59
  "ts-jest": "^29.4.6",
34
- "@types/jest": "^29.5.14"
60
+ "vite": "^7.3.1",
61
+ "vite-plugin-dts": "^4.5.4"
35
62
  },
36
63
  "scripts": {
37
64
  "build": "vite build",
65
+ "check:package": "publint",
38
66
  "dev": "cd playground && npm run dev",
39
67
  "test": "jest",
40
68
  "typecheck": "tsc --noEmit"