-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Expand file tree
/
Copy pathlocale_data_api.ts
More file actions
804 lines (763 loc) · 26.4 KB
/
locale_data_api.ts
File metadata and controls
804 lines (763 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
ɵCurrencyIndex,
ɵExtraLocaleDataIndex,
ɵfindLocaleData,
ɵgetLocaleCurrencyCode,
ɵgetLocalePluralCase,
ɵLocaleDataIndex,
ɵRuntimeError as RuntimeError,
} from '@angular/core';
import {CURRENCIES_EN, CurrenciesSymbols} from './currencies';
import {RuntimeErrorCode} from '../errors';
/**
* Format styles that can be used to represent numbers.
* @see {@link getLocaleNumberFormat}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated `getLocaleNumberFormat` is deprecated
*/
export enum NumberFormatStyle {
Decimal,
Percent,
Currency,
Scientific,
}
/**
* Plurality cases used for translating plurals to different languages.
*
* @see {@link NgPlural}
* @see {@link NgPluralCase}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated `getLocalePluralCase` is deprecated
*/
export enum Plural {
Zero = 0,
One = 1,
Two = 2,
Few = 3,
Many = 4,
Other = 5,
}
/**
* Context-dependant translation forms for strings.
* Typically the standalone version is for the nominative form of the word,
* and the format version is used for the genitive case.
* @see [CLDR website](http://cldr.unicode.org/translation/date-time-1/date-time#TOC-Standalone-vs.-Format-Styles)
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated locale data getters are deprecated
*/
export enum FormStyle {
Format,
Standalone,
}
/**
* String widths available for translations.
* The specific character widths are locale-specific.
* Examples are given for the word "Sunday" in English.
*
* @publicApi
*
* @deprecated locale data getters are deprecated
*/
export enum TranslationWidth {
/** 1 character for `en-US`. For example: 'S' */
Narrow,
/** 3 characters for `en-US`. For example: 'Sun' */
Abbreviated,
/** Full length for `en-US`. For example: "Sunday" */
Wide,
/** 2 characters for `en-US`, For example: "Su" */
Short,
}
/**
* String widths available for date-time formats.
* The specific character widths are locale-specific.
* Examples are given for `en-US`.
*
* @see {@link getLocaleDateFormat}
* @see {@link getLocaleTimeFormat}
* @see {@link getLocaleDateTimeFormat}
* @see [Internationalization (i18n) Guide](guide/i18n)
* @publicApi
*
* @deprecated 18.0
* Date locale data getters are deprecated
*/
export enum FormatWidth {
/**
* For `en-US`, `'M/d/yy, h:mm a'`
* (Example: `6/15/15, 9:03 AM`)
*/
Short,
/**
* For `en-US`, `'MMM d, y, h:mm:ss a'`
* (Example: `Jun 15, 2015, 9:03:01 AM`)
*/
Medium,
/**
* For `en-US`, `'MMMM d, y, h:mm:ss a z'`
* (Example: `June 15, 2015 at 9:03:01 AM GMT+1`)
*/
Long,
/**
* For `en-US`, `'EEEE, MMMM d, y, h:mm:ss a zzzz'`
* (Example: `Monday, June 15, 2015 at 9:03:01 AM GMT+01:00`)
*/
Full,
}
// This needs to be an object literal, rather than an enum, because TypeScript 5.4+
// doesn't allow numeric keys and we have `Infinity` and `NaN`.
/**
* Symbols that can be used to replace placeholders in number patterns.
* Examples are based on `en-US` values.
*
* @see {@link getLocaleNumberSymbol}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated `getLocaleNumberSymbol` is deprecated
*
* @object-literal-as-enum
*/
export const NumberSymbol = {
/**
* Decimal separator.
* For `en-US`, the dot character.
* Example: 2,345`.`67
*/
Decimal: 0,
/**
* Grouping separator, typically for thousands.
* For `en-US`, the comma character.
* Example: 2`,`345.67
*/
Group: 1,
/**
* List-item separator.
* Example: "one, two, and three"
*/
List: 2,
/**
* Sign for percentage (out of 100).
* Example: 23.4%
*/
PercentSign: 3,
/**
* Sign for positive numbers.
* Example: +23
*/
PlusSign: 4,
/**
* Sign for negative numbers.
* Example: -23
*/
MinusSign: 5,
/**
* Computer notation for exponential value (n times a power of 10).
* Example: 1.2E3
*/
Exponential: 6,
/**
* Human-readable format of exponential.
* Example: 1.2x103
*/
SuperscriptingExponent: 7,
/**
* Sign for permille (out of 1000).
* Example: 23.4‰
*/
PerMille: 8,
/**
* Infinity, can be used with plus and minus.
* Example: ∞, +∞, -∞
*/
Infinity: 9,
/**
* Not a number.
* Example: NaN
*/
NaN: 10,
/**
* Symbol used between time units.
* Example: 10:52
*/
TimeSeparator: 11,
/**
* Decimal separator for currency values (fallback to `Decimal`).
* Example: $2,345.67
*/
CurrencyDecimal: 12,
/**
* Group separator for currency values (fallback to `Group`).
* Example: $2,345.67
*/
CurrencyGroup: 13,
} as const;
export type NumberSymbol = (typeof NumberSymbol)[keyof typeof NumberSymbol];
/**
* The value for each day of the week, based on the `en-US` locale
*
* @publicApi
*
* @deprecated Week locale getters are deprecated
*/
export enum WeekDay {
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
}
/**
* Retrieves the locale ID from the currently loaded locale.
* The loaded locale could be, for example, a global one rather than a regional one.
* @param locale A locale code, such as `fr-FR`.
* @returns The locale code. For example, `fr`.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* This function serves no purpose when relying on the `Intl` API.
*/
export function getLocaleId(locale: string): string {
return ɵfindLocaleData(locale)[ɵLocaleDataIndex.LocaleId];
}
/**
* Retrieves day period strings for the given locale.
*
* @param locale A locale code for the locale format rules to use.
* @param formStyle The required grammatical form.
* @param width The required character width.
* @returns An array of localized period strings. For example, `[AM, PM]` for `en-US`.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.DateTimeFormat` for date formating instead.
*/
export function getLocaleDayPeriods(
locale: string,
formStyle: FormStyle,
width: TranslationWidth,
): Readonly<[string, string]> {
const data = ɵfindLocaleData(locale);
const amPmData = <[string, string][][]>[
data[ɵLocaleDataIndex.DayPeriodsFormat],
data[ɵLocaleDataIndex.DayPeriodsStandalone],
];
const amPm = getLastDefinedValue(amPmData, formStyle);
return getLastDefinedValue(amPm, width);
}
/**
* Retrieves days of the week for the given locale, using the Gregorian calendar.
*
* @param locale A locale code for the locale format rules to use.
* @param formStyle The required grammatical form.
* @param width The required character width.
* @returns An array of localized name strings.
* For example,`[Sunday, Monday, ... Saturday]` for `en-US`.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.DateTimeFormat` for date formating instead.
*/
export function getLocaleDayNames(
locale: string,
formStyle: FormStyle,
width: TranslationWidth,
): ReadonlyArray<string> {
const data = ɵfindLocaleData(locale);
const daysData = <string[][][]>[
data[ɵLocaleDataIndex.DaysFormat],
data[ɵLocaleDataIndex.DaysStandalone],
];
const days = getLastDefinedValue(daysData, formStyle);
return getLastDefinedValue(days, width);
}
/**
* Retrieves months of the year for the given locale, using the Gregorian calendar.
*
* @param locale A locale code for the locale format rules to use.
* @param formStyle The required grammatical form.
* @param width The required character width.
* @returns An array of localized name strings.
* For example, `[January, February, ...]` for `en-US`.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.DateTimeFormat` for date formating instead.
*/
export function getLocaleMonthNames(
locale: string,
formStyle: FormStyle,
width: TranslationWidth,
): ReadonlyArray<string> {
const data = ɵfindLocaleData(locale);
const monthsData = <string[][][]>[
data[ɵLocaleDataIndex.MonthsFormat],
data[ɵLocaleDataIndex.MonthsStandalone],
];
const months = getLastDefinedValue(monthsData, formStyle);
return getLastDefinedValue(months, width);
}
/**
* Retrieves Gregorian-calendar eras for the given locale.
* @param locale A locale code for the locale format rules to use.
* @param width The required character width.
* @returns An array of localized era strings.
* For example, `[AD, BC]` for `en-US`.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.DateTimeFormat` for date formating instead.
*/
export function getLocaleEraNames(
locale: string,
width: TranslationWidth,
): Readonly<[string, string]> {
const data = ɵfindLocaleData(locale);
const erasData = <[string, string][]>data[ɵLocaleDataIndex.Eras];
return getLastDefinedValue(erasData, width);
}
/**
* Retrieves the first day of the week for the given locale.
*
* @param locale A locale code for the locale format rules to use.
* @returns A day index number, using the 0-based week-day index for `en-US`
* (Sunday = 0, Monday = 1, ...).
* For example, for `fr-FR`, returns 1 to indicate that the first day is Monday.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Intl's [`getWeekInfo`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getWeekInfo) has partial support (Chromium M99 & Safari 17).
* You may want to rely on the following alternatives:
* - Libraries like [`Luxon`](https://moment.github.io/luxon/#/) rely on `Intl` but fallback on the ISO 8601 definition (monday) if `getWeekInfo` is not supported.
* - Other librairies like [`date-fns`](https://date-fns.org/), [`day.js`](https://day.js.org/en/) or [`weekstart`](https://www.npmjs.com/package/weekstart) library provide their own locale based data for the first day of the week.
*/
export function getLocaleFirstDayOfWeek(locale: string): WeekDay {
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.FirstDayOfWeek];
}
/**
* Range of week days that are considered the week-end for the given locale.
*
* @param locale A locale code for the locale format rules to use.
* @returns The range of day values, `[startDay, endDay]`.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Intl's [`getWeekInfo`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getWeekInfo) has partial support (Chromium M99 & Safari 17).
* Libraries like [`Luxon`](https://moment.github.io/luxon/#/) rely on `Intl` but fallback on the ISO 8601 definition (Saturday+Sunday) if `getWeekInfo` is not supported .
*/
export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.WeekendRange];
}
/**
* Retrieves a localized date-value formatting string.
*
* @param locale A locale code for the locale format rules to use.
* @param width The format type.
* @returns The localized formatting string.
* @see {@link FormatWidth}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.DateTimeFormat` for date formating instead.
*/
export function getLocaleDateFormat(locale: string, width: FormatWidth): string {
const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[ɵLocaleDataIndex.DateFormat], width);
}
/**
* Retrieves a localized time-value formatting string.
*
* @param locale A locale code for the locale format rules to use.
* @param width The format type.
* @returns The localized formatting string.
* @see {@link FormatWidth}
* @see [Internationalization (i18n) Guide](guide/i18n)
* @publicApi
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.DateTimeFormat` for date formating instead.
*/
export function getLocaleTimeFormat(locale: string, width: FormatWidth): string {
const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[ɵLocaleDataIndex.TimeFormat], width);
}
/**
* Retrieves a localized date-time formatting string.
*
* @param locale A locale code for the locale format rules to use.
* @param width The format type.
* @returns The localized formatting string.
* @see {@link FormatWidth}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.DateTimeFormat` for date formating instead.
*/
export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): string {
const data = ɵfindLocaleData(locale);
const dateTimeFormatData = <string[]>data[ɵLocaleDataIndex.DateTimeFormat];
return getLastDefinedValue(dateTimeFormatData, width);
}
/**
* Retrieves a localized number symbol that can be used to replace placeholders in number formats.
* @param locale The locale code.
* @param symbol The symbol to localize. Must be one of `NumberSymbol`.
* @returns The character for the localized symbol.
* @see {@link NumberSymbol}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.NumberFormat` to format numbers instead.
*/
export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): string {
const data = ɵfindLocaleData(locale);
const res = data[ɵLocaleDataIndex.NumberSymbols][symbol];
if (typeof res === 'undefined') {
if (symbol === NumberSymbol.CurrencyDecimal) {
return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Decimal];
} else if (symbol === NumberSymbol.CurrencyGroup) {
return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Group];
}
}
return res;
}
/**
* Retrieves a number format for a given locale.
*
* Numbers are formatted using patterns, like `#,###.00`. For example, the pattern `#,###.00`
* when used to format the number 12345.678 could result in "12'345,678". That would happen if the
* grouping separator for your language is an apostrophe, and the decimal separator is a comma.
*
* <b>Important:</b> The characters `.` `,` `0` `#` (and others below) are special placeholders
* that stand for the decimal separator, and so on, and are NOT real characters.
* You must NOT "translate" the placeholders. For example, don't change `.` to `,` even though in
* your language the decimal point is written with a comma. The symbols should be replaced by the
* local equivalents, using the appropriate `NumberSymbol` for your language.
*
* Here are the special characters used in number patterns:
*
* | Symbol | Meaning |
* |--------|---------|
* | . | Replaced automatically by the character used for the decimal point. |
* | , | Replaced by the "grouping" (thousands) separator. |
* | 0 | Replaced by a digit (or zero if there aren't enough digits). |
* | # | Replaced by a digit (or nothing if there aren't enough). |
* | ¤ | Replaced by a currency symbol, such as $ or USD. |
* | % | Marks a percent format. The % symbol may change position, but must be retained. |
* | E | Marks a scientific format. The E symbol may change position, but must be retained. |
* | ' | Special characters used as literal characters are quoted with ASCII single quotes. |
*
* @param locale A locale code for the locale format rules to use.
* @param type The type of numeric value to be formatted (such as `Decimal` or `Currency`.)
* @returns The localized format string.
* @see {@link NumberFormatStyle}
* @see [CLDR website](http://cldr.unicode.org/translation/number-patterns)
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Let `Intl.NumberFormat` determine the number format instead
*/
export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string {
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.NumberFormats][type];
}
/**
* Retrieves the symbol used to represent the currency for the main country
* corresponding to a given locale. For example, '$' for `en-US`.
*
* @param locale A locale code for the locale format rules to use.
* @returns The localized symbol character,
* or `null` if the main country cannot be determined.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Use the `Intl` API to format a currency with from currency code
*/
export function getLocaleCurrencySymbol(locale: string): string | null {
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.CurrencySymbol] || null;
}
/**
* Retrieves the name of the currency for the main country corresponding
* to a given locale. For example, 'US Dollar' for `en-US`.
* @param locale A locale code for the locale format rules to use.
* @returns The currency name,
* or `null` if the main country cannot be determined.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Use the `Intl` API to format a currency with from currency code
*/
export function getLocaleCurrencyName(locale: string): string | null {
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.CurrencyName] || null;
}
/**
* Retrieves the default currency code for the given locale.
*
* The default is defined as the first currency which is still in use.
*
* @param locale The code of the locale whose currency code we want.
* @returns The code of the default currency for the given locale.
*
* @publicApi
*
* @deprecated We recommend you create a map of locale to ISO 4217 currency codes.
* Time relative currency data is provided by the CLDR project. See https://www.unicode.org/cldr/charts/44/supplemental/detailed_territory_currency_information.html
*/
export function getLocaleCurrencyCode(locale: string): string | null {
return ɵgetLocaleCurrencyCode(locale);
}
/**
* Retrieves the currency values for a given locale.
* @param locale A locale code for the locale format rules to use.
* @returns The currency values.
* @see [Internationalization (i18n) Guide](guide/i18n)
*/
function getLocaleCurrencies(locale: string): {[code: string]: CurrenciesSymbols} {
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.Currencies];
}
/**
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Use `Intl.PluralRules` instead
*/
export const getLocalePluralCase: (locale: string) => (value: number) => Plural =
ɵgetLocalePluralCase;
function checkFullData(data: any) {
if (!data[ɵLocaleDataIndex.ExtraData]) {
throw new RuntimeError(
RuntimeErrorCode.MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE,
ngDevMode &&
`Missing extra locale data for the locale "${
data[ɵLocaleDataIndex.LocaleId]
}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`,
);
}
}
/**
* Retrieves locale-specific rules used to determine which day period to use
* when more than one period is defined for a locale.
*
* There is a rule for each defined day period. The
* first rule is applied to the first day period and so on.
* Fall back to AM/PM when no rules are available.
*
* A rule can specify a period as time range, or as a single time value.
*
* This functionality is only available when you have loaded the full locale data.
* See the ["I18n guide"](guide/i18n/format-data-locale).
*
* @param locale A locale code for the locale format rules to use.
* @returns The rules for the locale, a single time value or array of *from-time, to-time*,
* or null if no periods are available.
*
* @see {@link getLocaleExtraDayPeriods}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* Let `Intl.DateTimeFormat` determine the day period instead.
*/
export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Time])[] {
const data = ɵfindLocaleData(locale);
checkFullData(data);
const rules = data[ɵLocaleDataIndex.ExtraData][ɵExtraLocaleDataIndex.ExtraDayPeriodsRules] || [];
return rules.map((rule: string | [string, string]) => {
if (typeof rule === 'string') {
return extractTime(rule);
}
return [extractTime(rule[0]), extractTime(rule[1])];
});
}
/**
* Retrieves locale-specific day periods, which indicate roughly how a day is broken up
* in different languages.
* For example, for `en-US`, periods are morning, noon, afternoon, evening, and midnight.
*
* This functionality is only available when you have loaded the full locale data.
* See the ["I18n guide"](guide/i18n/format-data-locale).
*
* @param locale A locale code for the locale format rules to use.
* @param formStyle The required grammatical form.
* @param width The required character width.
* @returns The translated day-period strings.
* @see {@link getLocaleExtraDayPeriodRules}
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* To extract a day period use `Intl.DateTimeFormat` with the `dayPeriod` option instead.
*/
export function getLocaleExtraDayPeriods(
locale: string,
formStyle: FormStyle,
width: TranslationWidth,
): string[] {
const data = ɵfindLocaleData(locale);
checkFullData(data);
const dayPeriodsData = <string[][][]>[
data[ɵLocaleDataIndex.ExtraData][ɵExtraLocaleDataIndex.ExtraDayPeriodFormats],
data[ɵLocaleDataIndex.ExtraData][ɵExtraLocaleDataIndex.ExtraDayPeriodStandalone],
];
const dayPeriods = getLastDefinedValue(dayPeriodsData, formStyle) || [];
return getLastDefinedValue(dayPeriods, width) || [];
}
/**
* Retrieves the writing direction of a specified locale
* @param locale A locale code for the locale format rules to use.
* @publicApi
* @returns 'rtl' or 'ltr'
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* For dates and numbers, let `Intl.DateTimeFormat()` and `Intl.NumberFormat()` determine the writing direction.
* The `Intl` alternative [`getTextInfo`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo).
* has only partial support (Chromium M99 & Safari 17).
* 3rd party alternatives like [`rtl-detect`](https://www.npmjs.com/package/rtl-detect) can work around this issue.
*/
export function getLocaleDirection(locale: string): 'ltr' | 'rtl' {
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.Directionality];
}
/**
* Retrieves the first value that is defined in an array, going backwards from an index position.
*
* To avoid repeating the same data (as when the "format" and "standalone" forms are the same)
* add the first value to the locale data arrays, and add other values only if they are different.
*
* @param data The data array to retrieve from.
* @param index A 0-based index into the array to start from.
* @returns The value immediately before the given index position.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
*/
function getLastDefinedValue<T>(data: T[], index: number): T {
for (let i = index; i > -1; i--) {
if (typeof data[i] !== 'undefined') {
return data[i];
}
}
throw new RuntimeError(
RuntimeErrorCode.LOCALE_DATA_UNDEFINED,
ngDevMode && 'Locale data API: locale data undefined',
);
}
/**
* Represents a time value with hours and minutes.
*
* @publicApi
*
* @deprecated Locale date getters are deprecated
*/
export type Time = {
hours: number;
minutes: number;
};
/**
* Extracts the hours and minutes from a string like "15:45"
*/
function extractTime(time: string): Time {
const [h, m] = time.split(':');
return {hours: +h, minutes: +m};
}
/**
* Retrieves the currency symbol for a given currency code.
*
* For example, for the default `en-US` locale, the code `USD` can
* be represented by the narrow symbol `$` or the wide symbol `US$`.
*
* @param code The currency code.
* @param format The format, `wide` or `narrow`.
* @param locale A locale code for the locale format rules to use.
*
* @returns The symbol, or the currency code if no symbol is available.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* You can use `Intl.NumberFormat().formatToParts()` to extract the currency symbol.
* For example: `Intl.NumberFormat('en', {style:'currency', currency: 'USD'}).formatToParts().find(part => part.type === 'currency').value`
* returns `$` for USD currency code in the `en` locale.
* Note: `US$` is a currency symbol for the `en-ca` locale but not the `en-us` locale.
*/
export function getCurrencySymbol(code: string, format: 'wide' | 'narrow', locale = 'en'): string {
const currency = getLocaleCurrencies(locale)[code] || CURRENCIES_EN[code] || [];
const symbolNarrow = currency[ɵCurrencyIndex.SymbolNarrow];
if (format === 'narrow' && typeof symbolNarrow === 'string') {
return symbolNarrow;
}
return currency[ɵCurrencyIndex.Symbol] || code;
}
// Most currencies have cents, that's why the default is 2
const DEFAULT_NB_OF_CURRENCY_DIGITS = 2;
/**
* Reports the number of decimal digits for a given currency.
* The value depends upon the presence of cents in that particular currency.
*
* @param code The currency code.
* @returns The number of decimal digits, typically 0 or 2.
* @see [Internationalization (i18n) Guide](guide/i18n)
*
* @publicApi
*
* @deprecated Angular recommends relying on the `Intl` API for i18n.
* This function should not be used anymore. Let `Intl.NumberFormat` determine the number of digits to display for the currency
*/
export function getNumberOfCurrencyDigits(code: string): number {
let digits;
const currency = CURRENCIES_EN[code];
if (currency) {
digits = currency[ɵCurrencyIndex.NbOfDigits];
}
return typeof digits === 'number' ? digits : DEFAULT_NB_OF_CURRENCY_DIGITS;
}