intl-pluralrules-select.js 1.46 KB
Newer Older
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
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

if (this.Intl) {
  var pr;
  var suffixes;
  function format(n) {
    return "" + n + suffixes[pr.select(n)];
  }

  // These English examples illustrate the purpose of the PluralRules class.
  pr = new Intl.PluralRules("en-US");
  suffixes = {
    one:   " day",
    other: " days",
  };
  assertEquals("0 days",   format(0));
  assertEquals("0.5 days", format(0.5));
  assertEquals("1 day",    format(1));
  assertEquals("1.5 days", format(1.5));
  assertEquals("2 days",   format(2));

  pr = new Intl.PluralRules("en-US", {type: "ordinal"});
  suffixes = {
    one:   "st",
    two:   "nd",
    few:   "rd",
    other: "th",
  };
  assertEquals("0th",   format(0));
  assertEquals("1st",   format(1));
  assertEquals("2nd",   format(2));
  assertEquals("3rd",   format(3));
  assertEquals("4th",   format(4));
  assertEquals("11th",  format(11));
  assertEquals("21st",  format(21));
  assertEquals("103rd", format(103));

  // Arabic can cause every possible return value from select()
  pr = new Intl.PluralRules("ar");
  suffixes = null;
  assertEquals("zero",  pr.select(0));
  assertEquals("one",   pr.select(1));
  assertEquals("two",   pr.select(2));
  assertEquals("few",   pr.select(3));
  assertEquals("many",  pr.select(11));
  assertEquals("other", pr.select(100));
  assertEquals("other", pr.select(1.5));
}