Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(926)

Unified Diff: src/js/i18n.js

Issue 2736543002: [intl] Implement Intl.PluralRules
Patch Set: clarify comment Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/js/i18n.js
diff --git a/src/js/i18n.js b/src/js/i18n.js
index 3be9ed385a0b8e16c7ddae9878c38c2822fb8db3..43eba159d5707f01c4c2c5907fe78cefc54c5f93 100644
--- a/src/js/i18n.js
+++ b/src/js/i18n.js
@@ -24,6 +24,7 @@ var GlobalIntl = global.Intl;
var GlobalIntlDateTimeFormat = GlobalIntl.DateTimeFormat;
var GlobalIntlNumberFormat = GlobalIntl.NumberFormat;
var GlobalIntlCollator = GlobalIntl.Collator;
+var GlobalIntlPluralRules = utils.ImportNow("PluralRules");
var GlobalIntlv8BreakIterator = GlobalIntl.v8BreakIterator;
var GlobalNumber = global.Number;
var GlobalRegExp = global.RegExp;
@@ -34,6 +35,7 @@ var InstallGetter = utils.InstallGetter;
var InternalArray = utils.InternalArray;
var MaxSimple;
var ObjectHasOwnProperty = utils.ImportNow("ObjectHasOwnProperty");
+var ObjectKeys = global.Object.keys;
var OverrideFunction = utils.OverrideFunction;
var patternSymbol = utils.ImportNow("intl_pattern_symbol");
var resolvedSymbol = utils.ImportNow("intl_resolved_symbol");
@@ -138,7 +140,8 @@ var AVAILABLE_LOCALES = {
'collator': UNDEFINED,
'numberformat': UNDEFINED,
'dateformat': UNDEFINED,
- 'breakiterator': UNDEFINED
+ 'breakiterator': UNDEFINED,
+ 'pluralrules': UNDEFINED,
};
/**
@@ -204,7 +207,7 @@ var SERVICE_RE = UNDEFINED;
function GetServiceRE() {
if (IS_UNDEFINED(SERVICE_RE)) {
SERVICE_RE =
- new GlobalRegExp('^(collator|numberformat|dateformat|breakiterator)$');
+ new GlobalRegExp('^(' + %_Call(ArrayJoin, ObjectKeys(AVAILABLE_LOCALES), '|') + ')$');
}
return SERVICE_RE;
}
@@ -1106,6 +1109,103 @@ function compare(collator, x, y) {
AddBoundMethod(GlobalIntlCollator, 'compare', compare, 2, 'collator', false);
+function PluralRulesConstructor() {
+ var locales = arguments[0];
+ var options = arguments[1];
+
+ if (IS_UNDEFINED(options)) {
+ options = {};
+ }
+
+ var getOption = getGetOption(options, 'pluralrules');
+
+ var locale = resolveLocale('pluralrules', locales, options);
+
+ var internalOptions = {};
+ defineWEProperty(internalOptions, 'type', getOption(
+ 'type', 'string', ['cardinal', 'ordinal'], 'cardinal'));
+
+ SetNumberFormatDigitOptions(internalOptions, options, 0, 3);
+
+ var requestedLocale = locale.locale;
+ var resolved = %object_define_properties({}, {
+ type: {value: internalOptions.type, writable: true},
+ locale: {writable: true},
+ maximumFractionDigits: {writable: true},
+ minimumFractionDigits: {writable: true},
+ minimumIntegerDigits: {writable: true},
+ requestedLocale: {value: requestedLocale, writable: true},
+ });
+ if (HAS_OWN_PROPERTY(internalOptions, 'minimumSignificantDigits')) {
+ defineWEProperty(resolved, 'minimumSignificantDigits', UNDEFINED);
+ }
+ if (HAS_OWN_PROPERTY(internalOptions, 'maximumSignificantDigits')) {
+ defineWEProperty(resolved, 'maximumSignificantDigits', UNDEFINED);
+ }
+ defineWEProperty(resolved, 'pluralCategories', []);
+ var pluralRules = %CreatePluralRules(requestedLocale, internalOptions,
+ resolved);
+
+ %MarkAsInitializedIntlObjectOfType(pluralRules, 'pluralrules');
+ pluralRules[resolvedSymbol] = resolved;
+
+ return pluralRules;
+}
+%SetCode(GlobalIntlPluralRules, PluralRulesConstructor);
+
+InstallFunction(GlobalIntlPluralRules.prototype, 'resolvedOptions',
+ function() {
+ if (!%IsInitializedIntlObjectOfType(this, 'pluralrules')) {
+ throw %make_type_error(kIncompatibleMethodReceiver,
+ 'Intl.PluralRules.prototype.resolvedOptions',
+ this);
+ }
+ var locale = getOptimalLanguageTag(this[resolvedSymbol].requestedLocale,
+ this[resolvedSymbol].locale);
+
+ var result = {
+ locale: locale,
+ type: this[resolvedSymbol].type,
+ minimumIntegerDigits: this[resolvedSymbol].minimumIntegerDigits,
+ minimumFractionDigits: this[resolvedSymbol].minimumFractionDigits,
+ maximumFractionDigits: this[resolvedSymbol].maximumFractionDigits,
+ };
+
+ if (HAS_OWN_PROPERTY(this[resolvedSymbol], 'minimumSignificantDigits')) {
+ defineWECProperty(result, 'minimumSignificantDigits',
+ this[resolvedSymbol].minimumSignificantDigits);
+ }
+
+ if (HAS_OWN_PROPERTY(this[resolvedSymbol], 'maximumSignificantDigits')) {
+ defineWECProperty(result, 'maximumSignificantDigits',
+ this[resolvedSymbol].maximumSignificantDigits);
+ }
+
+ defineWECProperty(result, 'pluralCategories',
+ this[resolvedSymbol].pluralCategories);
+
+ return result;
+ }
+);
+
+InstallFunction(GlobalIntlPluralRules, 'supportedLocalesOf',
+ function(locales) {
+ return supportedLocalesOf('pluralrules', locales, arguments[1]);
+ }
+);
+
+InstallFunction(GlobalIntlPluralRules.prototype, 'select',
+ function(value) {
+ if (!%IsInitializedIntlObjectOfType(this, 'pluralrules')) {
+ throw %make_type_error(kIncompatibleMethodReceiver,
+ 'Intl.PluralRules.prototype.select',
+ this);
+ }
+
+ return %PluralRulesSelect(this, TO_NUMBER(value) + 0);
+ }
+);
+
/**
* Verifies that the input is a well-formed ISO 4217 currency code.
* Don't uppercase to test. It could convert invalid code into a valid one.

Powered by Google App Engine
This is Rietveld 408576698