@i18n-micro/node 1.2.0 → 1.2.2
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.cts +82 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.mjs +32 -38
- package/package.json +44 -15
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 u=require("@i18n-micro/core"),f=require("@i18n-micro/utils/parse-path"),h=require("node:fs/promises"),c=require("node:path");async function d(r,e=!1){const t={root:{},routes:{}};try{const a=await h.readdir(r,{recursive:!0,withFileTypes:!0});for(const o of a){if(!o.isFile()||!o.name.endsWith(".json"))continue;const n=c.join(o.path,o.name),l=c.relative(r,n).split(c.sep).join("/");try{const s=await h.readFile(n,"utf-8"),i=JSON.parse(s);f.storeLoadedTranslationFile(t,l,i,e)}catch(s){console.error(`Failed to load translation file ${n}:`,s)}}}catch(a){a&&typeof a=="object"&&"code"in a&&a.code!=="ENOENT"&&console.error(`Failed to read directory ${r}:`,a)}return t}async function T(r,e=!1){const{root:t}=await d(r,e);return t}class g extends u.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:o}=await d(t,this.disablePageLocales);for(const[n,l]of Object.entries(a))this.helper.mergeTranslation(n,"index",l,!0);for(const[n,l]of Object.entries(o))for(const[s,i]of Object.entries(l)){const p=this.helper.getCache(s,"index");this.helper.loadPageTranslations(s,n,f.mergeRouteTranslationsWithRoot(p,i))}}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,o=!0){super.loadRouteTranslationsCore(e,t,a,o)}hasTranslation(e){return this.helper.hasTranslation(this.locale,e)}clear(){super.clearCache()}}function b(r){return new g(r)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>u.FormatService});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>u.interpolate});exports.I18n=g;exports.createI18n=b;exports.loadRootTranslations=T;exports.loadTranslations=d;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
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 { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
|
|
11
|
+
import { TranslationKey } from '@i18n-micro/types';
|
|
12
|
+
import { Translations } from '@i18n-micro/types';
|
|
13
|
+
|
|
14
|
+
export { CleanTranslation }
|
|
15
|
+
|
|
16
|
+
export declare function createI18n(options: I18nOptions): I18n;
|
|
17
|
+
|
|
18
|
+
export { FormatService }
|
|
19
|
+
|
|
20
|
+
export { Getter }
|
|
21
|
+
|
|
22
|
+
export declare class I18n extends BaseI18n {
|
|
23
|
+
locale: string;
|
|
24
|
+
fallbackLocale: string;
|
|
25
|
+
translationDir?: string;
|
|
26
|
+
private disablePageLocales;
|
|
27
|
+
currentRoute: string;
|
|
28
|
+
constructor(options: I18nOptions);
|
|
29
|
+
setRoute(routeName: string): void;
|
|
30
|
+
getLocale(): string;
|
|
31
|
+
getFallbackLocale(): string;
|
|
32
|
+
getRoute(): string;
|
|
33
|
+
loadTranslations(dir?: string): Promise<void>;
|
|
34
|
+
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
|
+
}
|
|
40
|
+
|
|
41
|
+
export declare interface I18nOptions {
|
|
42
|
+
locale: string;
|
|
43
|
+
fallbackLocale?: string;
|
|
44
|
+
translationDir?: string;
|
|
45
|
+
plural?: PluralFunc;
|
|
46
|
+
missingWarn?: boolean;
|
|
47
|
+
missingHandler?: (locale: string, key: string, routeName: string) => void;
|
|
48
|
+
disablePageLocales?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { interpolate }
|
|
52
|
+
|
|
53
|
+
export declare interface LoadedTranslations extends TranslationFileBuckets<Translations> {
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Load only root-level translations from a directory
|
|
58
|
+
* @param dir - Directory path containing translation files
|
|
59
|
+
* @param disablePageLocales - If true, ignores pages/ folder and treats all files as root translations
|
|
60
|
+
*/
|
|
61
|
+
export declare function loadRootTranslations(dir: string, disablePageLocales?: boolean): Promise<Record<string, Translations>>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Load translations from a directory
|
|
65
|
+
* Supports both flat structure (locales/{lang}.json) and nested structure (locales/pages/{page}/{lang}.json)
|
|
66
|
+
* @param dir - Directory path containing translation files
|
|
67
|
+
* @param disablePageLocales - If true, ignores pages/ folder and treats all files as global translations
|
|
68
|
+
* @returns Object with global and route-specific translations
|
|
69
|
+
*/
|
|
70
|
+
export declare function loadTranslations(dir: string, disablePageLocales?: boolean): Promise<LoadedTranslations>;
|
|
71
|
+
|
|
72
|
+
export { Locale }
|
|
73
|
+
|
|
74
|
+
export { LocaleCode }
|
|
75
|
+
|
|
76
|
+
export { Params }
|
|
77
|
+
|
|
78
|
+
export { PluralFunc }
|
|
79
|
+
|
|
80
|
+
export { Translations }
|
|
81
|
+
|
|
82
|
+
export { }
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { Locale } from '@i18n-micro/types';
|
|
|
7
7
|
import { LocaleCode } from '@i18n-micro/types';
|
|
8
8
|
import { Params } from '@i18n-micro/types';
|
|
9
9
|
import { PluralFunc } from '@i18n-micro/types';
|
|
10
|
+
import { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
|
|
10
11
|
import { TranslationKey } from '@i18n-micro/types';
|
|
11
12
|
import { Translations } from '@i18n-micro/types';
|
|
12
13
|
|
|
@@ -49,9 +50,7 @@ export declare interface I18nOptions {
|
|
|
49
50
|
|
|
50
51
|
export { interpolate }
|
|
51
52
|
|
|
52
|
-
export declare interface LoadedTranslations {
|
|
53
|
-
root: Record<string, Translations>;
|
|
54
|
-
routes: Record<string, Record<string, Translations>>;
|
|
53
|
+
export declare interface LoadedTranslations extends TranslationFileBuckets<Translations> {
|
|
55
54
|
}
|
|
56
55
|
|
|
57
56
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1,42 +1,36 @@
|
|
|
1
|
-
import { BaseI18n as
|
|
2
|
-
import { FormatService as
|
|
1
|
+
import { BaseI18n as d } from "@i18n-micro/core";
|
|
2
|
+
import { FormatService as N, interpolate as O } from "@i18n-micro/core";
|
|
3
|
+
import { storeLoadedTranslationFile as h, mergeRouteTranslationsWithRoot as f } from "@i18n-micro/utils/parse-path";
|
|
3
4
|
import { readdir as m, readFile as p } from "node:fs/promises";
|
|
4
|
-
import { join as
|
|
5
|
-
async function
|
|
5
|
+
import { join as g, relative as T, sep as b } from "node:path";
|
|
6
|
+
async function c(r, e = !1) {
|
|
6
7
|
const t = {
|
|
7
8
|
root: {},
|
|
8
9
|
routes: {}
|
|
9
10
|
};
|
|
10
11
|
try {
|
|
11
|
-
const a = await m(
|
|
12
|
-
for (const
|
|
13
|
-
if (!
|
|
12
|
+
const a = await m(r, { recursive: !0, withFileTypes: !0 });
|
|
13
|
+
for (const o of a) {
|
|
14
|
+
if (!o.isFile() || !o.name.endsWith(".json"))
|
|
14
15
|
continue;
|
|
15
|
-
const s =
|
|
16
|
+
const s = g(o.path, o.name), l = T(r, s).split(b).join("/");
|
|
16
17
|
try {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const d = h.join("-");
|
|
22
|
-
t.routes[d] || (t.routes[d] = {}), t.routes[d][i] = u;
|
|
23
|
-
}
|
|
24
|
-
} else
|
|
25
|
-
t.root[i] = u;
|
|
26
|
-
} catch (l) {
|
|
27
|
-
console.error(`Failed to load translation file ${s}:`, l);
|
|
18
|
+
const n = await p(s, "utf-8"), i = JSON.parse(n);
|
|
19
|
+
h(t, l, i, e);
|
|
20
|
+
} catch (n) {
|
|
21
|
+
console.error(`Failed to load translation file ${s}:`, n);
|
|
28
22
|
}
|
|
29
23
|
}
|
|
30
24
|
} catch (a) {
|
|
31
|
-
a && typeof a == "object" && "code" in a && a.code !== "ENOENT" && console.error(`Failed to read directory ${
|
|
25
|
+
a && typeof a == "object" && "code" in a && a.code !== "ENOENT" && console.error(`Failed to read directory ${r}:`, a);
|
|
32
26
|
}
|
|
33
27
|
return t;
|
|
34
28
|
}
|
|
35
|
-
async function
|
|
36
|
-
const { root: t } = await
|
|
29
|
+
async function j(r, e = !1) {
|
|
30
|
+
const { root: t } = await c(r, e);
|
|
37
31
|
return t;
|
|
38
32
|
}
|
|
39
|
-
class
|
|
33
|
+
class y extends d {
|
|
40
34
|
constructor(e) {
|
|
41
35
|
const t = {
|
|
42
36
|
translations: /* @__PURE__ */ new Map()
|
|
@@ -66,13 +60,13 @@ class L extends g {
|
|
|
66
60
|
console.warn("[i18n-node] No translation directory specified");
|
|
67
61
|
return;
|
|
68
62
|
}
|
|
69
|
-
const { root: a, routes:
|
|
70
|
-
for (const [s,
|
|
71
|
-
this.helper.mergeTranslation(s, "index",
|
|
72
|
-
for (const [s,
|
|
73
|
-
for (const [n, i] of Object.entries(
|
|
74
|
-
const
|
|
75
|
-
this.helper.loadPageTranslations(n, s, u);
|
|
63
|
+
const { root: a, routes: o } = await c(t, this.disablePageLocales);
|
|
64
|
+
for (const [s, l] of Object.entries(a))
|
|
65
|
+
this.helper.mergeTranslation(s, "index", l, !0);
|
|
66
|
+
for (const [s, l] of Object.entries(o))
|
|
67
|
+
for (const [n, i] of Object.entries(l)) {
|
|
68
|
+
const u = this.helper.getCache(n, "index");
|
|
69
|
+
this.helper.loadPageTranslations(n, s, f(u, i));
|
|
76
70
|
}
|
|
77
71
|
}
|
|
78
72
|
async reload() {
|
|
@@ -81,8 +75,8 @@ class L extends g {
|
|
|
81
75
|
addTranslations(e, t, a = !0) {
|
|
82
76
|
super.loadTranslationsCore(e, t, a);
|
|
83
77
|
}
|
|
84
|
-
addRouteTranslations(e, t, a,
|
|
85
|
-
super.loadRouteTranslationsCore(e, t, a,
|
|
78
|
+
addRouteTranslations(e, t, a, o = !0) {
|
|
79
|
+
super.loadRouteTranslationsCore(e, t, a, o);
|
|
86
80
|
}
|
|
87
81
|
hasTranslation(e) {
|
|
88
82
|
return this.helper.hasTranslation(this.locale, e);
|
|
@@ -91,14 +85,14 @@ class L extends g {
|
|
|
91
85
|
super.clearCache();
|
|
92
86
|
}
|
|
93
87
|
}
|
|
94
|
-
function x(
|
|
95
|
-
return new
|
|
88
|
+
function x(r) {
|
|
89
|
+
return new y(r);
|
|
96
90
|
}
|
|
97
91
|
export {
|
|
98
|
-
|
|
99
|
-
|
|
92
|
+
N as FormatService,
|
|
93
|
+
y as I18n,
|
|
100
94
|
x as createI18n,
|
|
101
95
|
O as interpolate,
|
|
102
|
-
|
|
103
|
-
|
|
96
|
+
j as loadRootTranslations,
|
|
97
|
+
c as loadTranslations
|
|
104
98
|
};
|
package/package.json
CHANGED
|
@@ -1,40 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/node",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
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
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
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.
|
|
26
|
-
"@i18n-micro/
|
|
51
|
+
"@i18n-micro/core": "1.3.1",
|
|
52
|
+
"@i18n-micro/utils": "1.0.0",
|
|
53
|
+
"@i18n-micro/types": "1.2.3"
|
|
27
54
|
},
|
|
28
55
|
"devDependencies": {
|
|
56
|
+
"@types/jest": "^29.5.14",
|
|
29
57
|
"@types/node": "^20.19.26",
|
|
30
|
-
"vite": "^7.3.1",
|
|
31
|
-
"vite-plugin-dts": "^4.5.4",
|
|
32
58
|
"jest": "^29.7.0",
|
|
59
|
+
"publint": "^0.3.17",
|
|
33
60
|
"ts-jest": "^29.4.6",
|
|
34
|
-
"
|
|
61
|
+
"vite": "^7.3.1",
|
|
62
|
+
"vite-plugin-dts": "^4.5.4"
|
|
35
63
|
},
|
|
36
64
|
"scripts": {
|
|
37
65
|
"build": "vite build",
|
|
66
|
+
"check:package": "publint",
|
|
38
67
|
"dev": "cd playground && npm run dev",
|
|
39
68
|
"test": "jest",
|
|
40
69
|
"typecheck": "tsc --noEmit"
|