@i18n-micro/core
Advanced tools
+2
-2
@@ -1,4 +0,4 @@ | ||
| import { useTranslationHelper, TranslationStorage } from './translation'; | ||
| import { CleanTranslation, MissingHandler, Params, PluralFunc, TranslationKey, Translations } from '@i18n-micro/types'; | ||
| import { FormatService } from './format-service'; | ||
| import { Translations, Params, PluralFunc, CleanTranslation, TranslationKey, MissingHandler } from '@i18n-micro/types'; | ||
| import { TranslationStorage, useTranslationHelper } from './translation'; | ||
| export interface BaseI18nOptions { | ||
@@ -5,0 +5,0 @@ storage?: TranslationStorage; |
@@ -1,2 +0,2 @@ | ||
| import { Params, Strategies, PluralFunc } from '@i18n-micro/types'; | ||
| import { Params, PluralFunc, Strategies } from '@i18n-micro/types'; | ||
| export declare function interpolate(template: string, params: Params): string; | ||
@@ -3,0 +3,0 @@ export declare function withPrefixStrategy(strategy: Strategies): strategy is "prefix" | "prefix_and_default"; |
+1
-1
@@ -1,1 +0,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; | ||
| "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; |
+3
-3
@@ -1,5 +0,5 @@ | ||
| import { useTranslationHelper, TranslationStorage } from './translation'; | ||
| import { BaseI18n, BaseI18nOptions } from './base'; | ||
| import { FormatService } from './format-service'; | ||
| import { interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural } from './helpers'; | ||
| import { BaseI18n, BaseI18nOptions } from './base'; | ||
| import { defaultPlural, interpolate, isNoPrefixStrategy, isPrefixAndDefaultStrategy, isPrefixExceptDefaultStrategy, isPrefixStrategy, withPrefixStrategy } from './helpers'; | ||
| import { TranslationStorage, useTranslationHelper } from './translation'; | ||
| export { useTranslationHelper, interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural, FormatService, BaseI18n, type TranslationStorage, type BaseI18nOptions, }; |
+66
-72
@@ -0,1 +1,60 @@ | ||
| class g { | ||
| formatNumber(e, t, n) { | ||
| return new Intl.NumberFormat(t, n).format(e); | ||
| } | ||
| formatDate(e, t, n) { | ||
| const r = new Date(e); | ||
| return Number.isNaN(r.getTime()) ? "Invalid Date" : new Intl.DateTimeFormat(t, n).format(r); | ||
| } | ||
| formatRelativeTime(e, t, n) { | ||
| const r = new Date(e); | ||
| if (Number.isNaN(r.getTime())) | ||
| return new Intl.RelativeTimeFormat(t, n).format(0, "second"); | ||
| const i = Math.floor(((/* @__PURE__ */ new Date()).getTime() - r.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, n).format(-c, l); | ||
| } | ||
| return new Intl.RelativeTimeFormat(t, n).format(0, "second"); | ||
| } | ||
| } | ||
| const h = /\{(\w+)\}/g; | ||
| function m(s, e) { | ||
| return e ? s.replace(h, (t, n) => { | ||
| const r = e[n]; | ||
| return r !== void 0 ? String(r) : `{${n}}`; | ||
| }) : 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 N(s) { | ||
| return s === "prefix_and_default"; | ||
| } | ||
| const d = (s, e, t, n, r) => { | ||
| const a = r(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) { | ||
@@ -16,3 +75,3 @@ if (!s || typeof e != "string") return null; | ||
| } | ||
| function h(s) { | ||
| function p(s) { | ||
| const e = s?.translations ?? /* @__PURE__ */ new Map(); | ||
@@ -72,64 +131,5 @@ return { | ||
| } | ||
| class g { | ||
| formatNumber(e, t, n) { | ||
| return new Intl.NumberFormat(t, n).format(e); | ||
| } | ||
| formatDate(e, t, n) { | ||
| const r = new Date(e); | ||
| return Number.isNaN(r.getTime()) ? "Invalid Date" : new Intl.DateTimeFormat(t, n).format(r); | ||
| } | ||
| formatRelativeTime(e, t, n) { | ||
| const r = new Date(e); | ||
| if (Number.isNaN(r.getTime())) | ||
| return new Intl.RelativeTimeFormat(t, n).format(0, "second"); | ||
| const i = Math.floor(((/* @__PURE__ */ new Date()).getTime() - r.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, n).format(-c, l); | ||
| } | ||
| return new Intl.RelativeTimeFormat(t, n).format(0, "second"); | ||
| } | ||
| } | ||
| const m = /\{(\w+)\}/g; | ||
| function d(s, e) { | ||
| return e ? s.replace(m, (t, n) => { | ||
| const r = e[n]; | ||
| return r !== void 0 ? String(r) : `{${n}}`; | ||
| }) : 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 N(s) { | ||
| return s === "prefix_and_default"; | ||
| } | ||
| const p = (s, e, t, n, r) => { | ||
| const a = r(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 $ { | ||
| constructor(e = {}) { | ||
| 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; | ||
| 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; | ||
| } | ||
@@ -160,3 +160,3 @@ // --- Public methods (implemented in base class) --- | ||
| } | ||
| return typeof o == "string" && t ? d(o, t) : o; | ||
| return typeof o == "string" && t ? m(o, t) : o; | ||
| } | ||
@@ -177,9 +177,3 @@ /** | ||
| const i = (l, u, c) => this.t(l, u, c); | ||
| return this.pluralFunc( | ||
| e, | ||
| Number.parseInt(r.toString()), | ||
| a, | ||
| this.getLocale(), | ||
| i | ||
| ) ?? n ?? e; | ||
| return this.pluralFunc(e, Number.parseInt(r.toString(), 10), a, this.getLocale(), i) ?? n ?? e; | ||
| } | ||
@@ -237,4 +231,4 @@ /** | ||
| g as FormatService, | ||
| p as defaultPlural, | ||
| d as interpolate, | ||
| d as defaultPlural, | ||
| m as interpolate, | ||
| T as isNoPrefixStrategy, | ||
@@ -244,4 +238,4 @@ N as isPrefixAndDefaultStrategy, | ||
| P as isPrefixStrategy, | ||
| h as useTranslationHelper, | ||
| p as useTranslationHelper, | ||
| v as withPrefixStrategy | ||
| }; |
+2
-2
| { | ||
| "name": "@i18n-micro/core", | ||
| "version": "1.1.1", | ||
| "version": "1.1.2", | ||
| "description": "", | ||
@@ -25,3 +25,3 @@ "repository": "s00d/nuxt-i18n-micro", | ||
| "dependencies": { | ||
| "@i18n-micro/types": "1.1.0" | ||
| "@i18n-micro/types": "1.1.1" | ||
| }, | ||
@@ -28,0 +28,0 @@ "devDependencies": { |
+15
-48
@@ -1,13 +0,5 @@ | ||
| import { useTranslationHelper, type TranslationStorage } from './translation' | ||
| import type { CleanTranslation, Getter, MissingHandler, Params, PluralFunc, TranslationKey, Translations } from '@i18n-micro/types' | ||
| import { FormatService } from './format-service' | ||
| import { interpolate, defaultPlural } from './helpers' | ||
| import type { | ||
| Translations, | ||
| Params, | ||
| PluralFunc, | ||
| Getter, | ||
| CleanTranslation, | ||
| TranslationKey, | ||
| MissingHandler, | ||
| } from '@i18n-micro/types' | ||
| import { defaultPlural, interpolate } from './helpers' | ||
| import { type TranslationStorage, useTranslationHelper } from './translation' | ||
@@ -20,3 +12,3 @@ export interface BaseI18nOptions { | ||
| // Optional hooks for Nuxt runtime specific features | ||
| getPreviousPageInfo?: () => { locale: string, routeName: string } | null | ||
| getPreviousPageInfo?: () => { locale: string; routeName: string } | null | ||
| getCustomMissingHandler?: () => MissingHandler | null | ||
@@ -40,3 +32,3 @@ enablePreviousPageFallback?: boolean | ||
| // Optional hooks for Nuxt runtime specific features | ||
| public getPreviousPageInfo?: () => { locale: string, routeName: string } | null | ||
| public getPreviousPageInfo?: () => { locale: string; routeName: string } | null | ||
| public getCustomMissingHandler?: () => MissingHandler | null | ||
@@ -79,8 +71,3 @@ public enablePreviousPageFallback: boolean | ||
| */ | ||
| public t( | ||
| key: TranslationKey, | ||
| params?: Params, | ||
| defaultValue?: string | null, | ||
| routeName?: string, | ||
| ): CleanTranslation { | ||
| public t(key: TranslationKey, params?: Params, defaultValue?: string | null, routeName?: string): CleanTranslation { | ||
| if (!key) return '' | ||
@@ -124,7 +111,5 @@ | ||
| customHandler(locale, key, route) | ||
| } | ||
| else if (this.missingHandler) { | ||
| } else if (this.missingHandler) { | ||
| this.missingHandler(locale, key as string, route) | ||
| } | ||
| else if (this.missingWarn) { | ||
| } else if (this.missingWarn) { | ||
| const isDev = process.env.NODE_ENV !== 'production' | ||
@@ -136,7 +121,7 @@ const isClient = typeof window !== 'undefined' | ||
| } | ||
| value = defaultValue === undefined ? key : (defaultValue || key) | ||
| value = defaultValue === undefined ? key : defaultValue || key | ||
| } | ||
| // 5. Interpolate | ||
| return typeof value === 'string' && params ? interpolate(value, params) : value as CleanTranslation | ||
| return typeof value === 'string' && params ? interpolate(value, params) : (value as CleanTranslation) | ||
| } | ||
@@ -147,8 +132,3 @@ | ||
| */ | ||
| public ts( | ||
| key: TranslationKey, | ||
| params?: Params, | ||
| defaultValue?: string, | ||
| routeName?: string, | ||
| ): string { | ||
| public ts(key: TranslationKey, params?: Params, defaultValue?: string, routeName?: string): string { | ||
| const value = this.t(key, params, defaultValue, routeName) | ||
@@ -173,9 +153,3 @@ return value?.toString() ?? defaultValue ?? key | ||
| const result = this.pluralFunc( | ||
| key, | ||
| Number.parseInt(countValue.toString()), | ||
| params, | ||
| this.getLocale(), | ||
| getter, | ||
| ) | ||
| const result = this.pluralFunc(key, Number.parseInt(countValue.toString(), 10), params, this.getLocale(), getter) | ||
@@ -234,4 +208,3 @@ return result ?? defaultValue ?? key | ||
| this.helper.mergeGlobalTranslation(locale, translations, true) | ||
| } | ||
| else { | ||
| } else { | ||
| this.helper.setTranslations(locale, translations) | ||
@@ -245,12 +218,6 @@ } | ||
| */ | ||
| public loadRouteTranslationsCore( | ||
| locale: string, | ||
| routeName: string, | ||
| translations: Translations, | ||
| merge: boolean, | ||
| ): void { | ||
| public loadRouteTranslationsCore(locale: string, routeName: string, translations: Translations, merge: boolean): void { | ||
| if (merge) { | ||
| this.helper.mergeTranslation(locale, routeName, translations, true) | ||
| } | ||
| else { | ||
| } else { | ||
| this.helper.loadPageTranslations(locale, routeName, translations) | ||
@@ -257,0 +224,0 @@ } |
@@ -23,3 +23,3 @@ export class FormatService { | ||
| const units: { unit: Intl.RelativeTimeFormatUnit, seconds: number }[] = [ | ||
| const units: { unit: Intl.RelativeTimeFormatUnit; seconds: number }[] = [ | ||
| { unit: 'year', seconds: 31536000 }, | ||
@@ -26,0 +26,0 @@ { unit: 'month', seconds: 2592000 }, |
+1
-1
@@ -1,2 +0,2 @@ | ||
| import type { Params, Strategies, PluralFunc, Getter, TranslationKey } from '@i18n-micro/types' | ||
| import type { Getter, Params, PluralFunc, Strategies, TranslationKey } from '@i18n-micro/types' | ||
@@ -3,0 +3,0 @@ const RE_TOKEN = /\{(\w+)\}/g |
+11
-3
@@ -1,5 +0,13 @@ | ||
| import { useTranslationHelper, type TranslationStorage } from './translation' | ||
| import { BaseI18n, type BaseI18nOptions } from './base' | ||
| import { FormatService } from './format-service' | ||
| import { interpolate, withPrefixStrategy, isNoPrefixStrategy, isPrefixStrategy, isPrefixExceptDefaultStrategy, isPrefixAndDefaultStrategy, defaultPlural } from './helpers' | ||
| import { BaseI18n, type BaseI18nOptions } from './base' | ||
| import { | ||
| defaultPlural, | ||
| interpolate, | ||
| isNoPrefixStrategy, | ||
| isPrefixAndDefaultStrategy, | ||
| isPrefixExceptDefaultStrategy, | ||
| isPrefixStrategy, | ||
| withPrefixStrategy, | ||
| } from './helpers' | ||
| import { type TranslationStorage, useTranslationHelper } from './translation' | ||
@@ -6,0 +14,0 @@ export { |
@@ -27,4 +27,3 @@ import type { Translations } from '@i18n-micro/types' | ||
| value = (value as Translations)[part] | ||
| } | ||
| else { | ||
| } else { | ||
| return null | ||
@@ -89,4 +88,3 @@ } | ||
| translations.set(key, data) | ||
| } | ||
| else { | ||
| } else { | ||
| translations.set(key, { ...existing, ...data }) | ||
@@ -93,0 +91,0 @@ } |
+6
-11
@@ -0,3 +1,3 @@ | ||
| import type { PluralFunc, Translations } from '@i18n-micro/types' | ||
| import { BaseI18n, type BaseI18nOptions } from '../src/base' | ||
| import type { Translations, PluralFunc } from '@i18n-micro/types' | ||
@@ -10,8 +10,3 @@ // Test implementation of BaseI18n | ||
| constructor( | ||
| locale: string, | ||
| fallbackLocale: string, | ||
| route: string, | ||
| options?: BaseI18nOptions, | ||
| ) { | ||
| constructor(locale: string, fallbackLocale: string, route: string, options?: BaseI18nOptions) { | ||
| super(options) | ||
@@ -379,3 +374,3 @@ this._locale = locale | ||
| // Wait for async operation to complete | ||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
| await new Promise((resolve) => setTimeout(resolve, 0)) | ||
@@ -393,3 +388,3 @@ expect(i18n.has('greeting')).toBe(true) | ||
| // Wait for async operation to complete | ||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
| await new Promise((resolve) => setTimeout(resolve, 0)) | ||
@@ -408,3 +403,3 @@ expect(i18n.has('greeting')).toBe(true) | ||
| // Wait for async operation to complete | ||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
| await new Promise((resolve) => setTimeout(resolve, 0)) | ||
@@ -422,3 +417,3 @@ expect(i18n.has('title', 'about')).toBe(true) | ||
| // Wait for async operation to complete | ||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
| await new Promise((resolve) => setTimeout(resolve, 0)) | ||
@@ -425,0 +420,0 @@ expect(i18n.has('title', 'about')).toBe(true) |
@@ -1,2 +0,2 @@ | ||
| import { useTranslationHelper, interpolate } from '../src' | ||
| import { interpolate, useTranslationHelper } from '../src' | ||
@@ -3,0 +3,0 @@ describe('Translation Helper', () => { |
| import { | ||
| interpolate, | ||
| withPrefixStrategy, | ||
| isNoPrefixStrategy, | ||
| isPrefixAndDefaultStrategy, | ||
| isPrefixExceptDefaultStrategy, | ||
| isPrefixStrategy, | ||
| isPrefixExceptDefaultStrategy, | ||
| isPrefixAndDefaultStrategy, | ||
| withPrefixStrategy, | ||
| } from '../src/helpers' | ||
@@ -9,0 +9,0 @@ |
+2
-5
@@ -1,2 +0,1 @@ | ||
| // eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
| // @ts-nocheck | ||
@@ -13,3 +12,3 @@ import { resolve } from 'node:path' | ||
| formats: ['cjs', 'es'], | ||
| fileName: format => `index.${format === 'cjs' ? 'cjs' : 'mjs'}`, | ||
| fileName: (format) => `index.${format === 'cjs' ? 'cjs' : 'mjs'}`, | ||
| }, | ||
@@ -23,5 +22,3 @@ rollupOptions: { | ||
| }, | ||
| plugins: [ | ||
| dts(), | ||
| ], | ||
| plugins: [dts()], | ||
| }) |
72946
-0.32%1444
-2.56%+ Added
- Removed
Updated