@i18n-micro/node 1.2.16 → 1.2.17
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/README.md +3 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -20
- package/dist/index.d.ts +4 -20
- package/dist/index.mjs +30 -51
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# @i18n-micro/node
|
|
2
2
|
|
|
3
|
-
Node.js runtime for nuxt-i18n-micro
|
|
3
|
+
Node.js runtime for nuxt-i18n-micro — filesystem translation loading on top of [`@i18n-micro/runtime`](../runtime). Use this in Node.js applications, CLI tools, or backend services.
|
|
4
|
+
|
|
5
|
+
For browser / vanilla JS without `fs`, use `@i18n-micro/runtime` directly.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@i18n-micro/
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@i18n-micro/runtime"),t=require("@i18n-micro/utils/parse-path"),n=require("node:fs/promises"),r=require("node:path"),i=require("@i18n-micro/core");async function a(e,i=!1){let a={root:{},routes:{}};try{let o=(await(0,n.readdir)(e,{recursive:!0,withFileTypes:!0})).filter(e=>e.isFile()&&e.name.endsWith(`.json`)),s=await Promise.all(o.map(async t=>{let i=(0,r.join)(t.path,t.name),a=(0,r.relative)(e,i).split(r.sep).join(`/`);try{let e=await(0,n.readFile)(i,`utf-8`);return{relativePath:a,translations:JSON.parse(e)}}catch(e){return console.error(`Failed to load translation file ${i}:`,e),null}}));for(let e of s)e&&(0,t.storeLoadedTranslationFile)(a,e.relativePath,e.translations,i)}catch(t){t&&typeof t==`object`&&`code`in t&&t.code!==`ENOENT`&&console.error(`Failed to read directory ${e}:`,t)}return a}async function o(e,t=!1){let{root:n}=await a(e,t);return n}var s=class extends e.I18n{constructor(e){let{translationDir:t,disablePageLocales:n,...r}=e;super(r),this.translationDir=t,this.disablePageLocales=n??!1}async loadTranslations(e){let n=e||this.translationDir;if(!n){console.warn(`[i18n-node] No translation directory specified`);return}let{root:r,routes:i}=await a(n,this.disablePageLocales);for(let[e,t]of Object.entries(r))this.helper.mergeTranslation(e,`index`,t,!0);for(let[e,n]of Object.entries(i))for(let[r,i]of Object.entries(n)){let n=this.helper.getCache(r,`index`);this.helper.loadPageTranslations(r,e,(0,t.mergeRouteTranslationsWithRoot)(n,i))}this.onTranslationsChanged()}async reload(){let e=this.translationDir;if(!e){console.warn(`[i18n-node] No translation directory specified`);return}this.clearCache(),await this.loadTranslations(e),console.log(`[i18n-node] Cache cleared and translations reloaded.`)}};function c(e){return new s(e)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:function(){return i.FormatService}}),exports.I18n=s,exports.createI18n=c,Object.defineProperty(exports,"interpolate",{enumerable:!0,get:function(){return i.interpolate}}),exports.loadRootTranslations=o,exports.loadTranslations=a;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { BaseI18n } from '@i18n-micro/core';
|
|
2
1
|
import { CleanTranslation } from '@i18n-micro/types';
|
|
3
2
|
import { FormatService } from '@i18n-micro/core';
|
|
4
3
|
import { Getter } from '@i18n-micro/types';
|
|
4
|
+
import { I18n as I18n_2 } from '@i18n-micro/runtime';
|
|
5
|
+
import { I18nOptions as I18nOptions_2 } from '@i18n-micro/runtime';
|
|
5
6
|
import { interpolate } from '@i18n-micro/core';
|
|
6
7
|
import { Locale } from '@i18n-micro/types';
|
|
7
8
|
import { LocaleCode } from '@i18n-micro/types';
|
|
8
9
|
import { Params } from '@i18n-micro/types';
|
|
9
10
|
import { PluralFunc } from '@i18n-micro/types';
|
|
10
11
|
import { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
|
|
11
|
-
import { TranslationKey } from '@i18n-micro/types';
|
|
12
12
|
import { Translations } from '@i18n-micro/types';
|
|
13
13
|
|
|
14
14
|
export { CleanTranslation }
|
|
@@ -19,32 +19,16 @@ export { FormatService }
|
|
|
19
19
|
|
|
20
20
|
export { Getter }
|
|
21
21
|
|
|
22
|
-
export declare class I18n extends
|
|
23
|
-
locale: string;
|
|
24
|
-
fallbackLocale: string;
|
|
22
|
+
export declare class I18n extends I18n_2 {
|
|
25
23
|
translationDir?: string;
|
|
26
24
|
private disablePageLocales;
|
|
27
|
-
currentRoute: string;
|
|
28
25
|
constructor(options: I18nOptions);
|
|
29
|
-
setRoute(routeName: string): void;
|
|
30
|
-
getLocale(): string;
|
|
31
|
-
getFallbackLocale(): string;
|
|
32
|
-
getRoute(): string;
|
|
33
26
|
loadTranslations(dir?: string): Promise<void>;
|
|
34
27
|
reload(): Promise<void>;
|
|
35
|
-
addTranslations(locale: string, translations: Translations, merge?: boolean): void;
|
|
36
|
-
addRouteTranslations(locale: string, routeName: string, translations: Translations, merge?: boolean): void;
|
|
37
|
-
hasTranslation(key: TranslationKey): boolean;
|
|
38
|
-
clear(): void;
|
|
39
28
|
}
|
|
40
29
|
|
|
41
|
-
export declare interface I18nOptions {
|
|
42
|
-
locale: string;
|
|
43
|
-
fallbackLocale?: string;
|
|
30
|
+
export declare interface I18nOptions extends I18nOptions_2 {
|
|
44
31
|
translationDir?: string;
|
|
45
|
-
plural?: PluralFunc;
|
|
46
|
-
missingWarn?: boolean;
|
|
47
|
-
missingHandler?: (locale: string, key: string, routeName: string) => void;
|
|
48
32
|
disablePageLocales?: boolean;
|
|
49
33
|
}
|
|
50
34
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { BaseI18n } from '@i18n-micro/core';
|
|
2
1
|
import { CleanTranslation } from '@i18n-micro/types';
|
|
3
2
|
import { FormatService } from '@i18n-micro/core';
|
|
4
3
|
import { Getter } from '@i18n-micro/types';
|
|
4
|
+
import { I18n as I18n_2 } from '@i18n-micro/runtime';
|
|
5
|
+
import { I18nOptions as I18nOptions_2 } from '@i18n-micro/runtime';
|
|
5
6
|
import { interpolate } from '@i18n-micro/core';
|
|
6
7
|
import { Locale } from '@i18n-micro/types';
|
|
7
8
|
import { LocaleCode } from '@i18n-micro/types';
|
|
8
9
|
import { Params } from '@i18n-micro/types';
|
|
9
10
|
import { PluralFunc } from '@i18n-micro/types';
|
|
10
11
|
import { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
|
|
11
|
-
import { TranslationKey } from '@i18n-micro/types';
|
|
12
12
|
import { Translations } from '@i18n-micro/types';
|
|
13
13
|
|
|
14
14
|
export { CleanTranslation }
|
|
@@ -19,32 +19,16 @@ export { FormatService }
|
|
|
19
19
|
|
|
20
20
|
export { Getter }
|
|
21
21
|
|
|
22
|
-
export declare class I18n extends
|
|
23
|
-
locale: string;
|
|
24
|
-
fallbackLocale: string;
|
|
22
|
+
export declare class I18n extends I18n_2 {
|
|
25
23
|
translationDir?: string;
|
|
26
24
|
private disablePageLocales;
|
|
27
|
-
currentRoute: string;
|
|
28
25
|
constructor(options: I18nOptions);
|
|
29
|
-
setRoute(routeName: string): void;
|
|
30
|
-
getLocale(): string;
|
|
31
|
-
getFallbackLocale(): string;
|
|
32
|
-
getRoute(): string;
|
|
33
26
|
loadTranslations(dir?: string): Promise<void>;
|
|
34
27
|
reload(): Promise<void>;
|
|
35
|
-
addTranslations(locale: string, translations: Translations, merge?: boolean): void;
|
|
36
|
-
addRouteTranslations(locale: string, routeName: string, translations: Translations, merge?: boolean): void;
|
|
37
|
-
hasTranslation(key: TranslationKey): boolean;
|
|
38
|
-
clear(): void;
|
|
39
28
|
}
|
|
40
29
|
|
|
41
|
-
export declare interface I18nOptions {
|
|
42
|
-
locale: string;
|
|
43
|
-
fallbackLocale?: string;
|
|
30
|
+
export declare interface I18nOptions extends I18nOptions_2 {
|
|
44
31
|
translationDir?: string;
|
|
45
|
-
plural?: PluralFunc;
|
|
46
|
-
missingWarn?: boolean;
|
|
47
|
-
missingHandler?: (locale: string, key: string, routeName: string) => void;
|
|
48
32
|
disablePageLocales?: boolean;
|
|
49
33
|
}
|
|
50
34
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { mergeRouteTranslationsWithRoot as
|
|
3
|
-
import { readFile as
|
|
4
|
-
import { join as
|
|
1
|
+
import { I18n as e } from "@i18n-micro/runtime";
|
|
2
|
+
import { mergeRouteTranslationsWithRoot as t, storeLoadedTranslationFile as n } from "@i18n-micro/utils/parse-path";
|
|
3
|
+
import { readFile as r, readdir as i } from "node:fs/promises";
|
|
4
|
+
import { join as a, relative as o, sep as s } from "node:path";
|
|
5
|
+
import { FormatService as c, interpolate as l } from "@i18n-micro/core";
|
|
5
6
|
//#region src/loader.ts
|
|
6
7
|
async function u(e, t = !1) {
|
|
7
|
-
let
|
|
8
|
+
let c = {
|
|
8
9
|
root: {},
|
|
9
10
|
routes: {}
|
|
10
11
|
};
|
|
11
12
|
try {
|
|
12
|
-
let
|
|
13
|
+
let l = (await i(e, {
|
|
13
14
|
recursive: !0,
|
|
14
15
|
withFileTypes: !0
|
|
15
|
-
})).filter((e) => e.isFile() && e.name.endsWith(".json")), u = await Promise.all(
|
|
16
|
-
let n =
|
|
16
|
+
})).filter((e) => e.isFile() && e.name.endsWith(".json")), u = await Promise.all(l.map(async (t) => {
|
|
17
|
+
let n = a(t.path, t.name), i = o(e, n).split(s).join("/");
|
|
17
18
|
try {
|
|
18
|
-
let e = await
|
|
19
|
+
let e = await r(n, "utf-8");
|
|
19
20
|
return {
|
|
20
|
-
relativePath:
|
|
21
|
+
relativePath: i,
|
|
21
22
|
translations: JSON.parse(e)
|
|
22
23
|
};
|
|
23
24
|
} catch (e) {
|
|
24
25
|
return console.error(`Failed to load translation file ${n}:`, e), null;
|
|
25
26
|
}
|
|
26
27
|
}));
|
|
27
|
-
for (let e of u) e &&
|
|
28
|
+
for (let e of u) e && n(c, e.relativePath, e.translations, t);
|
|
28
29
|
} catch (t) {
|
|
29
30
|
t && typeof t == "object" && "code" in t && t.code !== "ENOENT" && console.error(`Failed to read directory ${e}:`, t);
|
|
30
31
|
}
|
|
31
|
-
return
|
|
32
|
+
return c;
|
|
32
33
|
}
|
|
33
34
|
async function d(e, t = !1) {
|
|
34
35
|
let { root: n } = await u(e, t);
|
|
@@ -38,56 +39,34 @@ async function d(e, t = !1) {
|
|
|
38
39
|
//#region src/i18n.ts
|
|
39
40
|
var f = class extends e {
|
|
40
41
|
constructor(e) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
plural: e.plural,
|
|
44
|
-
missingWarn: e.missingWarn,
|
|
45
|
-
missingHandler: e.missingHandler
|
|
46
|
-
}), this.currentRoute = "index", this.locale = e.locale, this.fallbackLocale = e.fallbackLocale || e.locale, this.translationDir = e.translationDir, this.disablePageLocales = e.disablePageLocales ?? !1;
|
|
47
|
-
}
|
|
48
|
-
setRoute(e) {
|
|
49
|
-
this.currentRoute = e;
|
|
50
|
-
}
|
|
51
|
-
getLocale() {
|
|
52
|
-
return this.locale;
|
|
53
|
-
}
|
|
54
|
-
getFallbackLocale() {
|
|
55
|
-
return this.fallbackLocale;
|
|
56
|
-
}
|
|
57
|
-
getRoute() {
|
|
58
|
-
return this.currentRoute;
|
|
42
|
+
let { translationDir: t, disablePageLocales: n, ...r } = e;
|
|
43
|
+
super(r), this.translationDir = t, this.disablePageLocales = n ?? !1;
|
|
59
44
|
}
|
|
60
45
|
async loadTranslations(e) {
|
|
61
|
-
let
|
|
62
|
-
if (!
|
|
46
|
+
let n = e || this.translationDir;
|
|
47
|
+
if (!n) {
|
|
63
48
|
console.warn("[i18n-node] No translation directory specified");
|
|
64
49
|
return;
|
|
65
50
|
}
|
|
66
|
-
let { root:
|
|
67
|
-
for (let [e, t] of Object.entries(
|
|
68
|
-
for (let [e,
|
|
69
|
-
let
|
|
70
|
-
this.helper.loadPageTranslations(
|
|
51
|
+
let { root: r, routes: i } = await u(n, this.disablePageLocales);
|
|
52
|
+
for (let [e, t] of Object.entries(r)) this.helper.mergeTranslation(e, "index", t, !0);
|
|
53
|
+
for (let [e, n] of Object.entries(i)) for (let [r, i] of Object.entries(n)) {
|
|
54
|
+
let n = this.helper.getCache(r, "index");
|
|
55
|
+
this.helper.loadPageTranslations(r, e, t(n, i));
|
|
71
56
|
}
|
|
57
|
+
this.onTranslationsChanged();
|
|
72
58
|
}
|
|
73
59
|
async reload() {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
super.loadRouteTranslationsCore(e, t, n, r);
|
|
81
|
-
}
|
|
82
|
-
hasTranslation(e) {
|
|
83
|
-
return this.helper.hasTranslation(this.locale, e);
|
|
84
|
-
}
|
|
85
|
-
clear() {
|
|
86
|
-
super.clearCache();
|
|
60
|
+
let e = this.translationDir;
|
|
61
|
+
if (!e) {
|
|
62
|
+
console.warn("[i18n-node] No translation directory specified");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.clearCache(), await this.loadTranslations(e), console.log("[i18n-node] Cache cleared and translations reloaded.");
|
|
87
66
|
}
|
|
88
67
|
};
|
|
89
68
|
function p(e) {
|
|
90
69
|
return new f(e);
|
|
91
70
|
}
|
|
92
71
|
//#endregion
|
|
93
|
-
export {
|
|
72
|
+
export { c as FormatService, f as I18n, p as createI18n, l as interpolate, d as loadRootTranslations, u as loadTranslations };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/node",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.17",
|
|
4
4
|
"description": "Node.js utilities for loading translations and locale data in Nuxt I18n Micro.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -45,8 +45,9 @@
|
|
|
45
45
|
"access": "public"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@i18n-micro/types": "^1.2.15",
|
|
49
48
|
"@i18n-micro/core": "^1.3.18",
|
|
49
|
+
"@i18n-micro/types": "^1.2.15",
|
|
50
|
+
"@i18n-micro/runtime": "^1.0.0",
|
|
50
51
|
"@i18n-micro/utils": "^1.0.23"
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|