PluralForm.jsm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. this.EXPORTED_SYMBOLS = [ "PluralForm" ];
  5. /**
  6. * This module provides the PluralForm object which contains a method to figure
  7. * out which plural form of a word to use for a given number based on the
  8. * current localization. There is also a makeGetter method that creates a get
  9. * function for the desired plural rule. This is useful for extensions that
  10. * specify their own plural rule instead of relying on the browser default.
  11. * (I.e., the extension hasn't been localized to the browser's locale.)
  12. *
  13. * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
  14. *
  15. * List of methods:
  16. *
  17. * string pluralForm
  18. * get(int aNum, string aWords)
  19. *
  20. * int numForms
  21. * numForms()
  22. *
  23. * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
  24. * makeGetter(int aRuleNum)
  25. * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
  26. */
  27. const Cc = Components.classes;
  28. const Ci = Components.interfaces;
  29. const kIntlProperties = "chrome://global/locale/intl.properties";
  30. // These are the available plural functions that give the appropriate index
  31. // based on the plural rule number specified. The first element is the number
  32. // of plural forms and the second is the function to figure out the index.
  33. var gFunctions = [
  34. // 0: Chinese
  35. [1, (n) => 0],
  36. // 1: English
  37. [2, (n) => n!=1?1:0],
  38. // 2: French
  39. [2, (n) => n>1?1:0],
  40. // 3: Latvian
  41. [3, (n) => n%10==1&&n%100!=11?1:n!=0?2:0],
  42. // 4: Scottish Gaelic
  43. [4, (n) => n==1||n==11?0:n==2||n==12?1:n>0&&n<20?2:3],
  44. // 5: Romanian
  45. [3, (n) => n==1?0:n==0||n%100>0&&n%100<20?1:2],
  46. // 6: Lithuanian
  47. [3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
  48. // 7: Russian
  49. [3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
  50. // 8: Slovak
  51. [3, (n) => n==1?0:n>=2&&n<=4?1:2],
  52. // 9: Polish
  53. [3, (n) => n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
  54. // 10: Slovenian
  55. [4, (n) => n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
  56. // 11: Irish Gaeilge
  57. [5, (n) => n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
  58. // 12: Arabic
  59. [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],
  60. // 13: Maltese
  61. [4, (n) => n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
  62. // 14: Macedonian
  63. [3, (n) => n%10==1?0:n%10==2?1:2],
  64. // 15: Icelandic
  65. [2, (n) => n%10==1&&n%100!=11?0:1],
  66. // 16: Breton
  67. [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],
  68. ];
  69. this.PluralForm = {
  70. /**
  71. * Get the correct plural form of a word based on the number
  72. *
  73. * @param aNum
  74. * The number to decide which plural form to use
  75. * @param aWords
  76. * A semi-colon (;) separated string of words to pick the plural form
  77. * @return The appropriate plural form of the word
  78. */
  79. get get()
  80. {
  81. // This method will lazily load to avoid perf when it is first needed and
  82. // creates getPluralForm function. The function it creates is based on the
  83. // value of pluralRule specified in the intl stringbundle.
  84. // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
  85. // Delete the getters to be overwritten
  86. delete PluralForm.numForms;
  87. delete PluralForm.get;
  88. // Make the plural form get function and set it as the default get
  89. [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(PluralForm.ruleNum);
  90. return PluralForm.get;
  91. },
  92. /**
  93. * Create a pair of plural form functions for the given plural rule number.
  94. *
  95. * @param aRuleNum
  96. * The plural rule number to create functions
  97. * @return A pair: [function that gets the right plural form,
  98. * function that returns the number of plural forms]
  99. */
  100. makeGetter: function(aRuleNum)
  101. {
  102. // Default to "all plural" if the value is out of bounds or invalid
  103. if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
  104. log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
  105. aRuleNum = 0;
  106. }
  107. // Get the desired pluralRule function
  108. let [numForms, pluralFunc] = gFunctions[aRuleNum];
  109. // Return functions that give 1) the number of forms and 2) gets the right
  110. // plural form
  111. return [function(aNum, aWords) {
  112. // Figure out which index to use for the semi-colon separated words
  113. let index = pluralFunc(aNum ? Number(aNum) : 0);
  114. let words = aWords ? aWords.split(/;/) : [""];
  115. // Explicitly check bounds to avoid strict warnings
  116. let ret = index < words.length ? words[index] : undefined;
  117. // Check for array out of bounds or empty strings
  118. if ((ret == undefined) || (ret == "")) {
  119. // Report the caller to help figure out who is causing badness
  120. let caller = Components.stack.caller ? Components.stack.caller.name : "top";
  121. // Display a message in the error console
  122. log(["Index #", index, " of '", aWords, "' for value ", aNum,
  123. " is invalid -- plural rule #", aRuleNum, "; called by ", caller]);
  124. // Default to the first entry (which might be empty, but not undefined)
  125. ret = words[0];
  126. }
  127. return ret;
  128. }, () => numForms];
  129. },
  130. /**
  131. * Get the number of forms for the current plural rule
  132. *
  133. * @return The number of forms
  134. */
  135. get numForms()
  136. {
  137. // We lazily load numForms, so trigger the init logic with get()
  138. PluralForm.get();
  139. return PluralForm.numForms;
  140. },
  141. /**
  142. * Get the plural rule number from the intl stringbundle
  143. *
  144. * @return The plural rule number
  145. */
  146. get ruleNum()
  147. {
  148. return Number(Cc["@mozilla.org/intl/stringbundle;1"].
  149. getService(Ci.nsIStringBundleService).createBundle(kIntlProperties).
  150. GetStringFromName("pluralRule"));
  151. }
  152. };
  153. /**
  154. * Private helper function to log errors to the error console and command line
  155. *
  156. * @param aMsg
  157. * Error message to log or an array of strings to concat
  158. */
  159. function log(aMsg)
  160. {
  161. let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
  162. Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
  163. logStringMessage(msg);
  164. dump(msg + "\n");
  165. }