home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2010 April / PCA177.iso / ESSENTIALS / Firefox Setup.exe / nonlocalized / modules / PluralForm.jsm < prev    next >
Encoding:
Text File  |  2009-07-15  |  7.5 KB  |  208 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Plural Form l10n Code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Edward Lee <edward.lee@engineering.uiuc.edu>.
  18.  * Portions created by the Initial Developer are Copyright (C) 2008
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var EXPORTED_SYMBOLS = [ "PluralForm" ];
  38.  
  39. /**
  40.  * This module provides the PluralForm object which contains a method to figure
  41.  * out which plural form of a word to use for a given number based on the
  42.  * current localization. There is also a makeGetter method that creates a get
  43.  * function for the desired plural rule. This is useful for extensions that
  44.  * specify their own plural rule instead of relying on the browser default.
  45.  * (I.e., the extension hasn't been localized to the browser's locale.)
  46.  *
  47.  * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
  48.  *
  49.  * List of methods:
  50.  *
  51.  * string pluralForm
  52.  * get(int aNum, string aWords)
  53.  *
  54.  * int numForms
  55.  * numForms()
  56.  *
  57.  * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
  58.  * makeGetter(int aRuleNum)
  59.  * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
  60.  */
  61.  
  62. const Cc = Components.classes;
  63. const Ci = Components.interfaces;
  64.  
  65. const kIntlProperties = "chrome://global/locale/intl.properties";
  66.  
  67. // These are the available plural functions that give the appropriate index
  68. // based on the plural rule number specified. The first element is the number
  69. // of plural forms and the second is the function to figure out the index.
  70. let gFunctions = [
  71.   // 0: Chinese
  72.   [1, function(n) 0],
  73.   // 1: English
  74.   [2, function(n) n!=1?1:0],
  75.   // 2: French
  76.   [2, function(n) n>1?1:0],
  77.   // 3: Latvian
  78.   [3, function(n) n%10==1&&n%100!=11?1:n!=0?2:0],
  79.   // 4: Scottish Gaelic
  80.   [3, function(n) n==1?0:n==2?1:2],
  81.   // 5: Romanian
  82.   [3, function(n) n==1?0:n==0||n%100>0&&n%100<20?1:2],
  83.   // 6: Lithuanian
  84.   [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
  85.   // 7: Russian
  86.   [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
  87.   // 8: Slovak
  88.   [3, function(n) n==1?0:n>=2&&n<=4?1:2],
  89.   // 9: Polish
  90.   [3, function(n) n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
  91.   // 10: Slovenian
  92.   [4, function(n) n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
  93.   // 11: Irish Gaeilge
  94.   [5, function(n) n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
  95.   // 12: Arabic
  96.   [6, function(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],
  97.   // 13: Maltese
  98.   [4, function(n) n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
  99.   // 14: Macedonian
  100.   [3, function(n) n%10==1?0:n%10==2?1:2],
  101.   // 15: Icelandic
  102.   [2, function(n) n%10==1&&n%100!=11?0:1],
  103. ];
  104.  
  105. let PluralForm = {
  106.   /**
  107.    * Get the correct plural form of a word based on the number
  108.    *
  109.    * @param aNum
  110.    *        The number to decide which plural form to use
  111.    * @param aWords
  112.    *        A semi-colon (;) separated string of words to pick the plural form
  113.    * @return The appropriate plural form of the word
  114.    */
  115.   get get()
  116.   {
  117.     // This method will lazily load to avoid perf when it is first needed and
  118.     // creates getPluralForm function. The function it creates is based on the
  119.     // value of pluralRule specified in the intl stringbundle.
  120.     // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
  121.  
  122.     // Delete the getters to be overwritten
  123.     delete PluralForm.numForms;
  124.     delete PluralForm.get;
  125.  
  126.     // Get the plural rule number from the intl stringbundle
  127.     let ruleNum = Number(Cc["@mozilla.org/intl/stringbundle;1"].
  128.       getService(Ci.nsIStringBundleService).createBundle(kIntlProperties).
  129.       GetStringFromName("pluralRule"));
  130.  
  131.     // Make the plural form get function and set it as the default get
  132.     [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(ruleNum);
  133.     return PluralForm.get;
  134.   },
  135.  
  136.   /**
  137.    * Create a pair of plural form functions for the given plural rule number.
  138.    *
  139.    * @param aRuleNum
  140.    *        The plural rule number to create functions
  141.    * @return A pair: [function that gets the right plural form,
  142.    *                  function that returns the number of plural forms]
  143.    */
  144.   makeGetter: function(aRuleNum)
  145.   {
  146.     // Default to "all plural" if the value is out of bounds or invalid
  147.     if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
  148.       log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
  149.       aRuleNum = 0;
  150.     }
  151.  
  152.     // Get the desired pluralRule function
  153.     let [numForms, pluralFunc] = gFunctions[aRuleNum];
  154.  
  155.     // Return functions that give 1) the number of forms and 2) gets the right
  156.     // plural form
  157.     return [function(aNum, aWords) {
  158.       // Figure out which index to use for the semi-colon separated words
  159.       let index = pluralFunc(aNum ? Number(aNum) : 0);
  160.       let words = aWords ? aWords.split(/;/) : [""];
  161.  
  162.       // Explicitly check bounds to avoid strict warnings
  163.       let ret = index < words.length ? words[index] : undefined;
  164.  
  165.       // Check for array out of bounds or empty strings
  166.       if ((ret == undefined) || (ret == "")) {
  167.         // Report the caller to help figure out who is causing badness
  168.         let caller = PluralForm.get.caller ? PluralForm.get.caller.name : "top";
  169.  
  170.         // Display a message in the error console
  171.         log(["Index #", index, " of '", aWords, "' for value ", aNum,
  172.             " is invalid -- plural rule #", aRuleNum, "; called by ", caller]);
  173.  
  174.         // Default to the first entry (which might be empty, but not undefined)
  175.         ret = words[0];
  176.       }
  177.  
  178.       return ret;
  179.     }, function() numForms];
  180.   },
  181.  
  182.   /**
  183.    * Get the number of forms for the current plural rule
  184.    *
  185.    * @return The number of forms
  186.    */
  187.   get numForms()
  188.   {
  189.     // We lazily load numForms, so trigger the init logic with get()
  190.     PluralForm.get();
  191.     return PluralForm.numForms;
  192.   },
  193. };
  194.  
  195. /**
  196.  * Private helper function to log errors to the error console and command line
  197.  *
  198.  * @param aMsg
  199.  *        Error message to log or an array of strings to concat
  200.  */
  201. function log(aMsg)
  202. {
  203.   let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
  204.   Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
  205.     logStringMessage(msg);
  206.   dump(msg + "\n");
  207. }
  208.