This repository was archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathlocales.js
101 lines (84 loc) · 3.22 KB
/
locales.js
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
import * as glob from 'glob';
import * as path from 'path';
let CLDR_DATES_DIR = path.dirname(require.resolve('cldr-dates-full/package.json'));
let CLDR_NUMBERS_DIR = path.dirname(require.resolve('cldr-numbers-full/package.json'));
// These are the exceptions to the default algorithm for determining a locale's
// parent locale.
let PARENT_LOCALES_HASH = require('cldr-core/supplemental/parentLocales.json')
.supplemental.parentLocales.parentLocale;
let CALENDARS_LOCALES_HASH = glob.sync('*/ca-*.json', {
cwd: path.resolve(CLDR_DATES_DIR, 'main')
}).reduce((hash, filename) => {
hash[path.dirname(filename)] = true;
return hash;
}, {});
let NUMBERS_LOCALES_HASH = glob.sync('*/numbers.json', {
cwd: path.resolve(CLDR_NUMBERS_DIR, 'main')
}).reduce((hash, filename) => {
hash[path.dirname(filename)] = true;
return hash;
}, {});
let CURRENCIES_LOCALES_HASH = glob.sync('*/currencies.json', {
cwd: path.resolve(CLDR_NUMBERS_DIR, 'main')
}).reduce((hash, filename) => {
hash[path.dirname(filename)] = true;
return hash;
}, {});
let DEFAULT_CONTENT_ARRAY = require('cldr-core/defaultContent.json')
.defaultContent.map((value) => {
return value.replace(/_/g, '-');
});
// Some locales that have a `pluralRuleFunction` don't have a `dateFields.json`
// file, and visa versa, so this creates a unique collection of all locales in
// the CLDR for which we need data from.
let ALL_LOCALES_HASH =
Object.keys(PARENT_LOCALES_HASH)
.concat(Object.keys(CALENDARS_LOCALES_HASH))
.concat(Object.keys(NUMBERS_LOCALES_HASH))
.concat(Object.keys(CURRENCIES_LOCALES_HASH))
.concat(DEFAULT_CONTENT_ARRAY)
.sort()
.reduce((hash, locale) => {
hash[locale.toLowerCase()] = locale;
return hash;
}, {});
export function getAllLocales() {
return Object.keys(ALL_LOCALES_HASH);
}
export function getParentLocale(locale) {
locale = normalizeLocale(locale);
// If we don't know about the locale, or if it's the "root" locale, then its
// parent should be `undefined`.
if (!locale || locale === 'root') {
return;
}
// First check the exceptions for locales which don't follow the standard
// hierarchical pattern.
let parentLocale = PARENT_LOCALES_HASH[locale];
if (parentLocale) {
return parentLocale;
}
// Be default, the language tags are hierarchal, therefore we can identify
// the parent locale by simply popping off the last segment.
let localeParts = locale.split('-');
if (localeParts.length > 1) {
localeParts.pop();
return localeParts.join('-');
}
// When there's nothing left in the hierarchy, the parent is the "root".
return 'root';
}
export function hasCalendars(locale) {
return CALENDARS_LOCALES_HASH.hasOwnProperty(normalizeLocale(locale));
}
export function hasNumbersFields(locale) {
return NUMBERS_LOCALES_HASH.hasOwnProperty(normalizeLocale(locale)) &&
CURRENCIES_LOCALES_HASH.hasOwnProperty(normalizeLocale(locale));
}
export function normalizeLocale(locale) {
let normalizedLocale = ALL_LOCALES_HASH[locale.toLowerCase()];
if (normalizedLocale) {
return normalizedLocale;
}
throw new Error('No locale data for: "' + locale + '"');
}