forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
locale_data_api.ts
84 lines (70 loc) · 2.56 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
/**
* @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';
/**
* 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];
}
/**
* 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}".`);
}
function getLocaleData(normalizedLocale: string): any {
if (normalizedLocale in LOCALE_DATA) {
return LOCALE_DATA[normalizedLocale];
}
if (typeof global.ng === 'undefined') global.ng = {};
if (typeof global.ng.common === 'undefined') global.ng.common = {};
if (typeof global.ng.common.locale === 'undefined') global.ng.common.locale = {};
// The locale names on the global object are not normalized, so we have to do a search.
// This is only once per requested locale; after that it is cached on LOCALE_DATA.
// Also generally only one or very few locales should be loaded onto the global.
for (const l in global.ng.common.locale) {
if (normalizeLocale(l) === normalizedLocale) {
const localeData = LOCALE_DATA[normalizedLocale] = global.ng.common.locale[l];
if (localeData !== undefined) {
localeData[LocaleDataIndex.ExtraData] = global.ng.common.locale[`extra/${l}`];
return localeData;
}
}
}
return undefined;
}
function normalizeLocale(locale: string): string {
return locale.toLowerCase().replace(/_/g, '-');
}