@mmstack/translate 20.5.4 → 20.5.5

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/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as i0 from '@angular/core';
2
- import { WritableSignal, Signal, Provider } from '@angular/core';
2
+ import { Signal, WritableSignal, Provider } from '@angular/core';
3
+ import { ResolveFn, ActivatedRouteSnapshot, CanMatchFn } from '@angular/router';
3
4
  import * as _formatjs_intl from '@formatjs/intl';
4
5
  import { IntlConfig } from '@formatjs/intl';
5
6
 
@@ -64,7 +65,226 @@ type TranslationNamespace<TNS extends string, T extends CompiledTranslation<Unkn
64
65
  };
65
66
  declare function createNamespace<const T extends UnknownStringKeyObject, TNS extends string>(ns: TNS, translation: T): TranslationNamespace<TNS, CompiledTranslation<T, TNS, string>, inferTranslationShape<T>>;
66
67
 
67
- declare function provideIntlConfig(config: Omit<IntlConfig, 'locale' | 'messages'>): Provider;
68
+ declare const FORMAT_PRESETS: Record<string, Intl.DateTimeFormatOptions>;
69
+ type DateFormat = keyof typeof FORMAT_PRESETS;
70
+ /**
71
+ * Supported date inputs
72
+ */
73
+ type SupportedDateInput = Date | string | number | null | undefined;
74
+ /**
75
+ * Options for formatting a date
76
+ */
77
+ type FormatDateOptions = {
78
+ /**
79
+ * Timezone to use for formatting
80
+ */
81
+ tz?: string;
82
+ /**
83
+ * Format to use for formatting
84
+ * @default 'medium'
85
+ */
86
+ format?: DateFormat;
87
+ /**
88
+ * Locale to use for formatting, opts out to dynamic locale changes
89
+ */
90
+ locale?: string;
91
+ };
92
+ /**
93
+ * Format a date using the current or provided locale & timezone
94
+ * By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
95
+ *
96
+ * @param date - Date to format
97
+ * @param opt - Options for formatting
98
+ * @returns Formatted date string
99
+ */
100
+ declare function formatDate(date: SupportedDateInput | Signal<SupportedDateInput>, opt?: FormatDateOptions | Signal<FormatDateOptions>): string;
101
+
102
+ /**
103
+ * Options for formatting a display name
104
+ */
105
+ type FormatDisplayNameOptions = {
106
+ /**
107
+ * The display style for the result set
108
+ */
109
+ style: Intl.RelativeTimeFormatStyle;
110
+ /**
111
+ * Locale to use for formatting, opts out to dynamic locale changes
112
+ */
113
+ locale?: string;
114
+ };
115
+ type SupportedCode = string | null | undefined;
116
+ /**
117
+ * Format a display name using the current or provided locale
118
+ * By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
119
+ *
120
+ * @param value - The code to format
121
+ * @param type - The type of display name to format
122
+ * @param opt - Options for formatting
123
+ * @returns Formatted display name string
124
+ */
125
+ declare function formatDisplayName(value: SupportedCode | Signal<SupportedCode>, type: Intl.DisplayNamesType | Signal<Intl.DisplayNamesType>, opt?: FormatDisplayNameOptions | Signal<FormatDisplayNameOptions>): string;
126
+
127
+ type ListType = 'conjunction' | 'disjunction' | 'unit';
128
+ type ListStyle = 'long' | 'short' | 'narrow';
129
+ type SupportedListInput = string[] | null | undefined;
130
+ /**
131
+ * Options for formatting a list
132
+ */
133
+ type FormatListOptions = {
134
+ /**
135
+ * The type of list to format
136
+ */
137
+ type?: ListType;
138
+ /**
139
+ * The style of list to format
140
+ */
141
+ style?: ListStyle;
142
+ /**
143
+ * Locale to use for formatting, opts out to dynamic locale changes
144
+ */
145
+ locale?: string;
146
+ };
147
+ /**
148
+ * Format a list using the current or provided locale
149
+ * By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
150
+ *
151
+ * @param value - The list to format
152
+ * @param opt - Options for formatting
153
+ * @returns Formatted list string
154
+ */
155
+ declare function formatList(value: SupportedListInput | Signal<SupportedListInput>, opt?: FormatListOptions | Signal<FormatListOptions>): string;
156
+
157
+ type NumberNotation = 'standard' | 'scientific' | 'engineering' | 'compact';
158
+ type SupportedNumberValue = number | null | undefined;
159
+ /**
160
+ * Options for formatting a number
161
+ */
162
+ type FormatNumberOptions = {
163
+ /**
164
+ * The notation to use for formatting
165
+ */
166
+ notation?: NumberNotation;
167
+ /**
168
+ * Minimum number of fraction digits to use
169
+ */
170
+ minFractionDigits?: number;
171
+ /**
172
+ * Maximum number of fraction digits to use
173
+ */
174
+ maxFractionDigits?: number;
175
+ /**
176
+ * Whether to use grouping
177
+ */
178
+ useGrouping?: boolean;
179
+ /**
180
+ * Locale to use for formatting, opts out to dynamic locale changes
181
+ */
182
+ locale?: string;
183
+ /**
184
+ * If the number is not a valid number, return formatted 0. By default formatter returns an empty string
185
+ * @default false
186
+ */
187
+ fallbackToZero?: boolean;
188
+ };
189
+ /**
190
+ * Format a number using the current or provided locale
191
+ * By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
192
+ *
193
+ * @param number - Number to format
194
+ * @param opt - Options for formatting
195
+ * @returns Formatted number string
196
+ */
197
+ declare function formatNumber(value: SupportedNumberValue | Signal<SupportedNumberValue>, opt?: FormatNumberOptions | Signal<FormatNumberOptions>): string;
198
+ /**
199
+ * Options for formatting a percentage value
200
+ */
201
+ type FormatPercentOptions = {
202
+ /**
203
+ * Minimum number of fraction digits to use
204
+ */
205
+ minFractionDigits?: number;
206
+ /**
207
+ * Maximum number of fraction digits to use
208
+ */
209
+ maxFractionDigits?: number;
210
+ /**
211
+ * Locale to use for formatting, opts out to dynamic locale changes
212
+ */
213
+ locale?: string;
214
+ /**
215
+ * If the number is not a valid number, return formatted 0. By default formatter returns an empty string
216
+ * @default false
217
+ */
218
+ fallbackToZero?: boolean;
219
+ };
220
+ /**
221
+ * Format a percentage using the current or provided locale
222
+ * By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
223
+ *
224
+ * @param number - Number to format
225
+ * @param opt - Options for formatting
226
+ * @returns Formatted percentage string
227
+ */
228
+ declare function formatPercent(value: SupportedNumberValue | Signal<SupportedNumberValue>, opt?: FormatPercentOptions | Signal<FormatPercentOptions>): string;
229
+ type CurrencyDisplay = 'symbol' | 'narrowSymbol' | 'code' | 'name';
230
+ /**
231
+ * Options for formatting a currency
232
+ */
233
+ type FormatCurrencyOptions = {
234
+ display?: CurrencyDisplay;
235
+ /**
236
+ * Locale to use for formatting, opts out to dynamic locale changes
237
+ */
238
+ locale?: string;
239
+ /**
240
+ * If the number is not a valid number, return formatted 0. By default formatter returns an empty string
241
+ * @default false
242
+ */
243
+ fallbackToZero?: boolean;
244
+ };
245
+ declare function formatCurrency(value: SupportedNumberValue | Signal<SupportedNumberValue>, currency: string | Signal<string>, opt?: FormatCurrencyOptions | Signal<FormatCurrencyOptions>): string;
246
+
247
+ /**
248
+ * Options for formatting a relative time value
249
+ */
250
+ type FormatRelativeTimeOptions = {
251
+ /**
252
+ * The length of the internationalized message.
253
+ * @default 'long'
254
+ */
255
+ style?: Intl.RelativeTimeFormatStyle;
256
+ /**
257
+ * Controls whether to use numeric values in the output.
258
+ * @default 'always'
259
+ */
260
+ numeric?: Intl.RelativeTimeFormatNumeric;
261
+ /**
262
+ * Locale to use for formatting, opts out to dynamic locale changes
263
+ */
264
+ locale?: string;
265
+ };
266
+ type RelativeTimeUnit = Intl.RelativeTimeFormatUnit;
267
+ type SupportedRelativeTimeInput = number | null | undefined;
268
+ /**
269
+ * Format a relative time using the current or provided locale
270
+ * By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
271
+ *
272
+ * @param value - The numeric value to use in the relative time internationalization message
273
+ * @param unit - The unit to use in the relative time internationalization message
274
+ * @param opt - Options for formatting
275
+ * @returns Formatted relative time string
276
+ */
277
+ declare function formatRelativeTime(value: SupportedRelativeTimeInput | Signal<SupportedRelativeTimeInput>, unit: RelativeTimeUnit | Signal<RelativeTimeUnit>, opt?: FormatRelativeTimeOptions | Signal<FormatRelativeTimeOptions>): string;
278
+
279
+ declare function provideIntlConfig(config: Omit<IntlConfig, 'locale' | 'messages'> & {
280
+ /** Checks next locale is in provided array before switching locales */
281
+ supportedLocales?: string[];
282
+ /** Preloads the default locale ensuring sync fallback, not necessary for most cases as it will lazily load automatically when needed */
283
+ preloadDefaultLocale?: boolean;
284
+ /** Auto-resolution when using a locale parameter via angular router */
285
+ localeParamName?: string;
286
+ }): Provider[];
287
+ declare function injectSupportedLocales(): string[];
68
288
  declare function injectIntl(): Signal<_formatjs_intl.IntlShape<string>>;
69
289
  /**
70
290
  * Inject a dynamic locale signal that supports runtime language switching.
@@ -97,9 +317,41 @@ type TFunctionWithSignalConstructor<TMap extends AnyStringRecord, TFN extends TF
97
317
  };
98
318
  declare function registerNamespace<TDefault extends CompiledTranslation<UnknownStringKeyObject, string>>(defaultTranslation: () => Promise<TDefault>, other: Record<string, () => Promise<CompiledTranslation<UnknownStringKeyObject, inferCompiledTranslationNamespace<TDefault>, string>>>): {
99
319
  injectNamespaceT: () => TFunctionWithSignalConstructor<inferCompiledTranslationMap<TDefault>, TFunction<inferCompiledTranslationMap<TDefault>>>;
100
- resolveNamespaceTranslation: () => Promise<void>;
320
+ resolveNamespaceTranslation: ResolveFn<void>;
321
+ };
322
+ type UntypedTFunction<TNS extends string> = {
323
+ (key: `${TNS}.${string}`, args?: Record<string, string>): string;
324
+ asSignal: (key: `${TNS}.${string}`, args?: () => Record<string, string>) => Signal<string>;
325
+ };
326
+ /**
327
+ * Registers a type-unsafe namespace, meant for remote loading of unknown key-value pairs using mmstack/translate infrastructure
328
+ * The resolver & t function work the same as they would with typed namespaces, but without type safety
329
+ */
330
+ declare function registerRemoteNamespace<TNS extends string>(ns: TNS, defaultTranslation: () => Promise<Record<string, string>>, other: Record<string, () => Promise<Record<string, string>>>): {
331
+ injectNamespaceT: () => UntypedTFunction<TNS>;
332
+ resolveNamespaceTranslation: ResolveFn<void>;
101
333
  };
102
334
 
335
+ declare function injectResolveParamLocale(snapshot: ActivatedRouteSnapshot): string;
336
+
337
+ /**
338
+ * Guard that validates the locale parameter against supported locales.
339
+ * Redirects to default locale if the locale is invalid.
340
+ *
341
+ * @param prefixSegments Optional array of path segments preceding the locale segment.
342
+ * if (you wanted to match /app/:locale/... you would pass ['app'] here) & the function would match the second parameter + redirect accordingly
343
+ *
344
+ * @example
345
+ * ```typescript
346
+ * {
347
+ * path: ':locale',
348
+ * canMatch: [canMatchLocale()],
349
+ * children: [...]
350
+ * }
351
+ * ```
352
+ */
353
+ declare function canMatchLocale(prefixSegments?: string[]): CanMatchFn;
354
+
103
355
  declare abstract class Translate<TInput extends string, T extends CompiledTranslation<UnknownStringKeyObject, string>, TMap extends inferCompiledTranslationMap<T> = inferCompiledTranslationMap<T>, TKey extends TInput & keyof TMap & string = TInput & keyof TMap & string> {
104
356
  private readonly t;
105
357
  readonly translate: i0.InputSignal<TMap[TKey] extends void ? TKey | [key: TKey] : [key: TKey, vars: TMap[TKey]]>;
@@ -115,5 +367,5 @@ declare abstract class Translator<T extends CompiledTranslation<UnknownStringKey
115
367
  transform<K extends keyof TMap & string>(key: K, ...args: TMap[K] extends void ? [locale?: string] : [TMap[K], locale?: string]): string;
116
368
  }
117
369
 
118
- export { Translate, Translator, compileTranslation, createNamespace, injectDynamicLocale, injectIntl, provideIntlConfig, registerNamespace };
119
- export type { CompiledTranslation, inferCompiledTranslationMap, inferCompiledTranslationNamespace, inferCompiledTranslationShape, mergeTranslationMaps };
370
+ export { Translate, Translator, canMatchLocale, compileTranslation, createNamespace, formatCurrency, formatDate, formatDisplayName, formatList, formatNumber, formatPercent, formatRelativeTime, injectDynamicLocale, injectIntl, injectResolveParamLocale, injectSupportedLocales, provideIntlConfig, registerNamespace, registerRemoteNamespace };
371
+ export type { CompiledTranslation, FormatCurrencyOptions, FormatDateOptions, FormatDisplayNameOptions, FormatListOptions, FormatNumberOptions, FormatPercentOptions, FormatRelativeTimeOptions, RelativeTimeUnit, SupportedDateInput, SupportedListInput, inferCompiledTranslationMap, inferCompiledTranslationNamespace, inferCompiledTranslationShape, mergeTranslationMaps };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/translate",
3
- "version": "20.5.4",
3
+ "version": "20.5.5",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "localize",
@@ -15,7 +15,8 @@
15
15
  },
16
16
  "peerDependencies": {
17
17
  "@angular/core": ">=20 <21",
18
- "@formatjs/intl": "~3.1.6"
18
+ "@angular/router": ">=20 <21",
19
+ "@formatjs/intl": "~3.1.8 || ^4.0.0"
19
20
  },
20
21
  "homepage": "https://github.com/mihajm/mmstack/blob/master/packages/translate",
21
22
  "sideEffects": false,