/
locale_data_api.ts
102 lines (86 loc) 路 2.92 KB
/
locale_data_api.ts
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
/**
* @license
* Copyright Google Inc. 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.io/license
*/
import {LOCALE_DATA, LocaleDataIndex} from './locale_data';
import localeEn from './locale_en';
import {global} from '../util/global';
/**
* Register locale data to be used internally by Angular. See the
* ["I18n guide"](guide/i18n#i18n-pipes) to know how to import additional locale data.
*
* The signature `registerLocaleData(data: any, extraData?: any)` is deprecated since v5.1
*/
export function registerLocaleData(data: any, localeId?: string | any, extraData?: any): void {
if (typeof localeId !== 'string') {
extraData = localeId;
localeId = data[LocaleDataIndex.LocaleId];
}
localeId = localeId.toLowerCase().replace(/_/g, '-');
LOCALE_DATA[localeId] = data;
if (extraData) {
LOCALE_DATA[localeId][LocaleDataIndex.ExtraData] = extraData;
}
}
/**
* Finds the locale data for a given locale.
*
* @param locale The locale code.
* @returns The locale data.
* @see [Internationalization (i18n) Guide](https://angular.io/guide/i18n)
*/
export function findLocaleData(locale: string): any {
const normalizedLocale = normalizeLocale(locale);
let match = getLocaleData(normalizedLocale);
if (match) {
return match;
}
// let's try to find a parent locale
const parentLocale = normalizedLocale.split('-')[0];
match = getLocaleData(parentLocale);
if (match) {
return match;
}
if (parentLocale === 'en') {
return localeEn;
}
throw new Error(`Missing locale data for the locale "${locale}".`);
}
/**
* Retrieves the plural function used by ICU expressions to determine the plural case to use
* for a given locale.
* @param locale A locale code for the locale format rules to use.
* @returns The plural function for the locale.
* @see `NgPlural`
* @see [Internationalization (i18n) Guide](https://angular.io/guide/i18n)
*/
export function getLocalePluralCase(locale: string): (value: number) => number {
const data = findLocaleData(locale);
return data[LocaleDataIndex.PluralCase];
}
/**
* Helper function to get the given `normalizedLocale` from `LOCALE_DATA`
* or from the global `ng.common.locale`.
*/
export function getLocaleData(normalizedLocale: string): any {
if (!(normalizedLocale in LOCALE_DATA)) {
LOCALE_DATA[normalizedLocale] = global.ng && global.ng.common && global.ng.common.locale &&
global.ng.common.locale[normalizedLocale];
}
return LOCALE_DATA[normalizedLocale];
}
/**
* Helper function to remove all the locale data from `LOCALE_DATA`.
*/
export function unregisterLocaleData() {
Object.keys(LOCALE_DATA).forEach(key => delete LOCALE_DATA[key]);
}
/**
* Returns the canonical form of a locale name - lowercase with `_` replaced with `-`.
*/
function normalizeLocale(locale: string): string {
return locale.toLowerCase().replace(/_/g, '-');
}