Bug 1422658 - Extend mozIntl to handle NumberFormat, PluralRules and Collator. r?jfkthame
MozReview-Commit-ID: Gtnvch5Ict6
--- a/toolkit/components/mozintl/mozIMozIntl.idl
+++ b/toolkit/components/mozintl/mozIMozIntl.idl
@@ -37,9 +37,12 @@
[scriptable, uuid(7f63279a-1a29-4ae6-9e7a-dc9684a23530)]
interface mozIMozIntl : nsISupports
{
jsval getCalendarInfo([optional] in jsval locales);
jsval getDisplayNames([optional] in jsval locales, [optional] in jsval options);
jsval getLocaleInfo([optional] in jsval locales);
readonly attribute jsval DateTimeFormat;
+ readonly attribute jsval NumberFormat;
+ readonly attribute jsval Collator;
+ readonly attribute jsval PluralRules;
};
--- a/toolkit/components/mozintl/mozIntl.js
+++ b/toolkit/components/mozintl/mozIntl.js
@@ -90,15 +90,42 @@ class MozIntl {
options.pattern = undefined;
}
}
super(resolvedLocales, options, ...args);
}
}
return MozDateTimeFormat;
}
+
+ get NumberFormat() {
+ class MozNumberFormat extends Intl.NumberFormat {
+ constructor(locales, options, ...args) {
+ super(getLocales(locales), options, ...args);
+ }
+ }
+ return MozNumberFormat;
+ }
+
+ get Collator() {
+ class MozCollator extends Intl.Collator {
+ constructor(locales, options, ...args) {
+ super(getLocales(locales), options, ...args);
+ }
+ }
+ return MozCollator;
+ }
+
+ get PluralRules() {
+ class MozPluralRules extends Intl.PluralRules {
+ constructor(locales, options, ...args) {
+ super(getLocales(locales), options, ...args);
+ }
+ }
+ return MozPluralRules;
+ }
}
MozIntl.prototype.classID = Components.ID("{35ec195a-e8d0-4300-83af-c8a2cc84b4a3}");
MozIntl.prototype.QueryInterface = XPCOMUtils.generateQI([Ci.mozIMozIntl, Ci.nsISupports]);
var components = [MozIntl];
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
--- a/toolkit/components/mozintl/test/test_mozintl.js
+++ b/toolkit/components/mozintl/test/test_mozintl.js
@@ -22,27 +22,31 @@ function test_methods_calling() {
Services.intl.getCalendarInfo("pl");
Services.intl.getDisplayNames("ar");
Services.intl.getLocaleInfo("de");
new Services.intl.DateTimeFormat("fr");
ok(true);
}
function test_constructors() {
- let dtf = new Intl.DateTimeFormat();
- let dtf2 = new Services.intl.DateTimeFormat();
+ let constructors = ["DateTimeFormat", "NumberFormat", "PluralRules", "Collator"];
- equal(typeof dtf, typeof dtf2);
+ constructors.forEach(constructor => {
+ let obj = new Intl[constructor]();
+ let obj2 = new Services.intl[constructor]();
+
+ equal(typeof obj, typeof obj2);
- Assert.throws(() => {
- // This is an observable difference between Intl and mozIntl.
- //
- // Old ECMA402 APIs (edition 1 and 2) allowed for constructors to be called
- // as functions.
- // Starting from ed.3 all new constructors are throwing when called without |new|.
- //
- // All MozIntl APIs do not implement the legacy behavior and throw
- // when called without |new|.
- //
- // For more information see https://github.com/tc39/ecma402/pull/84 .
- Services.intl.DateTimeFormat();
- }, /class constructors must be invoked with |new|/);
+ Assert.throws(() => {
+ // This is an observable difference between Intl and mozIntl.
+ //
+ // Old ECMA402 APIs (edition 1 and 2) allowed for constructors to be called
+ // as functions.
+ // Starting from ed.3 all new constructors are throwing when called without |new|.
+ //
+ // All MozIntl APIs do not implement the legacy behavior and throw
+ // when called without |new|.
+ //
+ // For more information see https://github.com/tc39/ecma402/pull/84 .
+ Services.intl[constructor]();
+ }, /class constructors must be invoked with |new|/);
+ });
}