UNPKG

3.14 kBJavaScriptView Raw
1/**
2 * @license Copyright 2013 Andy Earnshaw, MIT License
3 *
4 * Implements the ECMAScript Internationalization API in ES5-compatible environments,
5 * following the ECMA-402 specification as closely as possible
6 *
7 * ECMA-402: http://ecma-international.org/ecma-402/1.0/
8 *
9 * CLDR format locale data should be provided using IntlPolyfill.__addLocaleData().
10 */
11
12import {
13 defineProperty,
14 hop,
15 arrPush,
16 arrShift,
17 internals,
18} from "./util.js";
19
20import {
21 IsStructurallyValidLanguageTag,
22 defaultLocale,
23 setDefaultLocale,
24} from "./6.locales-currencies-tz.js";
25
26import {
27 Intl,
28} from "./8.intl.js";
29
30import "./11.numberformat.js";
31
32import "./12.datetimeformat.js";
33
34import ls from "./13.locale-sensitive-functions.js";
35
36defineProperty(Intl, '__applyLocaleSensitivePrototypes', {
37 writable: true,
38 configurable: true,
39 value: function () {
40 defineProperty(Number.prototype, 'toLocaleString', { writable: true, configurable: true, value: ls.Number.toLocaleString });
41 // Need this here for IE 8, to avoid the _DontEnum_ bug
42 defineProperty(Date.prototype, 'toLocaleString', { writable: true, configurable: true, value: ls.Date.toLocaleString });
43
44 for (let k in ls.Date) {
45 if (hop.call(ls.Date, k))
46 defineProperty(Date.prototype, k, { writable: true, configurable: true, value: ls.Date[k] });
47 }
48 },
49});
50
51/**
52 * Can't really ship a single script with data for hundreds of locales, so we provide
53 * this __addLocaleData method as a means for the developer to add the data on an
54 * as-needed basis
55 */
56defineProperty(Intl, '__addLocaleData', {
57 value: function (data) {
58 if (!IsStructurallyValidLanguageTag(data.locale))
59 throw new Error("Object passed doesn't identify itself with a valid language tag");
60
61 addLocaleData(data, data.locale);
62 },
63});
64
65function addLocaleData (data, tag) {
66 // Both NumberFormat and DateTimeFormat require number data, so throw if it isn't present
67 if (!data.number)
68 throw new Error("Object passed doesn't contain locale data for Intl.NumberFormat");
69
70 let locale,
71 locales = [ tag ],
72 parts = tag.split('-');
73
74 // Create fallbacks for locale data with scripts, e.g. Latn, Hans, Vaii, etc
75 if (parts.length > 2 && parts[1].length === 4)
76 arrPush.call(locales, parts[0] + '-' + parts[2]);
77
78 while ((locale = arrShift.call(locales))) {
79 // Add to NumberFormat internal properties as per 11.2.3
80 arrPush.call(internals.NumberFormat['[[availableLocales]]'], locale);
81 internals.NumberFormat['[[localeData]]'][locale] = data.number;
82
83 // ...and DateTimeFormat internal properties as per 12.2.3
84 if (data.date) {
85 data.date.nu = data.number.nu;
86 arrPush.call(internals.DateTimeFormat['[[availableLocales]]'], locale);
87 internals.DateTimeFormat['[[localeData]]'][locale] = data.date;
88 }
89 }
90
91 // If this is the first set of locale data added, make it the default
92 if (defaultLocale === undefined)
93 setDefaultLocale(tag);
94}
95
96export default Intl;