plural-rules.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 file is automatically generated with /python-lib/plural-rules-generator.py
  5. // Fetching data from: http://unicode.org/repos/cldr/trunk/common/supplemental/plurals.xml
  6. // Mapping of short locale name == to == > rule index in following list
  7. module.metadata = {
  8. "stability": "unstable"
  9. };
  10. const LOCALES_TO_RULES = {
  11. "af": 3,
  12. "ak": 4,
  13. "am": 4,
  14. "ar": 1,
  15. "asa": 3,
  16. "az": 0,
  17. "be": 11,
  18. "bem": 3,
  19. "bez": 3,
  20. "bg": 3,
  21. "bh": 4,
  22. "bm": 0,
  23. "bn": 3,
  24. "bo": 0,
  25. "br": 20,
  26. "brx": 3,
  27. "bs": 11,
  28. "ca": 3,
  29. "cgg": 3,
  30. "chr": 3,
  31. "cs": 12,
  32. "cy": 17,
  33. "da": 3,
  34. "de": 3,
  35. "dv": 3,
  36. "dz": 0,
  37. "ee": 3,
  38. "el": 3,
  39. "en": 3,
  40. "eo": 3,
  41. "es": 3,
  42. "et": 3,
  43. "eu": 3,
  44. "fa": 0,
  45. "ff": 5,
  46. "fi": 3,
  47. "fil": 4,
  48. "fo": 3,
  49. "fr": 5,
  50. "fur": 3,
  51. "fy": 3,
  52. "ga": 8,
  53. "gd": 24,
  54. "gl": 3,
  55. "gsw": 3,
  56. "gu": 3,
  57. "guw": 4,
  58. "gv": 23,
  59. "ha": 3,
  60. "haw": 3,
  61. "he": 2,
  62. "hi": 4,
  63. "hr": 11,
  64. "hu": 0,
  65. "id": 0,
  66. "ig": 0,
  67. "ii": 0,
  68. "is": 3,
  69. "it": 3,
  70. "iu": 7,
  71. "ja": 0,
  72. "jmc": 3,
  73. "jv": 0,
  74. "ka": 0,
  75. "kab": 5,
  76. "kaj": 3,
  77. "kcg": 3,
  78. "kde": 0,
  79. "kea": 0,
  80. "kk": 3,
  81. "kl": 3,
  82. "km": 0,
  83. "kn": 0,
  84. "ko": 0,
  85. "ksb": 3,
  86. "ksh": 21,
  87. "ku": 3,
  88. "kw": 7,
  89. "lag": 18,
  90. "lb": 3,
  91. "lg": 3,
  92. "ln": 4,
  93. "lo": 0,
  94. "lt": 10,
  95. "lv": 6,
  96. "mas": 3,
  97. "mg": 4,
  98. "mk": 16,
  99. "ml": 3,
  100. "mn": 3,
  101. "mo": 9,
  102. "mr": 3,
  103. "ms": 0,
  104. "mt": 15,
  105. "my": 0,
  106. "nah": 3,
  107. "naq": 7,
  108. "nb": 3,
  109. "nd": 3,
  110. "ne": 3,
  111. "nl": 3,
  112. "nn": 3,
  113. "no": 3,
  114. "nr": 3,
  115. "nso": 4,
  116. "ny": 3,
  117. "nyn": 3,
  118. "om": 3,
  119. "or": 3,
  120. "pa": 3,
  121. "pap": 3,
  122. "pl": 13,
  123. "ps": 3,
  124. "pt": 3,
  125. "rm": 3,
  126. "ro": 9,
  127. "rof": 3,
  128. "ru": 11,
  129. "rwk": 3,
  130. "sah": 0,
  131. "saq": 3,
  132. "se": 7,
  133. "seh": 3,
  134. "ses": 0,
  135. "sg": 0,
  136. "sh": 11,
  137. "shi": 19,
  138. "sk": 12,
  139. "sl": 14,
  140. "sma": 7,
  141. "smi": 7,
  142. "smj": 7,
  143. "smn": 7,
  144. "sms": 7,
  145. "sn": 3,
  146. "so": 3,
  147. "sq": 3,
  148. "sr": 11,
  149. "ss": 3,
  150. "ssy": 3,
  151. "st": 3,
  152. "sv": 3,
  153. "sw": 3,
  154. "syr": 3,
  155. "ta": 3,
  156. "te": 3,
  157. "teo": 3,
  158. "th": 0,
  159. "ti": 4,
  160. "tig": 3,
  161. "tk": 3,
  162. "tl": 4,
  163. "tn": 3,
  164. "to": 0,
  165. "tr": 0,
  166. "ts": 3,
  167. "tzm": 22,
  168. "uk": 11,
  169. "ur": 3,
  170. "ve": 3,
  171. "vi": 0,
  172. "vun": 3,
  173. "wa": 4,
  174. "wae": 3,
  175. "wo": 0,
  176. "xh": 3,
  177. "xog": 3,
  178. "yo": 0,
  179. "zh": 0,
  180. "zu": 3
  181. };
  182. // Utility functions for plural rules methods
  183. function isIn(n, list) list.indexOf(n) !== -1;
  184. function isBetween(n, start, end) start <= n && n <= end;
  185. // List of all plural rules methods, that maps an integer to the plural form name to use
  186. const RULES = {
  187. "0": function (n) {
  188. return "other"
  189. },
  190. "1": function (n) {
  191. if ((isBetween((n % 100), 3, 10)))
  192. return "few";
  193. if (n == 0)
  194. return "zero";
  195. if ((isBetween((n % 100), 11, 99)))
  196. return "many";
  197. if (n == 2)
  198. return "two";
  199. if (n == 1)
  200. return "one";
  201. return "other"
  202. },
  203. "2": function (n) {
  204. if (n != 0 && (n % 10) == 0)
  205. return "many";
  206. if (n == 2)
  207. return "two";
  208. if (n == 1)
  209. return "one";
  210. return "other"
  211. },
  212. "3": function (n) {
  213. if (n == 1)
  214. return "one";
  215. return "other"
  216. },
  217. "4": function (n) {
  218. if ((isBetween(n, 0, 1)))
  219. return "one";
  220. return "other"
  221. },
  222. "5": function (n) {
  223. if ((isBetween(n, 0, 2)) && n != 2)
  224. return "one";
  225. return "other"
  226. },
  227. "6": function (n) {
  228. if (n == 0)
  229. return "zero";
  230. if ((n % 10) == 1 && (n % 100) != 11)
  231. return "one";
  232. return "other"
  233. },
  234. "7": function (n) {
  235. if (n == 2)
  236. return "two";
  237. if (n == 1)
  238. return "one";
  239. return "other"
  240. },
  241. "8": function (n) {
  242. if ((isBetween(n, 3, 6)))
  243. return "few";
  244. if ((isBetween(n, 7, 10)))
  245. return "many";
  246. if (n == 2)
  247. return "two";
  248. if (n == 1)
  249. return "one";
  250. return "other"
  251. },
  252. "9": function (n) {
  253. if (n == 0 || n != 1 && (isBetween((n % 100), 1, 19)))
  254. return "few";
  255. if (n == 1)
  256. return "one";
  257. return "other"
  258. },
  259. "10": function (n) {
  260. if ((isBetween((n % 10), 2, 9)) && !(isBetween((n % 100), 11, 19)))
  261. return "few";
  262. if ((n % 10) == 1 && !(isBetween((n % 100), 11, 19)))
  263. return "one";
  264. return "other"
  265. },
  266. "11": function (n) {
  267. if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14)))
  268. return "few";
  269. if ((n % 10) == 0 || (isBetween((n % 10), 5, 9)) || (isBetween((n % 100), 11, 14)))
  270. return "many";
  271. if ((n % 10) == 1 && (n % 100) != 11)
  272. return "one";
  273. return "other"
  274. },
  275. "12": function (n) {
  276. if ((isBetween(n, 2, 4)))
  277. return "few";
  278. if (n == 1)
  279. return "one";
  280. return "other"
  281. },
  282. "13": function (n) {
  283. if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14)))
  284. return "few";
  285. if (n != 1 && (isBetween((n % 10), 0, 1)) || (isBetween((n % 10), 5, 9)) || (isBetween((n % 100), 12, 14)))
  286. return "many";
  287. if (n == 1)
  288. return "one";
  289. return "other"
  290. },
  291. "14": function (n) {
  292. if ((isBetween((n % 100), 3, 4)))
  293. return "few";
  294. if ((n % 100) == 2)
  295. return "two";
  296. if ((n % 100) == 1)
  297. return "one";
  298. return "other"
  299. },
  300. "15": function (n) {
  301. if (n == 0 || (isBetween((n % 100), 2, 10)))
  302. return "few";
  303. if ((isBetween((n % 100), 11, 19)))
  304. return "many";
  305. if (n == 1)
  306. return "one";
  307. return "other"
  308. },
  309. "16": function (n) {
  310. if ((n % 10) == 1 && n != 11)
  311. return "one";
  312. return "other"
  313. },
  314. "17": function (n) {
  315. if (n == 3)
  316. return "few";
  317. if (n == 0)
  318. return "zero";
  319. if (n == 6)
  320. return "many";
  321. if (n == 2)
  322. return "two";
  323. if (n == 1)
  324. return "one";
  325. return "other"
  326. },
  327. "18": function (n) {
  328. if (n == 0)
  329. return "zero";
  330. if ((isBetween(n, 0, 2)) && n != 0 && n != 2)
  331. return "one";
  332. return "other"
  333. },
  334. "19": function (n) {
  335. if ((isBetween(n, 2, 10)))
  336. return "few";
  337. if ((isBetween(n, 0, 1)))
  338. return "one";
  339. return "other"
  340. },
  341. "20": function (n) {
  342. if ((isBetween((n % 10), 3, 4) || ((n % 10) == 9)) && !(isBetween((n % 100), 10, 19) || isBetween((n % 100), 70, 79) || isBetween((n % 100), 90, 99)))
  343. return "few";
  344. if ((n % 1000000) == 0 && n != 0)
  345. return "many";
  346. if ((n % 10) == 2 && !isIn((n % 100), [12, 72, 92]))
  347. return "two";
  348. if ((n % 10) == 1 && !isIn((n % 100), [11, 71, 91]))
  349. return "one";
  350. return "other"
  351. },
  352. "21": function (n) {
  353. if (n == 0)
  354. return "zero";
  355. if (n == 1)
  356. return "one";
  357. return "other"
  358. },
  359. "22": function (n) {
  360. if ((isBetween(n, 0, 1)) || (isBetween(n, 11, 99)))
  361. return "one";
  362. return "other"
  363. },
  364. "23": function (n) {
  365. if ((isBetween((n % 10), 1, 2)) || (n % 20) == 0)
  366. return "one";
  367. return "other"
  368. },
  369. "24": function (n) {
  370. if ((isBetween(n, 3, 10) || isBetween(n, 13, 19)))
  371. return "few";
  372. if (isIn(n, [2, 12]))
  373. return "two";
  374. if (isIn(n, [1, 11]))
  375. return "one";
  376. return "other"
  377. },
  378. };
  379. /**
  380. * Return a function that gives the plural form name for a given integer
  381. * for the specified `locale`
  382. * let fun = getRulesForLocale('en');
  383. * fun(1) -> 'one'
  384. * fun(0) -> 'other'
  385. * fun(1000) -> 'other'
  386. */
  387. exports.getRulesForLocale = function getRulesForLocale(locale) {
  388. let index = LOCALES_TO_RULES[locale];
  389. if (!(index in RULES)) {
  390. console.warn('Plural form unknown for locale "' + locale + '"');
  391. return function () { return "other"; };
  392. }
  393. return RULES[index];
  394. }