@i18n-micro/core 1.0.28 → 1.1.0
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 -2
- package/dist/index.mjs +200 -236
- package/dist/route-service.d.ts +6 -5
- 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 -2
- package/src/route-service.ts +29 -34
- package/src/translation.ts +71 -181
- package/tests/base.test.ts +11 -36
- package/tests/route-service.test.ts +31 -81
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 d(o,e){if(!o||typeof e!="string")return null;if(e in o){const s=o[e];return s}const t=e.split(".");let r=o;for(const s of t)if(r&&typeof r=="object"&&s in r)r=r[s];else return null;return r??null}function y(o){const e=o?.translations??new Map;return{hasCache(t,r){const s=`${t}:${r}`;return e.has(s)||e.has(t)},getCache(t,r){const s=`${t}:${r}`;return e.get(s)},setCache(t,r,s){},hasTranslation(t,r){for(const[s,i]of e)if((s===t||s.startsWith(`${t}:`))&&d(i,r)!==null)return!0;return!1},hasGeneralTranslation(t){return e.has(t)},hasPageTranslation(t,r){return e.has(`${t}:${r}`)},getTranslation(t,r,s){const i=`${t}:${r}`,n=e.get(i),a=d(n,s);if(a!==null)return a;const l=e.get(t);return d(l,s)},loadTranslations(t,r){const s=e.get(t)??{};e.set(t,{...s,...r})},setTranslations(t,r){e.set(t,r)},loadPageTranslations(t,r,s){const i=`${t}:${r}`,n=e.get(i);!n||Object.keys(n).length===0?e.set(i,s):e.set(i,{...n,...s})},mergeTranslation(t,r,s,i=!1){const n=`${t}:${r}`,a=e.get(n)??{};e.set(n,{...a,...s})},mergeGlobalTranslation(t,r,s=!1){const i=e.get(t)??{};e.set(t,{...i,...r})},clearCache(){e.clear()}}}const L=/\{(\w+)\}/g;function C(o,e){return e?o.replace(L,(t,r)=>{const s=e[r];return s!==void 0?String(s):`{${r}}`}):o}function f(o){return o==="prefix"||o==="prefix_and_default"}function h(o){return o==="no_prefix"}function b(o){return o==="prefix"}function P(o){return o==="prefix_except_default"}function R(o){return o==="prefix_and_default"}const $=(o,e,t,r,s)=>{const i=s(o,t);if(!i)return null;const n=i.toString().split("|");if(n.length===0)return null;const a=e<n.length?n[e]:n[n.length-1];return a?a.trim().replace("{count}",e.toString()):null};class w{constructor(e,t,r,s,i,n){this.i18nConfig=e,this.router=t,this.hashLocaleDefault=r,this.noPrefixDefault=s,this.navigateTo=i,this.getDefaultLocale=n}extractLocaleFromPath(e){if(!e)return null;const r=e.split("?")[0]?.split("#")[0];if(!r||r==="/")return null;const s=r.split("/").filter(Boolean);if(s.length===0)return null;const i=s[0];return i&&(this.i18nConfig.locales?.map(a=>a.code)||[]).includes(i)?i:null}getCurrentLocale(e){if(e=e??this.router.currentRoute.value,this.i18nConfig.hashMode){const i=this.getDefaultLocale?.();if(i)return i;if(this.hashLocaleDefault)return this.hashLocaleDefault}if(h(this.i18nConfig.strategy)){const i=this.getDefaultLocale?.();if(i)return i;if(this.noPrefixDefault)return this.noPrefixDefault}const t=e.path||e.fullPath||"";if(R(this.i18nConfig.strategy)&&(t==="/"||t==="")){const i=this.getDefaultLocale?.();if(i)return i}if(e.params?.locale)return e.params.locale.toString();const r=this.extractLocaleFromPath(t);if(r)return r;if(P(this.i18nConfig.strategy))return(this.i18nConfig.defaultLocale||"en").toString();const s=this.getDefaultLocale?.();return s||(this.i18nConfig.defaultLocale||"en").toString()}getCurrentName(e){const t=this.getCurrentLocale(e);return this.i18nConfig.locales?.find(s=>s.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 s=this.router.resolve(t).fullPath;e?.baseDefault&&(s=s.replace(new RegExp(`^/${e.code}`),""));let i=e.baseUrl;return i||(i=""),i?.endsWith("/")&&(i=i.slice(0,-1)),i+s}switchLocaleRoute(e,t,r,s){const i=this.i18nConfig.locales?.find(g=>g.code===t),n=this.getRouteName(r,e);if(this.router.hasRoute(`localized-${n}-${t}`)){const m={...s?.[t]?{...s[t]}:{...r.params??{}}};delete m.locale,h(this.i18nConfig.strategy)||(m.locale=t);const v={name:`localized-${n}-${t}`,params:m,query:r.query,hash:r.hash};return i?.baseUrl?this.getFullPathWithBaseUrl(i,v):v}let a=n;const u={...s?.[t]?{...s[t]}:{...r.params??{}}};delete u.locale,h(this.i18nConfig.strategy)||(n==="custom-fallback-route"?a=n:a=t!==this.i18nConfig.defaultLocale||f(this.i18nConfig.strategy)?`localized-${n}`:n,h(this.i18nConfig.strategy)||(t!==this.i18nConfig.defaultLocale||f(this.i18nConfig.strategy))&&(u.locale=t));const c={name:a,params:u,query:r.query,hash:r.hash};return h(this.i18nConfig.strategy)&&this.i18nConfig.locales?.forEach((g,m)=>{c.name.endsWith(`-${g.code}`)&&(c.name=c.name.slice(0,-g.code-1))}),i?.baseUrl?this.getFullPathWithBaseUrl(i,c):c}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(!f(this.i18nConfig.strategy))return e;const t=this.i18nConfig.defaultLocale;let r=e;typeof e=="string"&&(r=this.router.resolve("/"+t+e));const s=this.getRouteName(r,t),i=this.resolveParams(r);return h(this.i18nConfig.strategy)||(i.locale=t),this.router.hasRoute(`localized-${s}`)?this.router.resolve({name:`localized-${s}`,query:r.query,hash:r.hash,params:i}):this.router.hasRoute(`localized-${s}-${t}`)?this.router.resolve({name:`localized-${s}-${t}`,query:r.query,hash:r.hash,params:i}):e}createLocalizedRoute(e,t,r){const s=this.router.resolve(e),i=this.getRouteName(s,r).replace(new RegExp(`-${this.i18nConfig.defaultLocale}$`),"");if(!h(this.i18nConfig.strategy)&&(!i||i==="")){let u=this.router.resolve(e).path.replace(new RegExp(`^/${r}/`),"/");return(r!==this.i18nConfig.defaultLocale||f(this.i18nConfig.strategy))&&(u="/"+r+u),this.router.resolve({path:u,query:s.query,hash:s.hash})}if(this.router.hasRoute(`localized-${i}-${r}`)){const l=this.resolveParams(s);return h(this.i18nConfig.strategy)||(l.locale=r),this.router.resolve({name:`localized-${i}-${r}`,params:l,query:s.query,hash:s.hash})}const n=r!==this.i18nConfig.defaultLocale||f(this.i18nConfig.strategy)?`localized-${i}`:i;if(!this.router.hasRoute(n)){const l=this.resolveParams(e);return delete l.locale,this.router.hasRoute(i)?this.router.resolve({name:i,params:l,query:s.query,hash:s.hash}):this.router.resolve("/")}const a=this.resolveParams(e);return delete a.locale,h(this.i18nConfig.strategy)||(r!==this.i18nConfig.defaultLocale||f(this.i18nConfig.strategy))&&(a.locale=r),this.router.resolve({name:n,params:a,query:s.query,hash:s.hash})}getLocalizedRoute(e,t,r){const s=r||this.getCurrentLocale(t),i=this.handlePrefixStrategy(e);return this.createLocalizedRoute(i,t,s)}getCurrentRoute(){return this.router.currentRoute.value}resolveRouteWithStrategy(e,t,r){return h(this.i18nConfig.strategy)?this.router.resolve(e):t!==this.i18nConfig.defaultLocale||f(this.i18nConfig.strategy)?this.router.resolve(`/${r}${e}`):this.router.resolve(e)}switchLocaleLogic(e,t,r){const s=this.getCurrentLocale();let i;typeof r=="string"?i=this.resolveRouteWithStrategy(r,e,s):i=r??this.getCurrentRoute();const n=this.switchLocaleRoute(s,e,i,t);return typeof n=="string"&&n.startsWith("http")?this.navigateTo(n,{redirectCode:200,external:!0}):(h(this.i18nConfig.strategy)&&(n.force=!0),this.router.push(n))}resolveLocalizedRoute(e,t){const r=this.getCurrentRoute(),s=this.getCurrentLocale(),i=t??s;let n;if(typeof e=="string")if(e.startsWith("/"))n=this.resolveRouteWithStrategy(e,i,s);else{const a=e;this.router.hasRoute(a)?n=this.router.resolve({name:a}):(e=`/${e}`,n=this.resolveRouteWithStrategy(e,i,s))}else n=e;return this.getLocalizedRoute(n,r,i)}}class p{formatNumber(e,t,r){return new Intl.NumberFormat(t,r).format(e)}formatDate(e,t,r){const s=new Date(e);return Number.isNaN(s.getTime())?"Invalid Date":new Intl.DateTimeFormat(t,r).format(s)}formatRelativeTime(e,t,r){const s=new Date(e);if(Number.isNaN(s.getTime()))return new Intl.RelativeTimeFormat(t,r).format(0,"second");const n=Math.floor((new Date().getTime()-s.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:u}of a){const c=Math.floor(n/u);if(c>=1)return new Intl.RelativeTimeFormat(t,r).format(-c,l)}return new Intl.RelativeTimeFormat(t,r).format(0,"second")}}class S{constructor(e={}){this.formatter=new p,this.helper=y(e.storage),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,s){if(!e)return"";const i=this.getLocale(),n=s||this.getRoute();let a=this.helper.getTranslation(i,n,e);if(!a&&this.enablePreviousPageFallback&&this.getPreviousPageInfo){const l=this.getPreviousPageInfo();if(l){const u=this.helper.getTranslation(l.locale,l.routeName,e);u&&(a=u,process.env.NODE_ENV!=="production"&&console.log(`Using fallback translation from previous route: ${l.routeName} -> ${e}`))}}if(!a){const l=this.getFallbackLocale();i!==l&&(a=this.helper.getTranslation(l,n,e))}if(!a){const l=this.getCustomMissingHandler?.();l?l(i,e,n):this.missingHandler?this.missingHandler(i,e,n):this.missingWarn&&process.env.NODE_ENV!=="production"&&typeof window<"u"&&console.warn(`Not found '${e}' key in '${i}' locale messages for route '${n}'.`),a=r===void 0?e:r||e}return typeof a=="string"&&t?C(a,t):a}ts(e,t,r,s){return this.t(e,t,r,s)?.toString()??r??e}tc(e,t,r){const{count:s,...i}=typeof t=="number"?{count:t}:t;if(s===void 0)return r??e;const n=(l,u,c)=>this.t(l,u,c);return this.pluralFunc(e,Number.parseInt(s.toString()),i,this.getLocale(),n)??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(),s=this.getLocale();return!!this.helper.getTranslation(s,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,s){s?this.helper.mergeTranslation(e,t,r,!0):this.helper.loadPageTranslations(e,t,r)}}exports.BaseI18n=S;exports.FormatService=p;exports.RouteService=w;exports.defaultPlural=$;exports.interpolate=C;exports.isNoPrefixStrategy=h;exports.isPrefixAndDefaultStrategy=R;exports.isPrefixExceptDefaultStrategy=P;exports.isPrefixStrategy=b;exports.useTranslationHelper=y;exports.withPrefixStrategy=f;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useTranslationHelper,
|
|
1
|
+
import { useTranslationHelper, TranslationStorage } from './translation';
|
|
2
2
|
import { RouteService } from './route-service';
|
|
3
3
|
import { FormatService } from './format-service';
|
|
4
4
|
import { interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural } from './helpers';
|
|
5
5
|
import { BaseI18n, BaseI18nOptions } from './base';
|
|
6
|
-
export { useTranslationHelper, interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural, RouteService, FormatService, BaseI18n, type
|
|
6
|
+
export { useTranslationHelper, interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural, RouteService, FormatService, BaseI18n, type TranslationStorage, type BaseI18nOptions, };
|