@i18n-micro/core 1.0.28 → 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/base.d.ts +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.mjs +141 -363
- package/dist/translation.d.ts +16 -17
- package/package.json +3 -3
- package/src/base.ts +4 -4
- package/src/helpers.ts +7 -6
- package/src/index.ts +2 -4
- package/src/translation.ts +71 -181
- package/tests/base.test.ts +11 -36
- package/dist/route-service.d.ts +0 -40
- package/src/route-service.ts +0 -474
- package/tests/route-service.test.ts +0 -397
package/dist/base.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { useTranslationHelper,
|
|
1
|
+
import { useTranslationHelper, TranslationStorage } from './translation';
|
|
2
2
|
import { FormatService } from './format-service';
|
|
3
3
|
import { Translations, Params, PluralFunc, CleanTranslation, TranslationKey, MissingHandler } from '@i18n-micro/types';
|
|
4
4
|
export interface BaseI18nOptions {
|
|
5
|
-
|
|
5
|
+
storage?: TranslationStorage;
|
|
6
6
|
plural?: PluralFunc;
|
|
7
7
|
missingWarn?: boolean;
|
|
8
8
|
missingHandler?: (locale: string, key: string, routeName: string) => void;
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const L={},T={},N=[],w={};function x(o){return Array.isArray(o)||typeof o=="object"&&o!==null?JSON.parse(JSON.stringify(o)):o}function v(o,e){let t=o;if(o===null||typeof e!="string")return null;if(o[e])t=o[e];else{const r=e.toString().split(".");for(const n of r)if(t&&typeof t=="object"&&n in t)t=t[n];else return null}return typeof t=="object"&&t!==null?x(t):t??null}function C(o){return typeof o=="object"&&o!==null&&"value"in o?o.value:o}function d(o,e,t){const r=C(o);r[e]=t}function g(o,e){return C(o)[e]}function R(o){const e=o?.generalLocaleCache??L,t=o?.routeLocaleCache??T,r=o?.dynamicTranslationsCaches??N,n=o?.serverTranslationCache??w;return{hasCache(s,i){const a=`${s}:${i}`;return(g(n,a)??new Map).size>0},getCache(s,i){const a=`${s}:${i}`;return g(n,a)},setCache(s,i,a){const l=`${s}:${i}`;d(n,l,a)},mergeTranslation(s,i,a,l=!1){const c=`${s}:${i}`,u=g(t,c);(u||l)&&d(t,c,{...u??{},...a});const h=process.env.NODE_ENV!=="production";!u&&h&&console.warn(`[i18n] mergeTranslation called for '${c}' which was not pre-loaded. Skipping merge. Use force: true if this is intentional.`)},mergeGlobalTranslation(s,i,a=!1){const l=g(e,s);!a&&!l&&console.error(`marge: route ${s} not loaded`),d(e,s,{...l??{},...i})},hasGeneralTranslation(s){return!!g(e,s)},hasPageTranslation(s,i){const a=`${s}:${i}`;return!!g(t,a)},hasTranslation:(s,i)=>{const a=C(r);for(const c of a)if(v(c[s]||null,i)!==null)return!0;const l=g(e,s);return v(l||null,i)!==null},getTranslation:(s,i,a)=>{const l=`${s}:${i}`,c=g(n,l),u=c?.get(a);if(u)return u;let h=null;const y=C(r);for(const m of y)if(h=v(m[s]||null,a),h!==null)break;if(!h){const m=g(t,l),S=g(e,s);h=v(m||null,a)??v(S||null,a)}if(h){const m=c??new Map;m.set(a,h),d(n,l,m)}return h},async loadPageTranslations(s,i,a){const l=`${s}:${i}`;d(t,l,{...a})},async loadTranslations(s,i){d(e,s,{...i})},clearCache(){const s=C(e);Object.keys(s).forEach(c=>{d(e,c,{})});const i=C(t);Object.keys(i).forEach(c=>{d(t,c,{})});const a=C(r);a.length=0;const l=C(n);Object.keys(l).forEach(c=>{g(n,c)?.clear()})}}}function b(o,e){let t=o;for(const r in e)t=t.split(`{${r}}`).join(String(e[r]));return t}function p(o){return o==="prefix"||o==="prefix_and_default"}function f(o){return o==="no_prefix"}function D(o){return o==="prefix"}function F(o){return o==="prefix_except_default"}function q(o){return o==="prefix_and_default"}const $=(o,e,t,r,n)=>{const s=n(o,t);if(!s)return null;const i=s.toString().split("|");if(i.length===0)return null;const a=e<i.length?i[e]:i[i.length-1];return a?a.trim().replace("{count}",e.toString()):null};class z{constructor(e,t,r,n,s,i,a=null,l=null){this.i18nConfig=e,this.router=t,this.hashLocaleDefault=r,this.noPrefixDefault=n,this.navigateTo=s,this.setCookie=i,this.cookieLocaleDefault=a,this.cookieLocaleName=l}extractLocaleFromPath(e){if(!e)return null;const r=e.split("?")[0]?.split("#")[0];if(!r||r==="/")return null;const n=r.split("/").filter(Boolean);if(n.length===0)return null;const s=n[0];return s&&(this.i18nConfig.locales?.map(a=>a.code)||[]).includes(s)?s:null}getCurrentLocale(e){if(e=e??this.router.currentRoute.value,this.i18nConfig.hashMode&&this.hashLocaleDefault)return this.hashLocaleDefault;if(f(this.i18nConfig.strategy)&&this.noPrefixDefault)return this.noPrefixDefault;if(e.params?.locale)return e.params.locale.toString();const t=e.path||e.fullPath||"",r=this.extractLocaleFromPath(t);return r||(this.cookieLocaleDefault?this.cookieLocaleDefault:(this.i18nConfig.defaultLocale||"en").toString())}getCurrentName(e){const t=this.getCurrentLocale(e);return this.i18nConfig.locales?.find(n=>n.code===t)?.displayName??null}getRouteName(e,t){return(e.name??"").toString().toString().replace("localized-","").replace(new RegExp(`-${t}$`),"")}getPluginRouteName(e,t){return this.i18nConfig.disablePageLocales?"general":this.getRouteName(e,t)}getFullPathWithBaseUrl(e,t){let n=this.router.resolve(t).fullPath;e?.baseDefault&&(n=n.replace(new RegExp(`^/${e.code}`),""));let s=e.baseUrl;return s||(s=""),s?.endsWith("/")&&(s=s.slice(0,-1)),s+n}switchLocaleRoute(e,t,r,n){const s=this.i18nConfig.locales?.find(h=>h.code===t),i=this.getRouteName(r,e);if(this.router.hasRoute(`localized-${i}-${t}`)){const y={...n?.[t]?{...n[t]}:{...r.params??{}}};delete y.locale,f(this.i18nConfig.strategy)||(y.locale=t);const m={name:`localized-${i}-${t}`,params:y,query:r.query,hash:r.hash};return s?.baseUrl?this.getFullPathWithBaseUrl(s,m):m}let a=i;const c={...n?.[t]?{...n[t]}:{...r.params??{}}};delete c.locale,f(this.i18nConfig.strategy)||(i==="custom-fallback-route"?a=i:a=t!==this.i18nConfig.defaultLocale||p(this.i18nConfig.strategy)?`localized-${i}`:i,f(this.i18nConfig.strategy)||(t!==this.i18nConfig.defaultLocale||p(this.i18nConfig.strategy))&&(c.locale=t));const u={name:a,params:c,query:r.query,hash:r.hash};return f(this.i18nConfig.strategy)&&this.i18nConfig.locales?.forEach((h,y)=>{u.name.endsWith(`-${h.code}`)&&(u.name=u.name.slice(0,-h.code-1))}),s?.baseUrl?this.getFullPathWithBaseUrl(s,u):u}resolveParams(e){const t=typeof e=="object"&&"params"in e&&typeof e.params=="object"?{...e.params}:{};if(typeof e=="string"){const r=this.router.resolve(e);r&&r.params&&Object.assign(t,r.params)}return t}handlePrefixStrategy(e){if(!p(this.i18nConfig.strategy))return e;const t=this.i18nConfig.defaultLocale;let r=e;typeof e=="string"&&(r=this.router.resolve("/"+t+e));const n=this.getRouteName(r,t),s=this.resolveParams(r);return f(this.i18nConfig.strategy)||(s.locale=t),this.router.hasRoute(`localized-${n}`)?this.router.resolve({name:`localized-${n}`,query:r.query,hash:r.hash,params:s}):this.router.hasRoute(`localized-${n}-${t}`)?this.router.resolve({name:`localized-${n}-${t}`,query:r.query,hash:r.hash,params:s}):e}createLocalizedRoute(e,t,r){const n=this.router.resolve(e),s=this.getRouteName(n,r).replace(new RegExp(`-${this.i18nConfig.defaultLocale}$`),"");if(!f(this.i18nConfig.strategy)&&(!s||s==="")){let c=this.router.resolve(e).path.replace(new RegExp(`^/${r}/`),"/");return(r!==this.i18nConfig.defaultLocale||p(this.i18nConfig.strategy))&&(c="/"+r+c),this.router.resolve({path:c,query:n.query,hash:n.hash})}if(this.router.hasRoute(`localized-${s}-${r}`)){const l=this.resolveParams(n);return f(this.i18nConfig.strategy)||(l.locale=r),this.router.resolve({name:`localized-${s}-${r}`,params:l,query:n.query,hash:n.hash})}const i=r!==this.i18nConfig.defaultLocale||p(this.i18nConfig.strategy)?`localized-${s}`:s;if(!this.router.hasRoute(i)){const l=this.resolveParams(e);return delete l.locale,this.router.hasRoute(s)?this.router.resolve({name:s,params:l,query:n.query,hash:n.hash}):this.router.resolve("/")}const a=this.resolveParams(e);return delete a.locale,f(this.i18nConfig.strategy)||(r!==this.i18nConfig.defaultLocale||p(this.i18nConfig.strategy))&&(a.locale=r),this.router.resolve({name:i,params:a,query:n.query,hash:n.hash})}getLocalizedRoute(e,t,r){const n=r||this.getCurrentLocale(t),s=this.handlePrefixStrategy(e);return this.createLocalizedRoute(s,t,n)}updateCookies(e){const t=this.cookieLocaleName||this.i18nConfig.localeCookie||"user-locale";this.i18nConfig.hashMode&&(this.setCookie("hash-locale",e),this.hashLocaleDefault=e),f(this.i18nConfig.strategy)&&(this.setCookie(t,e),this.noPrefixDefault=e),!this.i18nConfig.hashMode&&!f(this.i18nConfig.strategy)&&t&&(this.setCookie(t,e),this.cookieLocaleDefault=e)}getCurrentRoute(){return this.router.currentRoute.value}resolveRouteWithStrategy(e,t,r){return f(this.i18nConfig.strategy)?this.router.resolve(e):t!==this.i18nConfig.defaultLocale||p(this.i18nConfig.strategy)?this.router.resolve(`/${r}${e}`):this.router.resolve(e)}switchLocaleLogic(e,t,r){const n=this.getCurrentLocale();let s;typeof r=="string"?s=this.resolveRouteWithStrategy(r,e,n):s=r??this.getCurrentRoute(),this.updateCookies(e);const i=this.switchLocaleRoute(n,e,s,t);return typeof i=="string"&&i.startsWith("http")?this.navigateTo(i,{redirectCode:200,external:!0}):(f(this.i18nConfig.strategy)&&(i.force=!0),this.router.push(i))}resolveLocalizedRoute(e,t){const r=this.getCurrentRoute(),n=this.getCurrentLocale(),s=t??n;let i;if(typeof e=="string")if(e.startsWith("/"))i=this.resolveRouteWithStrategy(e,s,n);else{const a=e;this.router.hasRoute(a)?i=this.router.resolve({name:a}):(e=`/${e}`,i=this.resolveRouteWithStrategy(e,s,n))}else i=e;return this.getLocalizedRoute(i,r,s)}}class P{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),a=[{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 a){const u=Math.floor(i/c);if(u>=1)return new Intl.RelativeTimeFormat(t,r).format(-u,l)}return new Intl.RelativeTimeFormat(t,r).format(0,"second")}}class E{constructor(e={}){this.formatter=new P,this.helper=R(e.cache),this.formatter=new P,this.pluralFunc=e.plural||$,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 s=this.getLocale(),i=n||this.getRoute();let a=this.helper.getTranslation(s,i,e);if(!a&&this.enablePreviousPageFallback&&this.getPreviousPageInfo){const l=this.getPreviousPageInfo();if(l){const c=this.helper.getTranslation(l.locale,l.routeName,e);c&&(a=c,process.env.NODE_ENV!=="production"&&console.log(`Using fallback translation from previous route: ${l.routeName} -> ${e}`))}}if(!a){const l=this.getFallbackLocale();s!==l&&(a=this.helper.getTranslation(l,i,e))}if(!a){const l=this.getCustomMissingHandler?.();l?l(s,e,i):this.missingHandler?this.missingHandler(s,e,i):this.missingWarn&&process.env.NODE_ENV!=="production"&&typeof window<"u"&&console.warn(`Not found '${e}' key in '${s}' locale messages for route '${i}'.`),a=r===void 0?e:r||e}return typeof a=="string"&&t?b(a,t):a}ts(e,t,r,n){return this.t(e,t,r,n)?.toString()??r??e}tc(e,t,r){const{count:n,...s}=typeof t=="number"?{count:t}:t;if(n===void 0)return r??e;const i=(l,c,u)=>this.t(l,c,u);return this.pluralFunc(e,Number.parseInt(n.toString()),s,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.loadTranslations(e,t)}loadRouteTranslationsCore(e,t,r,n){n?this.helper.mergeTranslation(e,t,r,!0):this.helper.loadPageTranslations(e,t,r)}}exports.BaseI18n=E;exports.FormatService=P;exports.RouteService=z;exports.defaultPlural=$;exports.interpolate=b;exports.isNoPrefixStrategy=f;exports.isPrefixAndDefaultStrategy=q;exports.isPrefixExceptDefaultStrategy=F;exports.isPrefixStrategy=D;exports.useTranslationHelper=R;exports.withPrefixStrategy=p;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});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 h(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 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 m(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 d=(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};class N{constructor(e={}){this.formatter=new g,this.helper=h(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}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?m(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()),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=d;exports.interpolate=m;exports.isNoPrefixStrategy=T;exports.isPrefixAndDefaultStrategy=x;exports.isPrefixExceptDefaultStrategy=b;exports.isPrefixStrategy=P;exports.useTranslationHelper=h;exports.withPrefixStrategy=v;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { useTranslationHelper,
|
|
2
|
-
import { RouteService } from './route-service';
|
|
1
|
+
import { useTranslationHelper, TranslationStorage } from './translation';
|
|
3
2
|
import { FormatService } from './format-service';
|
|
4
3
|
import { interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural } from './helpers';
|
|
5
4
|
import { BaseI18n, BaseI18nOptions } from './base';
|
|
6
|
-
export { useTranslationHelper, interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural,
|
|
5
|
+
export { useTranslationHelper, interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural, FormatService, BaseI18n, type TranslationStorage, type BaseI18nOptions, };
|
package/dist/index.mjs
CHANGED
|
@@ -1,338 +1,86 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
t = t[n];
|
|
16
|
-
else
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
return typeof t == "object" && t !== null ? N(t) : t ?? null;
|
|
20
|
-
}
|
|
21
|
-
function p(o) {
|
|
22
|
-
return typeof o == "object" && o !== null && "value" in o ? o.value : o;
|
|
23
|
-
}
|
|
24
|
-
function d(o, e, t) {
|
|
25
|
-
const r = p(o);
|
|
26
|
-
r[e] = t;
|
|
27
|
-
}
|
|
28
|
-
function g(o, e) {
|
|
29
|
-
return p(o)[e];
|
|
1
|
+
function f(s, e) {
|
|
2
|
+
if (!s || typeof e != "string") return null;
|
|
3
|
+
if (e in s) {
|
|
4
|
+
const r = s[e];
|
|
5
|
+
return r;
|
|
6
|
+
}
|
|
7
|
+
const t = e.split(".");
|
|
8
|
+
let n = s;
|
|
9
|
+
for (const r of t)
|
|
10
|
+
if (n && typeof n == "object" && r in n)
|
|
11
|
+
n = n[r];
|
|
12
|
+
else
|
|
13
|
+
return null;
|
|
14
|
+
return n ?? null;
|
|
30
15
|
}
|
|
31
|
-
function
|
|
32
|
-
const e =
|
|
16
|
+
function h(s) {
|
|
17
|
+
const e = s?.translations ?? /* @__PURE__ */ new Map();
|
|
33
18
|
return {
|
|
34
|
-
hasCache(
|
|
35
|
-
const
|
|
36
|
-
return (
|
|
19
|
+
hasCache(t, n) {
|
|
20
|
+
const r = `${t}:${n}`;
|
|
21
|
+
return e.has(r) || e.has(t);
|
|
37
22
|
},
|
|
38
|
-
getCache(
|
|
39
|
-
const
|
|
40
|
-
return
|
|
23
|
+
getCache(t, n) {
|
|
24
|
+
const r = `${t}:${n}`;
|
|
25
|
+
return e.get(r);
|
|
41
26
|
},
|
|
42
|
-
setCache(
|
|
43
|
-
const l = `${s}:${i}`;
|
|
44
|
-
d(n, l, a);
|
|
27
|
+
setCache(t, n, r) {
|
|
45
28
|
},
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
const h = process.env.NODE_ENV !== "production";
|
|
53
|
-
!u && h && console.warn(`[i18n] mergeTranslation called for '${c}' which was not pre-loaded. Skipping merge. Use force: true if this is intentional.`);
|
|
29
|
+
hasTranslation(t, n) {
|
|
30
|
+
for (const [r, a] of e)
|
|
31
|
+
if ((r === t || r.startsWith(`${t}:`)) && f(a, n) !== null)
|
|
32
|
+
return !0;
|
|
33
|
+
return !1;
|
|
54
34
|
},
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
!a && !l && console.error(`marge: route ${s} not loaded`), d(e, s, {
|
|
58
|
-
...l ?? {},
|
|
59
|
-
...i
|
|
60
|
-
});
|
|
35
|
+
hasGeneralTranslation(t) {
|
|
36
|
+
return e.has(t);
|
|
61
37
|
},
|
|
62
|
-
|
|
63
|
-
return
|
|
38
|
+
hasPageTranslation(t, n) {
|
|
39
|
+
return e.has(`${t}:${n}`);
|
|
64
40
|
},
|
|
65
|
-
|
|
66
|
-
const a = `${
|
|
67
|
-
|
|
41
|
+
getTranslation(t, n, r) {
|
|
42
|
+
const a = `${t}:${n}`, i = e.get(a), o = f(i, r);
|
|
43
|
+
if (o !== null) return o;
|
|
44
|
+
const l = e.get(t);
|
|
45
|
+
return f(l, r);
|
|
68
46
|
},
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
if (v(c[s] || null, i) !== null)
|
|
73
|
-
return !0;
|
|
74
|
-
const l = g(e, s);
|
|
75
|
-
return v(l || null, i) !== null;
|
|
47
|
+
loadTranslations(t, n) {
|
|
48
|
+
const r = e.get(t) ?? {};
|
|
49
|
+
e.set(t, { ...r, ...n });
|
|
76
50
|
},
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (u)
|
|
80
|
-
return u;
|
|
81
|
-
let h = null;
|
|
82
|
-
const C = p(r);
|
|
83
|
-
for (const m of C)
|
|
84
|
-
if (h = v(m[s] || null, a), h !== null) break;
|
|
85
|
-
if (!h) {
|
|
86
|
-
const m = g(t, l), R = g(e, s);
|
|
87
|
-
h = v(m || null, a) ?? v(R || null, a);
|
|
88
|
-
}
|
|
89
|
-
if (h) {
|
|
90
|
-
const m = c ?? /* @__PURE__ */ new Map();
|
|
91
|
-
m.set(a, h), d(n, l, m);
|
|
92
|
-
}
|
|
93
|
-
return h;
|
|
51
|
+
setTranslations(t, n) {
|
|
52
|
+
e.set(t, n);
|
|
94
53
|
},
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
54
|
+
loadPageTranslations(t, n, r) {
|
|
55
|
+
const a = `${t}:${n}`, i = e.get(a);
|
|
56
|
+
!i || Object.keys(i).length === 0 ? e.set(a, r) : e.set(a, { ...i, ...r });
|
|
98
57
|
},
|
|
99
|
-
|
|
100
|
-
|
|
58
|
+
mergeTranslation(t, n, r, a = !1) {
|
|
59
|
+
const i = `${t}:${n}`, o = e.get(i) ?? {};
|
|
60
|
+
e.set(i, { ...o, ...r });
|
|
61
|
+
},
|
|
62
|
+
mergeGlobalTranslation(t, n, r = !1) {
|
|
63
|
+
const a = e.get(t) ?? {};
|
|
64
|
+
e.set(t, { ...a, ...n });
|
|
101
65
|
},
|
|
102
66
|
clearCache() {
|
|
103
|
-
|
|
104
|
-
Object.keys(s).forEach((c) => {
|
|
105
|
-
d(e, c, {});
|
|
106
|
-
});
|
|
107
|
-
const i = p(t);
|
|
108
|
-
Object.keys(i).forEach((c) => {
|
|
109
|
-
d(t, c, {});
|
|
110
|
-
});
|
|
111
|
-
const a = p(r);
|
|
112
|
-
a.length = 0;
|
|
113
|
-
const l = p(n);
|
|
114
|
-
Object.keys(l).forEach((c) => {
|
|
115
|
-
g(n, c)?.clear();
|
|
116
|
-
});
|
|
67
|
+
e.clear();
|
|
117
68
|
}
|
|
118
69
|
};
|
|
119
70
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return o === "prefix";
|
|
134
|
-
}
|
|
135
|
-
function F(o) {
|
|
136
|
-
return o === "prefix_except_default";
|
|
137
|
-
}
|
|
138
|
-
function q(o) {
|
|
139
|
-
return o === "prefix_and_default";
|
|
140
|
-
}
|
|
141
|
-
const x = (o, e, t, r, n) => {
|
|
142
|
-
const s = n(o, t);
|
|
143
|
-
if (!s)
|
|
144
|
-
return null;
|
|
145
|
-
const i = s.toString().split("|");
|
|
146
|
-
if (i.length === 0) return null;
|
|
147
|
-
const a = e < i.length ? i[e] : i[i.length - 1];
|
|
148
|
-
return a ? a.trim().replace("{count}", e.toString()) : null;
|
|
149
|
-
};
|
|
150
|
-
class z {
|
|
151
|
-
constructor(e, t, r, n, s, i, a = null, l = null) {
|
|
152
|
-
this.i18nConfig = e, this.router = t, this.hashLocaleDefault = r, this.noPrefixDefault = n, this.navigateTo = s, this.setCookie = i, this.cookieLocaleDefault = a, this.cookieLocaleName = l;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Extracts locale from URL path by checking the first path segment
|
|
156
|
-
* @param path - URL path (e.g., '/ru/sdfsdf' or '/en/about')
|
|
157
|
-
* @returns Locale code or null if not found
|
|
158
|
-
*/
|
|
159
|
-
extractLocaleFromPath(e) {
|
|
160
|
-
if (!e)
|
|
161
|
-
return null;
|
|
162
|
-
const r = e.split("?")[0]?.split("#")[0];
|
|
163
|
-
if (!r || r === "/")
|
|
164
|
-
return null;
|
|
165
|
-
const n = r.split("/").filter(Boolean);
|
|
166
|
-
if (n.length === 0)
|
|
167
|
-
return null;
|
|
168
|
-
const s = n[0];
|
|
169
|
-
return s && (this.i18nConfig.locales?.map((a) => a.code) || []).includes(s) ? s : null;
|
|
170
|
-
}
|
|
171
|
-
getCurrentLocale(e) {
|
|
172
|
-
if (e = e ?? this.router.currentRoute.value, this.i18nConfig.hashMode && this.hashLocaleDefault)
|
|
173
|
-
return this.hashLocaleDefault;
|
|
174
|
-
if (f(this.i18nConfig.strategy) && this.noPrefixDefault)
|
|
175
|
-
return this.noPrefixDefault;
|
|
176
|
-
if (e.params?.locale)
|
|
177
|
-
return e.params.locale.toString();
|
|
178
|
-
const t = e.path || e.fullPath || "", r = this.extractLocaleFromPath(t);
|
|
179
|
-
return r || (this.cookieLocaleDefault ? this.cookieLocaleDefault : (this.i18nConfig.defaultLocale || "en").toString());
|
|
180
|
-
}
|
|
181
|
-
getCurrentName(e) {
|
|
182
|
-
const t = this.getCurrentLocale(e);
|
|
183
|
-
return this.i18nConfig.locales?.find((n) => n.code === t)?.displayName ?? null;
|
|
184
|
-
}
|
|
185
|
-
getRouteName(e, t) {
|
|
186
|
-
return (e.name ?? "").toString().toString().replace("localized-", "").replace(new RegExp(`-${t}$`), "");
|
|
187
|
-
}
|
|
188
|
-
getPluginRouteName(e, t) {
|
|
189
|
-
return this.i18nConfig.disablePageLocales ? "general" : this.getRouteName(e, t);
|
|
190
|
-
}
|
|
191
|
-
getFullPathWithBaseUrl(e, t) {
|
|
192
|
-
let n = this.router.resolve(t).fullPath;
|
|
193
|
-
e?.baseDefault && (n = n.replace(new RegExp(`^/${e.code}`), ""));
|
|
194
|
-
let s = e.baseUrl;
|
|
195
|
-
return s || (s = ""), s?.endsWith("/") && (s = s.slice(0, -1)), s + n;
|
|
196
|
-
}
|
|
197
|
-
switchLocaleRoute(e, t, r, n) {
|
|
198
|
-
const s = this.i18nConfig.locales?.find((h) => h.code === t), i = this.getRouteName(r, e);
|
|
199
|
-
if (this.router.hasRoute(`localized-${i}-${t}`)) {
|
|
200
|
-
const C = { ...n?.[t] ? { ...n[t] } : { ...r.params ?? {} } };
|
|
201
|
-
delete C.locale, f(this.i18nConfig.strategy) || (C.locale = t);
|
|
202
|
-
const m = {
|
|
203
|
-
name: `localized-${i}-${t}`,
|
|
204
|
-
params: C,
|
|
205
|
-
query: r.query,
|
|
206
|
-
hash: r.hash
|
|
207
|
-
};
|
|
208
|
-
return s?.baseUrl ? this.getFullPathWithBaseUrl(s, m) : m;
|
|
209
|
-
}
|
|
210
|
-
let a = i;
|
|
211
|
-
const c = { ...n?.[t] ? { ...n[t] } : { ...r.params ?? {} } };
|
|
212
|
-
delete c.locale, f(this.i18nConfig.strategy) || (i === "custom-fallback-route" ? a = i : a = t !== this.i18nConfig.defaultLocale || y(this.i18nConfig.strategy) ? `localized-${i}` : i, f(this.i18nConfig.strategy) || (t !== this.i18nConfig.defaultLocale || y(this.i18nConfig.strategy)) && (c.locale = t));
|
|
213
|
-
const u = {
|
|
214
|
-
name: a,
|
|
215
|
-
params: c,
|
|
216
|
-
query: r.query,
|
|
217
|
-
hash: r.hash
|
|
218
|
-
};
|
|
219
|
-
return f(this.i18nConfig.strategy) && this.i18nConfig.locales?.forEach((h, C) => {
|
|
220
|
-
u.name.endsWith(`-${h.code}`) && (u.name = u.name.slice(0, -h.code - 1));
|
|
221
|
-
}), s?.baseUrl ? this.getFullPathWithBaseUrl(s, u) : u;
|
|
222
|
-
}
|
|
223
|
-
resolveParams(e) {
|
|
224
|
-
const t = typeof e == "object" && "params" in e && typeof e.params == "object" ? { ...e.params } : {};
|
|
225
|
-
if (typeof e == "string") {
|
|
226
|
-
const r = this.router.resolve(e);
|
|
227
|
-
r && r.params && Object.assign(t, r.params);
|
|
228
|
-
}
|
|
229
|
-
return t;
|
|
230
|
-
}
|
|
231
|
-
handlePrefixStrategy(e) {
|
|
232
|
-
if (!y(this.i18nConfig.strategy))
|
|
233
|
-
return e;
|
|
234
|
-
const t = this.i18nConfig.defaultLocale;
|
|
235
|
-
let r = e;
|
|
236
|
-
typeof e == "string" && (r = this.router.resolve("/" + t + e));
|
|
237
|
-
const n = this.getRouteName(r, t), s = this.resolveParams(r);
|
|
238
|
-
return f(this.i18nConfig.strategy) || (s.locale = t), this.router.hasRoute(`localized-${n}`) ? this.router.resolve({
|
|
239
|
-
name: `localized-${n}`,
|
|
240
|
-
query: r.query,
|
|
241
|
-
hash: r.hash,
|
|
242
|
-
params: s
|
|
243
|
-
}) : this.router.hasRoute(`localized-${n}-${t}`) ? this.router.resolve({
|
|
244
|
-
name: `localized-${n}-${t}`,
|
|
245
|
-
query: r.query,
|
|
246
|
-
hash: r.hash,
|
|
247
|
-
params: s
|
|
248
|
-
}) : e;
|
|
249
|
-
}
|
|
250
|
-
createLocalizedRoute(e, t, r) {
|
|
251
|
-
const n = this.router.resolve(e), s = this.getRouteName(n, r).replace(new RegExp(`-${this.i18nConfig.defaultLocale}$`), "");
|
|
252
|
-
if (!f(this.i18nConfig.strategy) && (!s || s === "")) {
|
|
253
|
-
let c = this.router.resolve(e).path.replace(new RegExp(`^/${r}/`), "/");
|
|
254
|
-
return (r !== this.i18nConfig.defaultLocale || y(this.i18nConfig.strategy)) && (c = "/" + r + c), this.router.resolve({
|
|
255
|
-
path: c,
|
|
256
|
-
query: n.query,
|
|
257
|
-
hash: n.hash
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
if (this.router.hasRoute(`localized-${s}-${r}`)) {
|
|
261
|
-
const l = this.resolveParams(n);
|
|
262
|
-
return f(this.i18nConfig.strategy) || (l.locale = r), this.router.resolve({
|
|
263
|
-
name: `localized-${s}-${r}`,
|
|
264
|
-
params: l,
|
|
265
|
-
query: n.query,
|
|
266
|
-
hash: n.hash
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
const i = r !== this.i18nConfig.defaultLocale || y(this.i18nConfig.strategy) ? `localized-${s}` : s;
|
|
270
|
-
if (!this.router.hasRoute(i)) {
|
|
271
|
-
const l = this.resolveParams(e);
|
|
272
|
-
return delete l.locale, this.router.hasRoute(s) ? this.router.resolve({
|
|
273
|
-
name: s,
|
|
274
|
-
params: l,
|
|
275
|
-
query: n.query,
|
|
276
|
-
hash: n.hash
|
|
277
|
-
}) : this.router.resolve("/");
|
|
278
|
-
}
|
|
279
|
-
const a = this.resolveParams(e);
|
|
280
|
-
return delete a.locale, f(this.i18nConfig.strategy) || (r !== this.i18nConfig.defaultLocale || y(this.i18nConfig.strategy)) && (a.locale = r), this.router.resolve({
|
|
281
|
-
name: i,
|
|
282
|
-
params: a,
|
|
283
|
-
query: n.query,
|
|
284
|
-
hash: n.hash
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
getLocalizedRoute(e, t, r) {
|
|
288
|
-
const n = r || this.getCurrentLocale(t), s = this.handlePrefixStrategy(e);
|
|
289
|
-
return this.createLocalizedRoute(s, t, n);
|
|
290
|
-
}
|
|
291
|
-
updateCookies(e) {
|
|
292
|
-
const t = this.cookieLocaleName || this.i18nConfig.localeCookie || "user-locale";
|
|
293
|
-
this.i18nConfig.hashMode && (this.setCookie("hash-locale", e), this.hashLocaleDefault = e), f(this.i18nConfig.strategy) && (this.setCookie(t, e), this.noPrefixDefault = e), !this.i18nConfig.hashMode && !f(this.i18nConfig.strategy) && t && (this.setCookie(t, e), this.cookieLocaleDefault = e);
|
|
294
|
-
}
|
|
295
|
-
getCurrentRoute() {
|
|
296
|
-
return this.router.currentRoute.value;
|
|
297
|
-
}
|
|
298
|
-
resolveRouteWithStrategy(e, t, r) {
|
|
299
|
-
return f(this.i18nConfig.strategy) ? this.router.resolve(e) : t !== this.i18nConfig.defaultLocale || y(this.i18nConfig.strategy) ? this.router.resolve(`/${r}${e}`) : this.router.resolve(e);
|
|
300
|
-
}
|
|
301
|
-
switchLocaleLogic(e, t, r) {
|
|
302
|
-
const n = this.getCurrentLocale();
|
|
303
|
-
let s;
|
|
304
|
-
typeof r == "string" ? s = this.resolveRouteWithStrategy(r, e, n) : s = r ?? this.getCurrentRoute(), this.updateCookies(e);
|
|
305
|
-
const i = this.switchLocaleRoute(n, e, s, t);
|
|
306
|
-
return typeof i == "string" && i.startsWith("http") ? this.navigateTo(i, { redirectCode: 200, external: !0 }) : (f(this.i18nConfig.strategy) && (i.force = !0), this.router.push(i));
|
|
307
|
-
}
|
|
308
|
-
resolveLocalizedRoute(e, t) {
|
|
309
|
-
const r = this.getCurrentRoute(), n = this.getCurrentLocale(), s = t ?? n;
|
|
310
|
-
let i;
|
|
311
|
-
if (typeof e == "string")
|
|
312
|
-
if (e.startsWith("/"))
|
|
313
|
-
i = this.resolveRouteWithStrategy(e, s, n);
|
|
314
|
-
else {
|
|
315
|
-
const a = e;
|
|
316
|
-
this.router.hasRoute(a) ? i = this.router.resolve({ name: a }) : (e = `/${e}`, i = this.resolveRouteWithStrategy(e, s, n));
|
|
317
|
-
}
|
|
318
|
-
else
|
|
319
|
-
i = e;
|
|
320
|
-
return this.getLocalizedRoute(i, r, s);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
class P {
|
|
324
|
-
formatNumber(e, t, r) {
|
|
325
|
-
return new Intl.NumberFormat(t, r).format(e);
|
|
326
|
-
}
|
|
327
|
-
formatDate(e, t, r) {
|
|
328
|
-
const n = new Date(e);
|
|
329
|
-
return Number.isNaN(n.getTime()) ? "Invalid Date" : new Intl.DateTimeFormat(t, r).format(n);
|
|
330
|
-
}
|
|
331
|
-
formatRelativeTime(e, t, r) {
|
|
332
|
-
const n = new Date(e);
|
|
333
|
-
if (Number.isNaN(n.getTime()))
|
|
334
|
-
return new Intl.RelativeTimeFormat(t, r).format(0, "second");
|
|
335
|
-
const i = Math.floor(((/* @__PURE__ */ new Date()).getTime() - n.getTime()) / 1e3), a = [
|
|
71
|
+
class g {
|
|
72
|
+
formatNumber(e, t, n) {
|
|
73
|
+
return new Intl.NumberFormat(t, n).format(e);
|
|
74
|
+
}
|
|
75
|
+
formatDate(e, t, n) {
|
|
76
|
+
const r = new Date(e);
|
|
77
|
+
return Number.isNaN(r.getTime()) ? "Invalid Date" : new Intl.DateTimeFormat(t, n).format(r);
|
|
78
|
+
}
|
|
79
|
+
formatRelativeTime(e, t, n) {
|
|
80
|
+
const r = new Date(e);
|
|
81
|
+
if (Number.isNaN(r.getTime()))
|
|
82
|
+
return new Intl.RelativeTimeFormat(t, n).format(0, "second");
|
|
83
|
+
const i = Math.floor(((/* @__PURE__ */ new Date()).getTime() - r.getTime()) / 1e3), o = [
|
|
336
84
|
{ unit: "year", seconds: 31536e3 },
|
|
337
85
|
{ unit: "month", seconds: 2592e3 },
|
|
338
86
|
{ unit: "day", seconds: 86400 },
|
|
@@ -340,65 +88,96 @@ class P {
|
|
|
340
88
|
{ unit: "minute", seconds: 60 },
|
|
341
89
|
{ unit: "second", seconds: 1 }
|
|
342
90
|
];
|
|
343
|
-
for (const { unit: l, seconds:
|
|
344
|
-
const
|
|
345
|
-
if (
|
|
346
|
-
return new Intl.RelativeTimeFormat(t,
|
|
91
|
+
for (const { unit: l, seconds: u } of o) {
|
|
92
|
+
const c = Math.floor(i / u);
|
|
93
|
+
if (c >= 1)
|
|
94
|
+
return new Intl.RelativeTimeFormat(t, n).format(-c, l);
|
|
347
95
|
}
|
|
348
|
-
return new Intl.RelativeTimeFormat(t,
|
|
96
|
+
return new Intl.RelativeTimeFormat(t, n).format(0, "second");
|
|
349
97
|
}
|
|
350
98
|
}
|
|
351
|
-
|
|
99
|
+
const m = /\{(\w+)\}/g;
|
|
100
|
+
function d(s, e) {
|
|
101
|
+
return e ? s.replace(m, (t, n) => {
|
|
102
|
+
const r = e[n];
|
|
103
|
+
return r !== void 0 ? String(r) : `{${n}}`;
|
|
104
|
+
}) : s;
|
|
105
|
+
}
|
|
106
|
+
function v(s) {
|
|
107
|
+
return s === "prefix" || s === "prefix_and_default";
|
|
108
|
+
}
|
|
109
|
+
function T(s) {
|
|
110
|
+
return s === "no_prefix";
|
|
111
|
+
}
|
|
112
|
+
function P(s) {
|
|
113
|
+
return s === "prefix";
|
|
114
|
+
}
|
|
115
|
+
function b(s) {
|
|
116
|
+
return s === "prefix_except_default";
|
|
117
|
+
}
|
|
118
|
+
function N(s) {
|
|
119
|
+
return s === "prefix_and_default";
|
|
120
|
+
}
|
|
121
|
+
const p = (s, e, t, n, r) => {
|
|
122
|
+
const a = r(s, t);
|
|
123
|
+
if (!a)
|
|
124
|
+
return null;
|
|
125
|
+
const i = a.toString().split("|");
|
|
126
|
+
if (i.length === 0) return null;
|
|
127
|
+
const o = e < i.length ? i[e] : i[i.length - 1];
|
|
128
|
+
return o ? o.trim().replace("{count}", e.toString()) : null;
|
|
129
|
+
};
|
|
130
|
+
class $ {
|
|
352
131
|
constructor(e = {}) {
|
|
353
|
-
this.formatter = new
|
|
132
|
+
this.formatter = new g(), this.helper = h(e.storage), this.formatter = new g(), this.pluralFunc = e.plural || p, this.missingWarn = e.missingWarn ?? !0, this.missingHandler = e.missingHandler, this.getPreviousPageInfo = e.getPreviousPageInfo, this.getCustomMissingHandler = e.getCustomMissingHandler, this.enablePreviousPageFallback = e.enablePreviousPageFallback ?? !1;
|
|
354
133
|
}
|
|
355
134
|
// --- Public methods (implemented in base class) ---
|
|
356
135
|
/**
|
|
357
136
|
* Get translation for a key
|
|
358
137
|
* Based on logic from src/runtime/plugins/01.plugin.ts
|
|
359
138
|
*/
|
|
360
|
-
t(e, t,
|
|
139
|
+
t(e, t, n, r) {
|
|
361
140
|
if (!e) return "";
|
|
362
|
-
const
|
|
363
|
-
let
|
|
364
|
-
if (!
|
|
141
|
+
const a = this.getLocale(), i = r || this.getRoute();
|
|
142
|
+
let o = this.helper.getTranslation(a, i, e);
|
|
143
|
+
if (!o && this.enablePreviousPageFallback && this.getPreviousPageInfo) {
|
|
365
144
|
const l = this.getPreviousPageInfo();
|
|
366
145
|
if (l) {
|
|
367
|
-
const
|
|
368
|
-
|
|
146
|
+
const u = this.helper.getTranslation(l.locale, l.routeName, e);
|
|
147
|
+
u && (o = u, process.env.NODE_ENV !== "production" && console.log(`Using fallback translation from previous route: ${l.routeName} -> ${e}`));
|
|
369
148
|
}
|
|
370
149
|
}
|
|
371
|
-
if (!
|
|
150
|
+
if (!o) {
|
|
372
151
|
const l = this.getFallbackLocale();
|
|
373
|
-
|
|
152
|
+
a !== l && (o = this.helper.getTranslation(l, i, e));
|
|
374
153
|
}
|
|
375
|
-
if (!
|
|
154
|
+
if (!o) {
|
|
376
155
|
const l = this.getCustomMissingHandler?.();
|
|
377
|
-
l ? l(
|
|
156
|
+
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;
|
|
378
157
|
}
|
|
379
|
-
return typeof
|
|
158
|
+
return typeof o == "string" && t ? d(o, t) : o;
|
|
380
159
|
}
|
|
381
160
|
/**
|
|
382
161
|
* Get translation as string
|
|
383
162
|
*/
|
|
384
|
-
ts(e, t,
|
|
385
|
-
return this.t(e, t,
|
|
163
|
+
ts(e, t, n, r) {
|
|
164
|
+
return this.t(e, t, n, r)?.toString() ?? n ?? e;
|
|
386
165
|
}
|
|
387
166
|
/**
|
|
388
167
|
* Plural translation
|
|
389
168
|
*/
|
|
390
|
-
tc(e, t,
|
|
391
|
-
const { count:
|
|
392
|
-
if (
|
|
393
|
-
return
|
|
394
|
-
const i = (l,
|
|
169
|
+
tc(e, t, n) {
|
|
170
|
+
const { count: r, ...a } = typeof t == "number" ? { count: t } : t;
|
|
171
|
+
if (r === void 0)
|
|
172
|
+
return n ?? e;
|
|
173
|
+
const i = (l, u, c) => this.t(l, u, c);
|
|
395
174
|
return this.pluralFunc(
|
|
396
175
|
e,
|
|
397
|
-
Number.parseInt(
|
|
398
|
-
|
|
176
|
+
Number.parseInt(r.toString()),
|
|
177
|
+
a,
|
|
399
178
|
this.getLocale(),
|
|
400
179
|
i
|
|
401
|
-
) ??
|
|
180
|
+
) ?? n ?? e;
|
|
402
181
|
}
|
|
403
182
|
/**
|
|
404
183
|
* Format number
|
|
@@ -423,8 +202,8 @@ class E {
|
|
|
423
202
|
* Based on logic from src/runtime/plugins/01.plugin.ts
|
|
424
203
|
*/
|
|
425
204
|
has(e, t) {
|
|
426
|
-
const
|
|
427
|
-
return !!this.helper.getTranslation(
|
|
205
|
+
const n = t || this.getRoute(), r = this.getLocale();
|
|
206
|
+
return !!this.helper.getTranslation(r, n, e);
|
|
428
207
|
}
|
|
429
208
|
/**
|
|
430
209
|
* Clear cache
|
|
@@ -437,27 +216,26 @@ class E {
|
|
|
437
216
|
* Core translation loading logic (without reactivity)
|
|
438
217
|
* Subclasses can override addTranslations/addRouteTranslations to add reactivity
|
|
439
218
|
*/
|
|
440
|
-
loadTranslationsCore(e, t,
|
|
441
|
-
|
|
219
|
+
loadTranslationsCore(e, t, n) {
|
|
220
|
+
n ? this.helper.mergeGlobalTranslation(e, t, !0) : this.helper.setTranslations(e, t);
|
|
442
221
|
}
|
|
443
222
|
/**
|
|
444
223
|
* Core route translation loading logic (without reactivity)
|
|
445
224
|
* Subclasses can override addRouteTranslations to add reactivity
|
|
446
225
|
*/
|
|
447
|
-
loadRouteTranslationsCore(e, t,
|
|
448
|
-
|
|
226
|
+
loadRouteTranslationsCore(e, t, n, r) {
|
|
227
|
+
r ? this.helper.mergeTranslation(e, t, n, !0) : this.helper.loadPageTranslations(e, t, n);
|
|
449
228
|
}
|
|
450
229
|
}
|
|
451
230
|
export {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
y as withPrefixStrategy
|
|
231
|
+
$ as BaseI18n,
|
|
232
|
+
g as FormatService,
|
|
233
|
+
p as defaultPlural,
|
|
234
|
+
d as interpolate,
|
|
235
|
+
T as isNoPrefixStrategy,
|
|
236
|
+
N as isPrefixAndDefaultStrategy,
|
|
237
|
+
b as isPrefixExceptDefaultStrategy,
|
|
238
|
+
P as isPrefixStrategy,
|
|
239
|
+
h as useTranslationHelper,
|
|
240
|
+
v as withPrefixStrategy
|
|
463
241
|
};
|