@i18n-micro/node 1.0.1 → 1.1.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 +1 -1
- package/dist/index.d.ts +0 -25
- package/dist/index.mjs +20 -41
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
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;
|
package/dist/index.d.ts
CHANGED
|
@@ -12,49 +12,24 @@ import { Translations } from '@i18n-micro/types';
|
|
|
12
12
|
|
|
13
13
|
export { CleanTranslation }
|
|
14
14
|
|
|
15
|
-
/**
|
|
16
|
-
* Create a new I18n instance
|
|
17
|
-
*/
|
|
18
15
|
export declare function createI18n(options: I18nOptions): I18n;
|
|
19
16
|
|
|
20
17
|
export { FormatService }
|
|
21
18
|
|
|
22
19
|
export { Getter }
|
|
23
20
|
|
|
24
|
-
/**
|
|
25
|
-
* Node.js I18n Adapter
|
|
26
|
-
*
|
|
27
|
-
* Provides a standalone i18n instance that uses the shared core logic from nuxt-i18n-micro.
|
|
28
|
-
* Fully compatible with the file structure and caching mechanisms of the Nuxt module.
|
|
29
|
-
*/
|
|
30
21
|
export declare class I18n extends BaseI18n {
|
|
31
22
|
locale: string;
|
|
32
23
|
fallbackLocale: string;
|
|
33
24
|
translationDir?: string;
|
|
34
25
|
private disablePageLocales;
|
|
35
|
-
/**
|
|
36
|
-
* Current active route name for translations.
|
|
37
|
-
* Defaults to 'general' (global translations only).
|
|
38
|
-
* Change this using setRoute() to access page-specific translations.
|
|
39
|
-
*/
|
|
40
26
|
currentRoute: string;
|
|
41
|
-
private cache;
|
|
42
27
|
constructor(options: I18nOptions);
|
|
43
|
-
/**
|
|
44
|
-
* Set the current route name context.
|
|
45
|
-
* Useful when processing a specific page request in Node.
|
|
46
|
-
*/
|
|
47
28
|
setRoute(routeName: string): void;
|
|
48
29
|
getLocale(): string;
|
|
49
30
|
getFallbackLocale(): string;
|
|
50
31
|
getRoute(): string;
|
|
51
|
-
/**
|
|
52
|
-
* Load translations from directory.
|
|
53
|
-
*/
|
|
54
32
|
loadTranslations(dir?: string): Promise<void>;
|
|
55
|
-
/**
|
|
56
|
-
* Clear cache and reload translations from disk
|
|
57
|
-
*/
|
|
58
33
|
reload(): Promise<void>;
|
|
59
34
|
addTranslations(locale: string, translations: Translations, merge?: boolean): void;
|
|
60
35
|
addRouteTranslations(locale: string, routeName: string, translations: Translations, merge?: boolean): void;
|
package/dist/index.mjs
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import {
|
|
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";
|
|
1
|
+
import { BaseI18n as g } from "@i18n-micro/core";
|
|
4
2
|
import { FormatService as O, interpolate as k } from "@i18n-micro/core";
|
|
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";
|
|
5
5
|
async function f(o, e = !1) {
|
|
6
6
|
const a = {
|
|
7
7
|
global: {},
|
|
8
8
|
routes: {}
|
|
9
9
|
};
|
|
10
10
|
try {
|
|
11
|
-
const t = await
|
|
11
|
+
const t = await m(o, { recursive: !0, withFileTypes: !0 });
|
|
12
12
|
for (const r of t) {
|
|
13
13
|
if (!r.isFile() || !r.name.endsWith(".json"))
|
|
14
14
|
continue;
|
|
15
|
-
const s = b(r.path, r.name), l =
|
|
15
|
+
const s = b(r.path, r.name), l = T(o, s).split(y), i = w(r.name, ".json");
|
|
16
16
|
try {
|
|
17
|
-
const c = await
|
|
17
|
+
const c = await p(s, "utf-8"), h = JSON.parse(c);
|
|
18
18
|
if (!e && l[0] === "pages" && l.length >= 2) {
|
|
19
19
|
const d = l.slice(1, -1);
|
|
20
20
|
if (d.length > 0) {
|
|
21
|
-
const
|
|
22
|
-
a.routes[
|
|
21
|
+
const u = d.join("-");
|
|
22
|
+
a.routes[u] || (a.routes[u] = {}), a.routes[u][i] = h;
|
|
23
23
|
}
|
|
24
24
|
} else
|
|
25
|
-
a.global[i] =
|
|
25
|
+
a.global[i] = h;
|
|
26
26
|
} catch (c) {
|
|
27
27
|
console.error(`Failed to load translation file ${s}:`, c);
|
|
28
28
|
}
|
|
@@ -32,38 +32,25 @@ async function f(o, e = !1) {
|
|
|
32
32
|
}
|
|
33
33
|
return a;
|
|
34
34
|
}
|
|
35
|
-
async function
|
|
35
|
+
async function R(o, e = !1) {
|
|
36
36
|
const { global: a } = await f(o, e);
|
|
37
37
|
return a;
|
|
38
38
|
}
|
|
39
|
-
class
|
|
39
|
+
class L extends g {
|
|
40
40
|
constructor(e) {
|
|
41
41
|
const a = {
|
|
42
|
-
|
|
43
|
-
routeLocaleCache: {},
|
|
44
|
-
dynamicTranslationsCaches: [],
|
|
45
|
-
serverTranslationCache: {}
|
|
42
|
+
translations: /* @__PURE__ */ new Map()
|
|
46
43
|
};
|
|
47
44
|
super({
|
|
48
|
-
|
|
45
|
+
storage: a,
|
|
49
46
|
plural: e.plural,
|
|
50
47
|
missingWarn: e.missingWarn,
|
|
51
48
|
missingHandler: e.missingHandler
|
|
52
|
-
}), this.currentRoute = "general", this.
|
|
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;
|
|
49
|
+
}), this.currentRoute = "general", this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1;
|
|
58
50
|
}
|
|
59
|
-
/**
|
|
60
|
-
* Set the current route name context.
|
|
61
|
-
* Useful when processing a specific page request in Node.
|
|
62
|
-
*/
|
|
63
51
|
setRoute(e) {
|
|
64
52
|
this.currentRoute = e;
|
|
65
53
|
}
|
|
66
|
-
// --- Implementation of abstract methods ---
|
|
67
54
|
getLocale() {
|
|
68
55
|
return this.locale;
|
|
69
56
|
}
|
|
@@ -73,10 +60,6 @@ class y extends L {
|
|
|
73
60
|
getRoute() {
|
|
74
61
|
return this.currentRoute;
|
|
75
62
|
}
|
|
76
|
-
// --- Loader & Cache Management ---
|
|
77
|
-
/**
|
|
78
|
-
* Load translations from directory.
|
|
79
|
-
*/
|
|
80
63
|
async loadTranslations(e) {
|
|
81
64
|
const a = e || this.translationDir;
|
|
82
65
|
if (!a) {
|
|
@@ -88,15 +71,11 @@ class y extends L {
|
|
|
88
71
|
this.helper.mergeGlobalTranslation(s, n, !0);
|
|
89
72
|
for (const [s, n] of Object.entries(r))
|
|
90
73
|
for (const [l, i] of Object.entries(n))
|
|
91
|
-
|
|
74
|
+
this.helper.loadPageTranslations(l, s, i);
|
|
92
75
|
}
|
|
93
|
-
/**
|
|
94
|
-
* Clear cache and reload translations from disk
|
|
95
|
-
*/
|
|
96
76
|
async reload() {
|
|
97
77
|
this.helper.clearCache(), await this.loadTranslations(), console.log("[i18n-node] Cache cleared and translations reloaded.");
|
|
98
78
|
}
|
|
99
|
-
// --- Manual Manipulation ---
|
|
100
79
|
addTranslations(e, a, t = !0) {
|
|
101
80
|
super.loadTranslationsCore(e, a, t);
|
|
102
81
|
}
|
|
@@ -110,14 +89,14 @@ class y extends L {
|
|
|
110
89
|
super.clearCache();
|
|
111
90
|
}
|
|
112
91
|
}
|
|
113
|
-
function
|
|
114
|
-
return new
|
|
92
|
+
function N(o) {
|
|
93
|
+
return new L(o);
|
|
115
94
|
}
|
|
116
95
|
export {
|
|
117
96
|
O as FormatService,
|
|
118
|
-
|
|
119
|
-
|
|
97
|
+
L as I18n,
|
|
98
|
+
N as createI18n,
|
|
120
99
|
k as interpolate,
|
|
121
|
-
|
|
100
|
+
R as loadGlobalTranslations,
|
|
122
101
|
f as loadTranslations
|
|
123
102
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@i18n-micro/
|
|
26
|
-
"@i18n-micro/
|
|
25
|
+
"@i18n-micro/core": "1.1.2",
|
|
26
|
+
"@i18n-micro/types": "1.1.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^20.19.26",
|
|
30
|
-
"vite": "^7.
|
|
30
|
+
"vite": "^7.3.1",
|
|
31
31
|
"vite-plugin-dts": "^4.5.4",
|
|
32
32
|
"jest": "^29.7.0",
|
|
33
33
|
"ts-jest": "^29.4.6",
|