@i18n-micro/node 1.1.0 → 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 CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("node:fs/promises"),c=require("node:path"),h=require("@i18n-micro/core");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 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.mjs CHANGED
@@ -1,20 +1,20 @@
1
- import { readdir as g, readFile as m } from "node:fs/promises";
2
- import { join as p, relative as b, sep as T, basename as y } from "node:path";
3
- import { BaseI18n as w } 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 g(o, { recursive: !0, withFileTypes: !0 });
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 = p(r.path, r.name), l = b(o, s).split(T), i = y(r.name, ".json");
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 m(s, "utf-8"), h = JSON.parse(c);
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) {
@@ -36,7 +36,7 @@ async function R(o, e = !1) {
36
36
  const { global: a } = await f(o, e);
37
37
  return a;
38
38
  }
39
- class L extends w {
39
+ class L extends g {
40
40
  constructor(e) {
41
41
  const a = {
42
42
  translations: /* @__PURE__ */ new Map()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@i18n-micro/node",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -22,8 +22,8 @@
22
22
  "access": "public"
23
23
  },
24
24
  "dependencies": {
25
- "@i18n-micro/types": "1.1.0",
26
- "@i18n-micro/core": "1.1.0"
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",