DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var EXPORTED_SYMBOLS = ["PluralForm"];

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

/**
 * This module provides the PluralForm object which contains a method to figure
 * out which plural form of a word to use for a given number based on the
 * current localization. There is also a makeGetter method that creates a get
 * function for the desired plural rule. This is useful for extensions that
 * specify their own plural rule instead of relying on the browser default.
 * (I.e., the extension hasn't been localized to the browser's locale.)
 *
 * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
 *
 * NOTE: any change to these plural forms need to be reflected in
 * compare-locales:
 * https://hg.mozilla.org/l10n/compare-locales/file/default/compare_locales/plurals.py
 *
 * List of methods:
 *
 * string pluralForm
 * get(int aNum, string aWords)
 *
 * int numForms
 * numForms()
 *
 * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
 * makeGetter(int aRuleNum)
 * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
 */

const kIntlProperties = "chrome://global/locale/intl.properties";

// These are the available plural functions that give the appropriate index
// based on the plural rule number specified. The first element is the number
// of plural forms and the second is the function to figure out the index.
/* eslint-disable no-nested-ternary */
var gFunctions = [
  // 0: Chinese
  [1, n => 0],
  // 1: English
  [2, n => (n != 1 ? 1 : 0)],
  // 2: French
  [2, n => (n > 1 ? 1 : 0)],
  // 3: Latvian
  [3, n => (n % 10 == 1 && n % 100 != 11 ? 1 : n % 10 == 0 ? 0 : 2)],
  // 4: Scottish Gaelic
  [
    4,
    n =>
      n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 0 && n < 20 ? 2 : 3,
  ],
  // 5: Romanian
  [3, n => (n == 1 ? 0 : n == 0 || (n % 100 > 0 && n % 100 < 20) ? 1 : 2)],
  // 6: Lithuanian
  [
    3,
    n =>
      n % 10 == 1 && n % 100 != 11
        ? 0
        : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20)
        ? 2
        : 1,
  ],
  // 7: Russian
  [
    3,
    n =>
      n % 10 == 1 && n % 100 != 11
        ? 0
        : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
        ? 1
        : 2,
  ],
  // 8: Slovak
  [3, n => (n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2)],
  // 9: Polish
  [
    3,
    n =>
      n == 1
        ? 0
        : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
        ? 1
        : 2,
  ],
  // 10: Slovenian
  [
    4,
    n =>
      n % 100 == 1
        ? 0
        : n % 100 == 2
        ? 1
        : n % 100 == 3 || n % 100 == 4
        ? 2
        : 3,
  ],
  // 11: Irish Gaeilge
  [
    5,
    n =>
      n == 1
        ? 0
        : n == 2
        ? 1
        : n >= 3 && n <= 6
        ? 2
        : n >= 7 && n <= 10
        ? 3
        : 4,
  ],
  // 12: Arabic
  [
    6,
    n =>
      n == 0
        ? 5
        : n == 1
        ? 0
        : n == 2
        ? 1
        : n % 100 >= 3 && n % 100 <= 10
        ? 2
        : n % 100 >= 11 && n % 100 <= 99
        ? 3
        : 4,
  ],
  // 13: Maltese
  [
    4,
    n =>
      n == 1
        ? 0
        : n == 0 || (n % 100 > 0 && n % 100 <= 10)
        ? 1
        : n % 100 > 10 && n % 100 < 20
        ? 2
        : 3,
  ],
  // 14: Unused
  [3, n => (n % 10 == 1 ? 0 : n % 10 == 2 ? 1 : 2)],
  // 15: Icelandic, Macedonian
  [2, n => (n % 10 == 1 && n % 100 != 11 ? 0 : 1)],
  // 16: Breton
  [
    5,
    n =>
      n % 10 == 1 && n % 100 != 11 && n % 100 != 71 && n % 100 != 91
        ? 0
        : n % 10 == 2 && n % 100 != 12 && n % 100 != 72 && n % 100 != 92
        ? 1
        : (n % 10 == 3 || n % 10 == 4 || n % 10 == 9) &&
          n % 100 != 13 &&
          n % 100 != 14 &&
          n % 100 != 19 &&
          n % 100 != 73 &&
          n % 100 != 74 &&
          n % 100 != 79 &&
          n % 100 != 93 &&
          n % 100 != 94 &&
          n % 100 != 99
        ? 2
        : n % 1000000 == 0 && n != 0
        ? 3
        : 4,
  ],
  // 17: Shuar
  [2, n => (n != 0 ? 1 : 0)],
  // 18: Welsh
  [
    6,
    n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n == 3 ? 3 : n == 6 ? 4 : 5),
  ],
  // 19: Slavic languages (bs, hr, sr). Same as rule 7, but resulting in different CLDR categories
  [
    3,
    n =>
      n % 10 == 1 && n % 100 != 11
        ? 0
        : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
        ? 1
        : 2,
  ],
];
/* eslint-enable no-nested-ternary */

var PluralForm = {
  /**
   * Get the correct plural form of a word based on the number
   *
   * @param aNum
   *        The number to decide which plural form to use
   * @param aWords
   *        A semi-colon (;) separated string of words to pick the plural form
   * @return The appropriate plural form of the word
   */
  get get() {
    // This method will lazily load to avoid perf when it is first needed and
    // creates getPluralForm function. The function it creates is based on the
    // value of pluralRule specified in the intl stringbundle.
    // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals

    // Delete the getters to be overwritten
    delete PluralForm.numForms;
    delete PluralForm.get;

    // Make the plural form get function and set it as the default get
    [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(
      PluralForm.ruleNum
    );
    return PluralForm.get;
  },

  /**
   * Create a pair of plural form functions for the given plural rule number.
   *
   * @param aRuleNum
   *        The plural rule number to create functions
   * @return A pair: [function that gets the right plural form,
   *                  function that returns the number of plural forms]
   */
  makeGetter(aRuleNum) {
    // Default to "all plural" if the value is out of bounds or invalid
    if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
      log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
      aRuleNum = 0;
    }

    // Get the desired pluralRule function
    let [numForms, pluralFunc] = gFunctions[aRuleNum];

    // Return functions that give 1) the number of forms and 2) gets the right
    // plural form
    return [
      function(aNum, aWords) {
        // Figure out which index to use for the semi-colon separated words
        let index = pluralFunc(aNum ? Number(aNum) : 0);
        let words = aWords ? aWords.split(/;/) : [""];

        // Explicitly check bounds to avoid strict warnings
        let ret = index < words.length ? words[index] : undefined;

        // Check for array out of bounds or empty strings
        if (ret == undefined || ret == "") {
          // Report the caller to help figure out who is causing badness
          let caller = Components.stack.caller
            ? Components.stack.caller.name
            : "top";

          // Display a message in the error console
          log([
            "Index #",
            index,
            " of '",
            aWords,
            "' for value ",
            aNum,
            " is invalid -- plural rule #",
            aRuleNum,
            "; called by ",
            caller,
          ]);

          // Default to the first entry (which might be empty, but not undefined)
          ret = words[0];
        }

        return ret;
      },
      () => numForms,
    ];
  },

  /**
   * Get the number of forms for the current plural rule
   *
   * @return The number of forms
   */
  get numForms() {
    // We lazily load numForms, so trigger the init logic with get()
    PluralForm.get();
    return PluralForm.numForms;
  },

  /**
   * Get the plural rule number from the intl stringbundle
   *
   * @return The plural rule number
   */
  get ruleNum() {
    return Number(
      Services.strings
        .createBundle(kIntlProperties)
        .GetStringFromName("pluralRule")
    );
  },
};

/**
 * Private helper function to log errors to the error console and command line
 *
 * @param aMsg
 *        Error message to log or an array of strings to concat
 */
function log(aMsg) {
  let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
  Services.console.logStringMessage(msg);
  dump(msg + "\n");
}