@i18n-micro/core
Advanced tools
+0
-10
@@ -9,8 +9,3 @@ import { CleanTranslation, MissingHandler, Params, PluralFunc, TranslationKey, Translations } from '@i18n-micro/types'; | ||
| missingHandler?: (locale: string, key: string, routeName: string) => void; | ||
| getPreviousPageInfo?: () => { | ||
| locale: string; | ||
| routeName: string; | ||
| } | null; | ||
| getCustomMissingHandler?: () => MissingHandler | null; | ||
| enablePreviousPageFallback?: boolean; | ||
| } | ||
@@ -29,8 +24,3 @@ /** | ||
| missingHandler?: (locale: string, key: string, routeName: string) => void; | ||
| getPreviousPageInfo?: () => { | ||
| locale: string; | ||
| routeName: string; | ||
| } | null; | ||
| getCustomMissingHandler?: () => MissingHandler | null; | ||
| enablePreviousPageFallback: boolean; | ||
| constructor(options?: BaseI18nOptions); | ||
@@ -37,0 +27,0 @@ /** |
+1
-1
@@ -1,1 +0,1 @@ | ||
| "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class g{formatNumber(e,t,r){return new Intl.NumberFormat(t,r).format(e)}formatDate(e,t,r){const n=new Date(e);return Number.isNaN(n.getTime())?"Invalid Date":new Intl.DateTimeFormat(t,r).format(n)}formatRelativeTime(e,t,r){const n=new Date(e);if(Number.isNaN(n.getTime()))return new Intl.RelativeTimeFormat(t,r).format(0,"second");const i=Math.floor((new Date().getTime()-n.getTime())/1e3),o=[{unit:"year",seconds:31536e3},{unit:"month",seconds:2592e3},{unit:"day",seconds:86400},{unit:"hour",seconds:3600},{unit:"minute",seconds:60},{unit:"second",seconds:1}];for(const{unit:l,seconds:u}of o){const c=Math.floor(i/u);if(c>=1)return new Intl.RelativeTimeFormat(t,r).format(-c,l)}return new Intl.RelativeTimeFormat(t,r).format(0,"second")}}const p=/\{(\w+)\}/g;function h(s,e){return e?s.replace(p,(t,r)=>{const n=e[r];return n!==void 0?String(n):`{${r}}`}):s}function v(s){return s==="prefix"||s==="prefix_and_default"}function T(s){return s==="no_prefix"}function P(s){return s==="prefix"}function b(s){return s==="prefix_except_default"}function x(s){return s==="prefix_and_default"}const m=(s,e,t,r,n)=>{const a=n(s,t);if(!a)return null;const i=a.toString().split("|");if(i.length===0)return null;const o=e<i.length?i[e]:i[i.length-1];return o?o.trim().replace("{count}",e.toString()):null};function f(s,e){if(!s||typeof e!="string")return null;if(e in s){const n=s[e];return n}const t=e.split(".");let r=s;for(const n of t)if(r&&typeof r=="object"&&n in r)r=r[n];else return null;return r??null}function d(s){const e=s?.translations??new Map;return{hasCache(t,r){const n=`${t}:${r}`;return e.has(n)||e.has(t)},getCache(t,r){const n=`${t}:${r}`;return e.get(n)},setCache(t,r,n){},hasTranslation(t,r){for(const[n,a]of e)if((n===t||n.startsWith(`${t}:`))&&f(a,r)!==null)return!0;return!1},hasGeneralTranslation(t){return e.has(t)},hasPageTranslation(t,r){return e.has(`${t}:${r}`)},getTranslation(t,r,n){const a=`${t}:${r}`,i=e.get(a),o=f(i,n);if(o!==null)return o;const l=e.get(t);return f(l,n)},loadTranslations(t,r){const n=e.get(t)??{};e.set(t,{...n,...r})},setTranslations(t,r){e.set(t,r)},loadPageTranslations(t,r,n){const a=`${t}:${r}`,i=e.get(a);!i||Object.keys(i).length===0?e.set(a,n):e.set(a,{...i,...n})},mergeTranslation(t,r,n,a=!1){const i=`${t}:${r}`,o=e.get(i)??{};e.set(i,{...o,...n})},mergeGlobalTranslation(t,r,n=!1){const a=e.get(t)??{};e.set(t,{...a,...r})},clearCache(){e.clear()}}}class N{constructor(e={}){this.formatter=new g,this.helper=d(e.storage),this.formatter=new g,this.pluralFunc=e.plural||m,this.missingWarn=e.missingWarn??!0,this.missingHandler=e.missingHandler,this.getPreviousPageInfo=e.getPreviousPageInfo,this.getCustomMissingHandler=e.getCustomMissingHandler,this.enablePreviousPageFallback=e.enablePreviousPageFallback??!1}t(e,t,r,n){if(!e)return"";const a=this.getLocale(),i=n||this.getRoute();let o=this.helper.getTranslation(a,i,e);if(!o&&this.enablePreviousPageFallback&&this.getPreviousPageInfo){const l=this.getPreviousPageInfo();if(l){const u=this.helper.getTranslation(l.locale,l.routeName,e);u&&(o=u,process.env.NODE_ENV!=="production"&&console.log(`Using fallback translation from previous route: ${l.routeName} -> ${e}`))}}if(!o){const l=this.getFallbackLocale();a!==l&&(o=this.helper.getTranslation(l,i,e))}if(!o){const l=this.getCustomMissingHandler?.();l?l(a,e,i):this.missingHandler?this.missingHandler(a,e,i):this.missingWarn&&process.env.NODE_ENV!=="production"&&typeof window<"u"&&console.warn(`Not found '${e}' key in '${a}' locale messages for route '${i}'.`),o=r===void 0?e:r||e}return typeof o=="string"&&t?h(o,t):o}ts(e,t,r,n){return this.t(e,t,r,n)?.toString()??r??e}tc(e,t,r){const{count:n,...a}=typeof t=="number"?{count:t}:t;if(n===void 0)return r??e;const i=(l,u,c)=>this.t(l,u,c);return this.pluralFunc(e,Number.parseInt(n.toString(),10),a,this.getLocale(),i)??r??e}tn(e,t){return this.formatter.formatNumber(e,this.getLocale(),t)}td(e,t){return this.formatter.formatDate(e,this.getLocale(),t)}tdr(e,t){return this.formatter.formatRelativeTime(e,this.getLocale(),t)}has(e,t){const r=t||this.getRoute(),n=this.getLocale();return!!this.helper.getTranslation(n,r,e)}clearCache(){this.helper.clearCache()}loadTranslationsCore(e,t,r){r?this.helper.mergeGlobalTranslation(e,t,!0):this.helper.setTranslations(e,t)}loadRouteTranslationsCore(e,t,r,n){n?this.helper.mergeTranslation(e,t,r,!0):this.helper.loadPageTranslations(e,t,r)}}exports.BaseI18n=N;exports.FormatService=g;exports.defaultPlural=m;exports.interpolate=h;exports.isNoPrefixStrategy=T;exports.isPrefixAndDefaultStrategy=x;exports.isPrefixExceptDefaultStrategy=b;exports.isPrefixStrategy=P;exports.useTranslationHelper=d;exports.withPrefixStrategy=v; | ||
| "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class g{formatNumber(t,e,r){return new Intl.NumberFormat(e,r).format(t)}formatDate(t,e,r){const n=new Date(t);return Number.isNaN(n.getTime())?"Invalid Date":new Intl.DateTimeFormat(e,r).format(n)}formatRelativeTime(t,e,r){const n=new Date(t);if(Number.isNaN(n.getTime()))return new Intl.RelativeTimeFormat(e,r).format(0,"second");const i=Math.floor((new Date().getTime()-n.getTime())/1e3),o=[{unit:"year",seconds:31536e3},{unit:"month",seconds:2592e3},{unit:"day",seconds:86400},{unit:"hour",seconds:3600},{unit:"minute",seconds:60},{unit:"second",seconds:1}];for(const{unit:l,seconds:c}of o){const u=Math.floor(i/c);if(u>=1)return new Intl.RelativeTimeFormat(e,r).format(-u,l)}return new Intl.RelativeTimeFormat(e,r).format(0,"second")}}const p=/\{(\w+)\}/g;function h(s,t){return t?s.replace(p,(e,r)=>{const n=t[r];return n!==void 0?String(n):`{${r}}`}):s}function T(s){return s==="prefix"||s==="prefix_and_default"}function v(s){return s==="no_prefix"}function x(s){return s==="prefix"}function S(s){return s==="prefix_except_default"}function w(s){return s==="prefix_and_default"}const m=(s,t,e,r,n)=>{const a=n(s,e);if(!a)return null;const i=a.toString().split("|");if(i.length===0)return null;const o=t<i.length?i[t]:i[i.length-1];return o?o.trim().replace("{count}",t.toString()):null};function f(s,t){if(!s||typeof t!="string")return null;if(t in s){const n=s[t];return n}const e=t.split(".");let r=s;for(const n of e)if(r&&typeof r=="object"&&n in r)r=r[n];else return null;return r??null}function d(s){const t=s?.translations??new Map;return{hasCache(e,r){const n=`${e}:${r}`;return t.has(n)||t.has(e)},getCache(e,r){const n=`${e}:${r}`;return t.get(n)},setCache(e,r,n){},hasTranslation(e,r){for(const[n,a]of t)if((n===e||n.startsWith(`${e}:`))&&f(a,r)!==null)return!0;return!1},hasGeneralTranslation(e){return t.has(e)},hasPageTranslation(e,r){return t.has(`${e}:${r}`)},getTranslation(e,r,n){const a=`${e}:${r}`,i=t.get(a),o=f(i,n);if(o!==null)return o;const l=t.get(e);return f(l,n)},loadTranslations(e,r){const n=t.get(e)??{};t.set(e,{...n,...r})},setTranslations(e,r){t.set(e,r)},loadPageTranslations(e,r,n){const a=`${e}:${r}`,i=t.get(a);!i||Object.keys(i).length===0?t.set(a,n):t.set(a,{...i,...n})},mergeTranslation(e,r,n,a=!1){const i=`${e}:${r}`,o=t.get(i)??{};t.set(i,{...o,...n})},mergeGlobalTranslation(e,r,n=!1){const a=t.get(e)??{};t.set(e,{...a,...r})},clearCache(){t.clear()}}}class ${constructor(t={}){this.formatter=new g,this.helper=d(t.storage),this.formatter=new g,this.pluralFunc=t.plural||m,this.missingWarn=t.missingWarn??!0,this.missingHandler=t.missingHandler,this.getCustomMissingHandler=t.getCustomMissingHandler}t(t,e,r,n){if(!t)return"";const a=this.getLocale(),i=n||this.getRoute();let o=this.helper.getTranslation(a,i,t);if(!o){const l=this.getFallbackLocale();a!==l&&(o=this.helper.getTranslation(l,i,t))}if(!o){const l=this.getCustomMissingHandler?.();l?l(a,t,i):this.missingHandler?this.missingHandler(a,t,i):this.missingWarn&&process.env.NODE_ENV!=="production"&&typeof window<"u"&&console.warn(`Not found '${t}' key in '${a}' locale messages for route '${i}'.`),o=r===void 0?t:r||t}return typeof o=="string"&&e?h(o,e):o}ts(t,e,r,n){return this.t(t,e,r,n)?.toString()??r??t}tc(t,e,r){const{count:n,...a}=typeof e=="number"?{count:e}:e;if(n===void 0)return r??t;const i=(l,c,u)=>this.t(l,c,u);return this.pluralFunc(t,Number.parseInt(n.toString(),10),a,this.getLocale(),i)??r??t}tn(t,e){return this.formatter.formatNumber(t,this.getLocale(),e)}td(t,e){return this.formatter.formatDate(t,this.getLocale(),e)}tdr(t,e){return this.formatter.formatRelativeTime(t,this.getLocale(),e)}has(t,e){const r=e||this.getRoute(),n=this.getLocale();return!!this.helper.getTranslation(n,r,t)}clearCache(){this.helper.clearCache()}loadTranslationsCore(t,e,r){r?this.helper.mergeGlobalTranslation(t,e,!0):this.helper.setTranslations(t,e)}loadRouteTranslationsCore(t,e,r,n){n?this.helper.mergeTranslation(t,e,r,!0):this.helper.loadPageTranslations(t,e,r)}}exports.BaseI18n=$;exports.FormatService=g;exports.defaultPlural=m;exports.interpolate=h;exports.isNoPrefixStrategy=v;exports.isPrefixAndDefaultStrategy=w;exports.isPrefixExceptDefaultStrategy=S;exports.isPrefixStrategy=x;exports.useTranslationHelper=d;exports.withPrefixStrategy=T; |
+102
-109
@@ -1,13 +0,13 @@ | ||
| class g { | ||
| formatNumber(e, t, n) { | ||
| return new Intl.NumberFormat(t, n).format(e); | ||
| class h { | ||
| formatNumber(t, e, n) { | ||
| return new Intl.NumberFormat(e, n).format(t); | ||
| } | ||
| formatDate(e, t, n) { | ||
| const r = new Date(e); | ||
| return Number.isNaN(r.getTime()) ? "Invalid Date" : new Intl.DateTimeFormat(t, n).format(r); | ||
| formatDate(t, e, n) { | ||
| const r = new Date(t); | ||
| return Number.isNaN(r.getTime()) ? "Invalid Date" : new Intl.DateTimeFormat(e, n).format(r); | ||
| } | ||
| formatRelativeTime(e, t, n) { | ||
| const r = new Date(e); | ||
| formatRelativeTime(t, e, n) { | ||
| const r = new Date(t); | ||
| if (Number.isNaN(r.getTime())) | ||
| return new Intl.RelativeTimeFormat(t, n).format(0, "second"); | ||
| return new Intl.RelativeTimeFormat(e, n).format(0, "second"); | ||
| const i = Math.floor(((/* @__PURE__ */ new Date()).getTime() - r.getTime()) / 1e3), o = [ | ||
@@ -21,34 +21,34 @@ { unit: "year", seconds: 31536e3 }, | ||
| ]; | ||
| for (const { unit: l, seconds: u } of o) { | ||
| const c = Math.floor(i / u); | ||
| if (c >= 1) | ||
| return new Intl.RelativeTimeFormat(t, n).format(-c, l); | ||
| for (const { unit: l, seconds: c } of o) { | ||
| const u = Math.floor(i / c); | ||
| if (u >= 1) | ||
| return new Intl.RelativeTimeFormat(e, n).format(-u, l); | ||
| } | ||
| return new Intl.RelativeTimeFormat(t, n).format(0, "second"); | ||
| return new Intl.RelativeTimeFormat(e, n).format(0, "second"); | ||
| } | ||
| } | ||
| const h = /\{(\w+)\}/g; | ||
| function m(s, e) { | ||
| return e ? s.replace(h, (t, n) => { | ||
| const r = e[n]; | ||
| const g = /\{(\w+)\}/g; | ||
| function m(s, t) { | ||
| return t ? s.replace(g, (e, n) => { | ||
| const r = t[n]; | ||
| return r !== void 0 ? String(r) : `{${n}}`; | ||
| }) : s; | ||
| } | ||
| function v(s) { | ||
| function T(s) { | ||
| return s === "prefix" || s === "prefix_and_default"; | ||
| } | ||
| function T(s) { | ||
| function v(s) { | ||
| return s === "no_prefix"; | ||
| } | ||
| function P(s) { | ||
| function w(s) { | ||
| return s === "prefix"; | ||
| } | ||
| function b(s) { | ||
| function x(s) { | ||
| return s === "prefix_except_default"; | ||
| } | ||
| function N(s) { | ||
| function $(s) { | ||
| return s === "prefix_and_default"; | ||
| } | ||
| const d = (s, e, t, n, r) => { | ||
| const a = r(s, t); | ||
| const d = (s, t, e, n, r) => { | ||
| const a = r(s, e); | ||
| if (!a) | ||
@@ -58,14 +58,14 @@ return null; | ||
| if (i.length === 0) return null; | ||
| const o = e < i.length ? i[e] : i[i.length - 1]; | ||
| return o ? o.trim().replace("{count}", e.toString()) : null; | ||
| const o = t < i.length ? i[t] : i[i.length - 1]; | ||
| return o ? o.trim().replace("{count}", t.toString()) : null; | ||
| }; | ||
| function f(s, e) { | ||
| if (!s || typeof e != "string") return null; | ||
| if (e in s) { | ||
| const r = s[e]; | ||
| function f(s, t) { | ||
| if (!s || typeof t != "string") return null; | ||
| if (t in s) { | ||
| const r = s[t]; | ||
| return r; | ||
| } | ||
| const t = e.split("."); | ||
| const e = t.split("."); | ||
| let n = s; | ||
| for (const r of t) | ||
| for (const r of e) | ||
| if (n && typeof n == "object" && r in n) | ||
@@ -78,59 +78,59 @@ n = n[r]; | ||
| function p(s) { | ||
| const e = s?.translations ?? /* @__PURE__ */ new Map(); | ||
| const t = s?.translations ?? /* @__PURE__ */ new Map(); | ||
| return { | ||
| hasCache(t, n) { | ||
| const r = `${t}:${n}`; | ||
| return e.has(r) || e.has(t); | ||
| hasCache(e, n) { | ||
| const r = `${e}:${n}`; | ||
| return t.has(r) || t.has(e); | ||
| }, | ||
| getCache(t, n) { | ||
| const r = `${t}:${n}`; | ||
| return e.get(r); | ||
| getCache(e, n) { | ||
| const r = `${e}:${n}`; | ||
| return t.get(r); | ||
| }, | ||
| setCache(t, n, r) { | ||
| setCache(e, n, r) { | ||
| }, | ||
| hasTranslation(t, n) { | ||
| for (const [r, a] of e) | ||
| if ((r === t || r.startsWith(`${t}:`)) && f(a, n) !== null) | ||
| hasTranslation(e, n) { | ||
| for (const [r, a] of t) | ||
| if ((r === e || r.startsWith(`${e}:`)) && f(a, n) !== null) | ||
| return !0; | ||
| return !1; | ||
| }, | ||
| hasGeneralTranslation(t) { | ||
| return e.has(t); | ||
| hasGeneralTranslation(e) { | ||
| return t.has(e); | ||
| }, | ||
| hasPageTranslation(t, n) { | ||
| return e.has(`${t}:${n}`); | ||
| hasPageTranslation(e, n) { | ||
| return t.has(`${e}:${n}`); | ||
| }, | ||
| getTranslation(t, n, r) { | ||
| const a = `${t}:${n}`, i = e.get(a), o = f(i, r); | ||
| getTranslation(e, n, r) { | ||
| const a = `${e}:${n}`, i = t.get(a), o = f(i, r); | ||
| if (o !== null) return o; | ||
| const l = e.get(t); | ||
| const l = t.get(e); | ||
| return f(l, r); | ||
| }, | ||
| loadTranslations(t, n) { | ||
| const r = e.get(t) ?? {}; | ||
| e.set(t, { ...r, ...n }); | ||
| loadTranslations(e, n) { | ||
| const r = t.get(e) ?? {}; | ||
| t.set(e, { ...r, ...n }); | ||
| }, | ||
| setTranslations(t, n) { | ||
| e.set(t, n); | ||
| setTranslations(e, n) { | ||
| t.set(e, n); | ||
| }, | ||
| loadPageTranslations(t, n, r) { | ||
| const a = `${t}:${n}`, i = e.get(a); | ||
| !i || Object.keys(i).length === 0 ? e.set(a, r) : e.set(a, { ...i, ...r }); | ||
| loadPageTranslations(e, n, r) { | ||
| const a = `${e}:${n}`, i = t.get(a); | ||
| !i || Object.keys(i).length === 0 ? t.set(a, r) : t.set(a, { ...i, ...r }); | ||
| }, | ||
| mergeTranslation(t, n, r, a = !1) { | ||
| const i = `${t}:${n}`, o = e.get(i) ?? {}; | ||
| e.set(i, { ...o, ...r }); | ||
| mergeTranslation(e, n, r, a = !1) { | ||
| const i = `${e}:${n}`, o = t.get(i) ?? {}; | ||
| t.set(i, { ...o, ...r }); | ||
| }, | ||
| mergeGlobalTranslation(t, n, r = !1) { | ||
| const a = e.get(t) ?? {}; | ||
| e.set(t, { ...a, ...n }); | ||
| mergeGlobalTranslation(e, n, r = !1) { | ||
| const a = t.get(e) ?? {}; | ||
| t.set(e, { ...a, ...n }); | ||
| }, | ||
| clearCache() { | ||
| e.clear(); | ||
| t.clear(); | ||
| } | ||
| }; | ||
| } | ||
| class $ { | ||
| constructor(e = {}) { | ||
| this.formatter = new g(), this.helper = p(e.storage), this.formatter = new g(), this.pluralFunc = e.plural || d, this.missingWarn = e.missingWarn ?? !0, this.missingHandler = e.missingHandler, this.getPreviousPageInfo = e.getPreviousPageInfo, this.getCustomMissingHandler = e.getCustomMissingHandler, this.enablePreviousPageFallback = e.enablePreviousPageFallback ?? !1; | ||
| class _ { | ||
| constructor(t = {}) { | ||
| this.formatter = new h(), this.helper = p(t.storage), this.formatter = new h(), this.pluralFunc = t.plural || d, this.missingWarn = t.missingWarn ?? !0, this.missingHandler = t.missingHandler, this.getCustomMissingHandler = t.getCustomMissingHandler; | ||
| } | ||
@@ -142,22 +142,15 @@ // --- Public methods (implemented in base class) --- | ||
| */ | ||
| t(e, t, n, r) { | ||
| if (!e) return ""; | ||
| t(t, e, n, r) { | ||
| if (!t) return ""; | ||
| const a = this.getLocale(), i = r || this.getRoute(); | ||
| let o = this.helper.getTranslation(a, i, e); | ||
| if (!o && this.enablePreviousPageFallback && this.getPreviousPageInfo) { | ||
| const l = this.getPreviousPageInfo(); | ||
| if (l) { | ||
| const u = this.helper.getTranslation(l.locale, l.routeName, e); | ||
| u && (o = u, process.env.NODE_ENV !== "production" && console.log(`Using fallback translation from previous route: ${l.routeName} -> ${e}`)); | ||
| } | ||
| } | ||
| let o = this.helper.getTranslation(a, i, t); | ||
| if (!o) { | ||
| const l = this.getFallbackLocale(); | ||
| a !== l && (o = this.helper.getTranslation(l, i, e)); | ||
| a !== l && (o = this.helper.getTranslation(l, i, t)); | ||
| } | ||
| if (!o) { | ||
| const l = this.getCustomMissingHandler?.(); | ||
| l ? l(a, e, i) : this.missingHandler ? this.missingHandler(a, e, i) : this.missingWarn && process.env.NODE_ENV !== "production" && typeof window < "u" && console.warn(`Not found '${e}' key in '${a}' locale messages for route '${i}'.`), o = n === void 0 ? e : n || e; | ||
| l ? l(a, t, i) : this.missingHandler ? this.missingHandler(a, t, i) : this.missingWarn && process.env.NODE_ENV !== "production" && typeof window < "u" && console.warn(`Not found '${t}' key in '${a}' locale messages for route '${i}'.`), o = n === void 0 ? t : n || t; | ||
| } | ||
| return typeof o == "string" && t ? m(o, t) : o; | ||
| return typeof o == "string" && e ? m(o, e) : o; | ||
| } | ||
@@ -167,4 +160,4 @@ /** | ||
| */ | ||
| ts(e, t, n, r) { | ||
| return this.t(e, t, n, r)?.toString() ?? n ?? e; | ||
| ts(t, e, n, r) { | ||
| return this.t(t, e, n, r)?.toString() ?? n ?? t; | ||
| } | ||
@@ -174,8 +167,8 @@ /** | ||
| */ | ||
| tc(e, t, n) { | ||
| const { count: r, ...a } = typeof t == "number" ? { count: t } : t; | ||
| tc(t, e, n) { | ||
| const { count: r, ...a } = typeof e == "number" ? { count: e } : e; | ||
| if (r === void 0) | ||
| return n ?? e; | ||
| const i = (l, u, c) => this.t(l, u, c); | ||
| return this.pluralFunc(e, Number.parseInt(r.toString(), 10), a, this.getLocale(), i) ?? n ?? e; | ||
| return n ?? t; | ||
| const i = (l, c, u) => this.t(l, c, u); | ||
| return this.pluralFunc(t, Number.parseInt(r.toString(), 10), a, this.getLocale(), i) ?? n ?? t; | ||
| } | ||
@@ -185,4 +178,4 @@ /** | ||
| */ | ||
| tn(e, t) { | ||
| return this.formatter.formatNumber(e, this.getLocale(), t); | ||
| tn(t, e) { | ||
| return this.formatter.formatNumber(t, this.getLocale(), e); | ||
| } | ||
@@ -192,4 +185,4 @@ /** | ||
| */ | ||
| td(e, t) { | ||
| return this.formatter.formatDate(e, this.getLocale(), t); | ||
| td(t, e) { | ||
| return this.formatter.formatDate(t, this.getLocale(), e); | ||
| } | ||
@@ -199,4 +192,4 @@ /** | ||
| */ | ||
| tdr(e, t) { | ||
| return this.formatter.formatRelativeTime(e, this.getLocale(), t); | ||
| tdr(t, e) { | ||
| return this.formatter.formatRelativeTime(t, this.getLocale(), e); | ||
| } | ||
@@ -207,5 +200,5 @@ /** | ||
| */ | ||
| has(e, t) { | ||
| const n = t || this.getRoute(), r = this.getLocale(); | ||
| return !!this.helper.getTranslation(r, n, e); | ||
| has(t, e) { | ||
| const n = e || this.getRoute(), r = this.getLocale(); | ||
| return !!this.helper.getTranslation(r, n, t); | ||
| } | ||
@@ -223,4 +216,4 @@ /** | ||
| */ | ||
| loadTranslationsCore(e, t, n) { | ||
| n ? this.helper.mergeGlobalTranslation(e, t, !0) : this.helper.setTranslations(e, t); | ||
| loadTranslationsCore(t, e, n) { | ||
| n ? this.helper.mergeGlobalTranslation(t, e, !0) : this.helper.setTranslations(t, e); | ||
| } | ||
@@ -231,17 +224,17 @@ /** | ||
| */ | ||
| loadRouteTranslationsCore(e, t, n, r) { | ||
| r ? this.helper.mergeTranslation(e, t, n, !0) : this.helper.loadPageTranslations(e, t, n); | ||
| loadRouteTranslationsCore(t, e, n, r) { | ||
| r ? this.helper.mergeTranslation(t, e, n, !0) : this.helper.loadPageTranslations(t, e, n); | ||
| } | ||
| } | ||
| export { | ||
| $ as BaseI18n, | ||
| g as FormatService, | ||
| _ as BaseI18n, | ||
| h as FormatService, | ||
| d as defaultPlural, | ||
| m as interpolate, | ||
| T as isNoPrefixStrategy, | ||
| N as isPrefixAndDefaultStrategy, | ||
| b as isPrefixExceptDefaultStrategy, | ||
| P as isPrefixStrategy, | ||
| v as isNoPrefixStrategy, | ||
| $ as isPrefixAndDefaultStrategy, | ||
| x as isPrefixExceptDefaultStrategy, | ||
| w as isPrefixStrategy, | ||
| p as useTranslationHelper, | ||
| v as withPrefixStrategy | ||
| T as withPrefixStrategy | ||
| }; |
+2
-2
| { | ||
| "name": "@i18n-micro/core", | ||
| "version": "1.1.3", | ||
| "version": "1.1.4", | ||
| "description": "", | ||
@@ -25,3 +25,3 @@ "repository": "s00d/nuxt-i18n-micro", | ||
| "dependencies": { | ||
| "@i18n-micro/types": "1.1.3" | ||
| "@i18n-micro/types": "1.1.4" | ||
| }, | ||
@@ -28,0 +28,0 @@ "devDependencies": { |
+3
-25
@@ -11,6 +11,3 @@ import type { CleanTranslation, Getter, MissingHandler, Params, PluralFunc, TranslationKey, Translations } from '@i18n-micro/types' | ||
| missingHandler?: (locale: string, key: string, routeName: string) => void | ||
| // Optional hooks for Nuxt runtime specific features | ||
| getPreviousPageInfo?: () => { locale: string; routeName: string } | null | ||
| getCustomMissingHandler?: () => MissingHandler | null | ||
| enablePreviousPageFallback?: boolean | ||
| } | ||
@@ -31,6 +28,3 @@ | ||
| public missingHandler?: (locale: string, key: string, routeName: string) => void | ||
| // Optional hooks for Nuxt runtime specific features | ||
| public getPreviousPageInfo?: () => { locale: string; routeName: string } | null | ||
| public getCustomMissingHandler?: () => MissingHandler | null | ||
| public enablePreviousPageFallback: boolean | ||
@@ -43,5 +37,3 @@ constructor(options: BaseI18nOptions = {}) { | ||
| this.missingHandler = options.missingHandler | ||
| this.getPreviousPageInfo = options.getPreviousPageInfo | ||
| this.getCustomMissingHandler = options.getCustomMissingHandler | ||
| this.enablePreviousPageFallback = options.enablePreviousPageFallback ?? false | ||
| } | ||
@@ -83,17 +75,3 @@ | ||
| // 2. If translation not found and there are saved previous translations, use them (only if enabled) | ||
| if (!value && this.enablePreviousPageFallback && this.getPreviousPageInfo) { | ||
| const prev = this.getPreviousPageInfo() | ||
| if (prev) { | ||
| const prevValue = this.helper.getTranslation<string>(prev.locale, prev.routeName, key) | ||
| if (prevValue) { | ||
| value = prevValue | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| console.log(`Using fallback translation from previous route: ${prev.routeName} -> ${key}`) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // 3. Fallback to fallbackLocale if not found and different (for non-Nuxt adapters) | ||
| // 2. Fallback to fallbackLocale if not found and different (for non-Nuxt adapters) | ||
| if (!value) { | ||
@@ -106,3 +84,3 @@ const fallbackLocale = this.getFallbackLocale() | ||
| // 4. Handle missing | ||
| // 3. Handle missing | ||
| if (!value) { | ||
@@ -125,3 +103,3 @@ // Call custom handler if set (Nuxt runtime), otherwise use instance handler | ||
| // 5. Interpolate | ||
| // 4. Interpolate | ||
| return typeof value === 'string' && params ? interpolate(value, params) : (value as CleanTranslation) | ||
@@ -128,0 +106,0 @@ } |
+0
-17
@@ -121,19 +121,2 @@ import type { PluralFunc, Translations } from '@i18n-micro/types' | ||
| test('should use previousPageInfo fallback when enabled', async () => { | ||
| const storage = { translations: new Map<string, Translations>() } | ||
| const prevInfo = { locale: 'fr', routeName: 'previous' } | ||
| const i18n = new TestI18n('en', 'en', 'general', { | ||
| storage, | ||
| getPreviousPageInfo: () => prevInfo, | ||
| enablePreviousPageFallback: true, | ||
| }) | ||
| const translations: Translations = { greeting: 'Bonjour' } | ||
| await i18n['helper'].loadPageTranslations('fr', 'previous', translations) | ||
| // Current locale is 'en', route is 'general', translation not found | ||
| // Should fallback to previous page info: locale 'fr', route 'previous' | ||
| expect(i18n.t('greeting')).toBe('Bonjour') | ||
| }) | ||
| test('should call missingHandler when translation is missing', () => { | ||
@@ -140,0 +123,0 @@ const handler = jest.fn() |
4
-42.86%69807
-4.13%1389
-3.81%+ Added
- Removed
Updated