@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 +1 -1
- package/dist/index.d.ts +4 -40
- package/dist/index.mjs +65 -96
- package/package.json +8 -8
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 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
|
|
2
|
-
import { join as
|
|
3
|
-
import {
|
|
4
|
-
import { FormatService as
|
|
5
|
-
async function
|
|
6
|
-
const
|
|
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
|
|
12
|
-
for (const
|
|
13
|
-
if (!
|
|
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 =
|
|
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
|
|
18
|
-
if (!e &&
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
21
|
-
const
|
|
22
|
-
|
|
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
|
-
|
|
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 (
|
|
31
|
-
|
|
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
|
|
33
|
+
return a;
|
|
34
34
|
}
|
|
35
|
-
async function
|
|
36
|
-
const { global:
|
|
37
|
-
return
|
|
35
|
+
async function v(o, e = !1) {
|
|
36
|
+
const { global: a } = await f(o, e);
|
|
37
|
+
return a;
|
|
38
38
|
}
|
|
39
|
-
class
|
|
39
|
+
class y extends L {
|
|
40
40
|
constructor(e) {
|
|
41
|
-
|
|
41
|
+
const a = {
|
|
42
42
|
generalLocaleCache: {},
|
|
43
43
|
routeLocaleCache: {},
|
|
44
44
|
dynamicTranslationsCaches: [],
|
|
45
45
|
serverTranslationCache: {}
|
|
46
|
-
}
|
|
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
|
-
|
|
56
|
-
|
|
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
|
-
|
|
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
|
-
|
|
98
|
-
|
|
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
|
|
113
|
-
if (!
|
|
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:
|
|
118
|
-
for (const [s,
|
|
119
|
-
this.helper.mergeGlobalTranslation(s,
|
|
120
|
-
for (const [s,
|
|
121
|
-
for (const [
|
|
122
|
-
await this.helper.loadPageTranslations(
|
|
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,
|
|
132
|
-
|
|
100
|
+
addTranslations(e, a, t = !0) {
|
|
101
|
+
super.loadTranslationsCore(e, a, t);
|
|
133
102
|
}
|
|
134
|
-
addRouteTranslations(e, t, r
|
|
135
|
-
|
|
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
|
-
|
|
110
|
+
super.clearCache();
|
|
142
111
|
}
|
|
143
112
|
}
|
|
144
|
-
function
|
|
145
|
-
return new
|
|
113
|
+
function F(o) {
|
|
114
|
+
return new y(o);
|
|
146
115
|
}
|
|
147
116
|
export {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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.
|
|
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/
|
|
26
|
-
"@i18n-micro/
|
|
25
|
+
"@i18n-micro/types": "1.0.16",
|
|
26
|
+
"@i18n-micro/core": "1.0.28"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/node": "^20.
|
|
30
|
-
"vite": "^
|
|
31
|
-
"vite-plugin-dts": "^4.
|
|
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.
|
|
34
|
-
"@types/jest": "^29.5.
|
|
33
|
+
"ts-jest": "^29.4.6",
|
|
34
|
+
"@types/jest": "^29.5.14"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "vite build",
|