You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1034 lines
29 KiB

7 years ago
  1. /**
  2. * Copyright (c) 2011-2013 Fabien Cazenave, Mozilla.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. /*
  23. Additional modifications for PDF.js project:
  24. - Disables language initialization on page loading;
  25. - Removes consoleWarn and consoleLog and use console.log/warn directly.
  26. - Removes window._ assignment.
  27. - Remove compatibility code for OldIE.
  28. */
  29. /*jshint browser: true, devel: true, es5: true, globalstrict: true */
  30. 'use strict';
  31. document.webL10n = (function(window, document, undefined) {
  32. var gL10nData = {};
  33. var gTextData = '';
  34. var gTextProp = 'textContent';
  35. var gLanguage = '';
  36. var gMacros = {};
  37. var gReadyState = 'loading';
  38. /**
  39. * Synchronously loading l10n resources significantly minimizes flickering
  40. * from displaying the app with non-localized strings and then updating the
  41. * strings. Although this will block all script execution on this page, we
  42. * expect that the l10n resources are available locally on flash-storage.
  43. *
  44. * As synchronous XHR is generally considered as a bad idea, we're still
  45. * loading l10n resources asynchronously -- but we keep this in a setting,
  46. * just in case... and applications using this library should hide their
  47. * content until the `localized' event happens.
  48. */
  49. var gAsyncResourceLoading = true; // read-only
  50. /**
  51. * DOM helpers for the so-called "HTML API".
  52. *
  53. * These functions are written for modern browsers. For old versions of IE,
  54. * they're overridden in the 'startup' section at the end of this file.
  55. */
  56. function getL10nResourceLinks() {
  57. return document.querySelectorAll('link[type="application/l10n"]');
  58. }
  59. function getL10nDictionary() {
  60. var script = document.querySelector('script[type="application/l10n"]');
  61. // TODO: support multiple and external JSON dictionaries
  62. return script ? JSON.parse(script.innerHTML) : null;
  63. }
  64. function getTranslatableChildren(element) {
  65. return element ? element.querySelectorAll('*[data-l10n-id]') : [];
  66. }
  67. function getL10nAttributes(element) {
  68. if (!element)
  69. return {};
  70. var l10nId = element.getAttribute('data-l10n-id');
  71. var l10nArgs = element.getAttribute('data-l10n-args');
  72. var args = {};
  73. if (l10nArgs) {
  74. try {
  75. args = JSON.parse(l10nArgs);
  76. } catch (e) {
  77. console.warn('could not parse arguments for #' + l10nId);
  78. }
  79. }
  80. return { id: l10nId, args: args };
  81. }
  82. function fireL10nReadyEvent(lang) {
  83. var evtObject = document.createEvent('Event');
  84. evtObject.initEvent('localized', true, false);
  85. evtObject.language = lang;
  86. document.dispatchEvent(evtObject);
  87. }
  88. function xhrLoadText(url, onSuccess, onFailure) {
  89. onSuccess = onSuccess || function _onSuccess(data) {};
  90. onFailure = onFailure || function _onFailure() {};
  91. var xhr = new XMLHttpRequest();
  92. xhr.open('GET', url, gAsyncResourceLoading);
  93. if (xhr.overrideMimeType) {
  94. xhr.overrideMimeType('text/plain; charset=utf-8');
  95. }
  96. xhr.onreadystatechange = function() {
  97. if (xhr.readyState == 4) {
  98. if (xhr.status == 200 || xhr.status === 0) {
  99. onSuccess(xhr.responseText);
  100. } else {
  101. onFailure();
  102. }
  103. }
  104. };
  105. xhr.onerror = onFailure;
  106. xhr.ontimeout = onFailure;
  107. // in Firefox OS with the app:// protocol, trying to XHR a non-existing
  108. // URL will raise an exception here -- hence this ugly try...catch.
  109. try {
  110. xhr.send(null);
  111. } catch (e) {
  112. onFailure();
  113. }
  114. }
  115. /**
  116. * l10n resource parser:
  117. * - reads (async XHR) the l10n resource matching `lang';
  118. * - imports linked resources (synchronously) when specified;
  119. * - parses the text data (fills `gL10nData' and `gTextData');
  120. * - triggers success/failure callbacks when done.
  121. *
  122. * @param {string} href
  123. * URL of the l10n resource to parse.
  124. *
  125. * @param {string} lang
  126. * locale (language) to parse. Must be a lowercase string.
  127. *
  128. * @param {Function} successCallback
  129. * triggered when the l10n resource has been successully parsed.
  130. *
  131. * @param {Function} failureCallback
  132. * triggered when the an error has occured.
  133. *
  134. * @return {void}
  135. * uses the following global variables: gL10nData, gTextData, gTextProp.
  136. */
  137. function parseResource(href, lang, successCallback, failureCallback) {
  138. var baseURL = href.replace(/[^\/]*$/, '') || './';
  139. // handle escaped characters (backslashes) in a string
  140. function evalString(text) {
  141. if (text.lastIndexOf('\\') < 0)
  142. return text;
  143. return text.replace(/\\\\/g, '\\')
  144. .replace(/\\n/g, '\n')
  145. .replace(/\\r/g, '\r')
  146. .replace(/\\t/g, '\t')
  147. .replace(/\\b/g, '\b')
  148. .replace(/\\f/g, '\f')
  149. .replace(/\\{/g, '{')
  150. .replace(/\\}/g, '}')
  151. .replace(/\\"/g, '"')
  152. .replace(/\\'/g, "'");
  153. }
  154. // parse *.properties text data into an l10n dictionary
  155. // If gAsyncResourceLoading is false, then the callback will be called
  156. // synchronously. Otherwise it is called asynchronously.
  157. function parseProperties(text, parsedPropertiesCallback) {
  158. var dictionary = {};
  159. // token expressions
  160. var reBlank = /^\s*|\s*$/;
  161. var reComment = /^\s*#|^\s*$/;
  162. var reSection = /^\s*\[(.*)\]\s*$/;
  163. var reImport = /^\s*@import\s+url\((.*)\)\s*$/i;
  164. var reSplit = /^([^=\s]*)\s*=\s*(.+)$/; // TODO: escape EOLs with '\'
  165. // parse the *.properties file into an associative array
  166. function parseRawLines(rawText, extendedSyntax, parsedRawLinesCallback) {
  167. var entries = rawText.replace(reBlank, '').split(/[\r\n]+/);
  168. var currentLang = '*';
  169. var genericLang = lang.split('-', 1)[0];
  170. var skipLang = false;
  171. var match = '';
  172. function nextEntry() {
  173. // Use infinite loop instead of recursion to avoid reaching the
  174. // maximum recursion limit for content with many lines.
  175. while (true) {
  176. if (!entries.length) {
  177. parsedRawLinesCallback();
  178. return;
  179. }
  180. var line = entries.shift();
  181. // comment or blank line?
  182. if (reComment.test(line))
  183. continue;
  184. // the extended syntax supports [lang] sections and @import rules
  185. if (extendedSyntax) {
  186. match = reSection.exec(line);
  187. if (match) { // section start?
  188. // RFC 4646, section 4.4, "All comparisons MUST be performed
  189. // in a case-insensitive manner."
  190. currentLang = match[1].toLowerCase();
  191. skipLang = (currentLang !== '*') &&
  192. (currentLang !== lang) && (currentLang !== genericLang);
  193. continue;
  194. } else if (skipLang) {
  195. continue;
  196. }
  197. match = reImport.exec(line);
  198. if (match) { // @import rule?
  199. loadImport(baseURL + match[1], nextEntry);
  200. return;
  201. }
  202. }
  203. // key-value pair
  204. var tmp = line.match(reSplit);
  205. if (tmp && tmp.length == 3) {
  206. dictionary[tmp[1]] = evalString(tmp[2]);
  207. }
  208. }
  209. }
  210. nextEntry();
  211. }
  212. // import another *.properties file
  213. function loadImport(url, callback) {
  214. xhrLoadText(url, function(content) {
  215. parseRawLines(content, false, callback); // don't allow recursive imports
  216. }, function () {
  217. console.warn(url + ' not found.');
  218. callback();
  219. });
  220. }
  221. // fill the dictionary
  222. parseRawLines(text, true, function() {
  223. parsedPropertiesCallback(dictionary);
  224. });
  225. }
  226. // load and parse l10n data (warning: global variables are used here)
  227. xhrLoadText(href, function(response) {
  228. gTextData += response; // mostly for debug
  229. // parse *.properties text data into an l10n dictionary
  230. parseProperties(response, function(data) {
  231. // find attribute descriptions, if any
  232. for (var key in data) {
  233. var id, prop, index = key.lastIndexOf('.');
  234. if (index > 0) { // an attribute has been specified
  235. id = key.substring(0, index);
  236. prop = key.substr(index + 1);
  237. } else { // no attribute: assuming text content by default
  238. id = key;
  239. prop = gTextProp;
  240. }
  241. if (!gL10nData[id]) {
  242. gL10nData[id] = {};
  243. }
  244. gL10nData[id][prop] = data[key];
  245. }
  246. // trigger callback
  247. if (successCallback) {
  248. successCallback();
  249. }
  250. });
  251. }, failureCallback);
  252. }
  253. // load and parse all resources for the specified locale
  254. function loadLocale(lang, callback) {
  255. // RFC 4646, section 2.1 states that language tags have to be treated as
  256. // case-insensitive. Convert to lowercase for case-insensitive comparisons.
  257. if (lang) {
  258. lang = lang.toLowerCase();
  259. }
  260. callback = callback || function _callback() {};
  261. clear();
  262. gLanguage = lang;
  263. // check all <link type="application/l10n" href="..." /> nodes
  264. // and load the resource files
  265. var langLinks = getL10nResourceLinks();
  266. var langCount = langLinks.length;
  267. if (langCount === 0) {
  268. // we might have a pre-compiled dictionary instead
  269. var dict = getL10nDictionary();
  270. if (dict && dict.locales && dict.default_locale) {
  271. console.log('using the embedded JSON directory, early way out');
  272. gL10nData = dict.locales[lang];
  273. if (!gL10nData) {
  274. var defaultLocale = dict.default_locale.toLowerCase();
  275. for (var anyCaseLang in dict.locales) {
  276. anyCaseLang = anyCaseLang.toLowerCase();
  277. if (anyCaseLang === lang) {
  278. gL10nData = dict.locales[lang];
  279. break;
  280. } else if (anyCaseLang === defaultLocale) {
  281. gL10nData = dict.locales[defaultLocale];
  282. }
  283. }
  284. }
  285. callback();
  286. } else {
  287. console.log('no resource to load, early way out');
  288. }
  289. // early way out
  290. fireL10nReadyEvent(lang);
  291. gReadyState = 'complete';
  292. return;
  293. }
  294. // start the callback when all resources are loaded
  295. var onResourceLoaded = null;
  296. var gResourceCount = 0;
  297. onResourceLoaded = function() {
  298. gResourceCount++;
  299. if (gResourceCount >= langCount) {
  300. callback();
  301. fireL10nReadyEvent(lang);
  302. gReadyState = 'complete';
  303. }
  304. };
  305. // load all resource files
  306. function L10nResourceLink(link) {
  307. var href = link.href;
  308. // Note: If |gAsyncResourceLoading| is false, then the following callbacks
  309. // are synchronously called.
  310. this.load = function(lang, callback) {
  311. parseResource(href, lang, callback, function() {
  312. console.warn(href + ' not found.');
  313. // lang not found, used default resource instead
  314. console.warn('"' + lang + '" resource not found');
  315. gLanguage = '';
  316. // Resource not loaded, but we still need to call the callback.
  317. callback();
  318. });
  319. };
  320. }
  321. for (var i = 0; i < langCount; i++) {
  322. var resource = new L10nResourceLink(langLinks[i]);
  323. resource.load(lang, onResourceLoaded);
  324. }
  325. }
  326. // clear all l10n data
  327. function clear() {
  328. gL10nData = {};
  329. gTextData = '';
  330. gLanguage = '';
  331. // TODO: clear all non predefined macros.
  332. // There's no such macro /yet/ but we're planning to have some...
  333. }
  334. /**
  335. * Get rules for plural forms (shared with JetPack), see:
  336. * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
  337. * https://github.com/mozilla/addon-sdk/blob/master/python-lib/plural-rules-generator.p
  338. *
  339. * @param {string} lang
  340. * locale (language) used.
  341. *
  342. * @return {Function}
  343. * returns a function that gives the plural form name for a given integer:
  344. * var fun = getPluralRules('en');
  345. * fun(1) -> 'one'
  346. * fun(0) -> 'other'
  347. * fun(1000) -> 'other'.
  348. */
  349. function getPluralRules(lang) {
  350. var locales2rules = {
  351. 'af': 3,
  352. 'ak': 4,
  353. 'am': 4,
  354. 'ar': 1,
  355. 'asa': 3,
  356. 'az': 0,
  357. 'be': 11,
  358. 'bem': 3,
  359. 'bez': 3,
  360. 'bg': 3,
  361. 'bh': 4,
  362. 'bm': 0,
  363. 'bn': 3,
  364. 'bo': 0,
  365. 'br': 20,
  366. 'brx': 3,
  367. 'bs': 11,
  368. 'ca': 3,
  369. 'cgg': 3,
  370. 'chr': 3,
  371. 'cs': 12,
  372. 'cy': 17,
  373. 'da': 3,
  374. 'de': 3,
  375. 'dv': 3,
  376. 'dz': 0,
  377. 'ee': 3,
  378. 'el': 3,
  379. 'en': 3,
  380. 'eo': 3,
  381. 'es': 3,
  382. 'et': 3,
  383. 'eu': 3,
  384. 'fa': 0,
  385. 'ff': 5,
  386. 'fi': 3,
  387. 'fil': 4,
  388. 'fo': 3,
  389. 'fr': 5,
  390. 'fur': 3,
  391. 'fy': 3,
  392. 'ga': 8,
  393. 'gd': 24,
  394. 'gl': 3,
  395. 'gsw': 3,
  396. 'gu': 3,
  397. 'guw': 4,
  398. 'gv': 23,
  399. 'ha': 3,
  400. 'haw': 3,
  401. 'he': 2,
  402. 'hi': 4,
  403. 'hr': 11,
  404. 'hu': 0,
  405. 'id': 0,
  406. 'ig': 0,
  407. 'ii': 0,
  408. 'is': 3,
  409. 'it': 3,
  410. 'iu': 7,
  411. 'ja': 0,
  412. 'jmc': 3,
  413. 'jv': 0,
  414. 'ka': 0,
  415. 'kab': 5,
  416. 'kaj': 3,
  417. 'kcg': 3,
  418. 'kde': 0,
  419. 'kea': 0,
  420. 'kk': 3,
  421. 'kl': 3,
  422. 'km': 0,
  423. 'kn': 0,
  424. 'ko': 0,
  425. 'ksb': 3,
  426. 'ksh': 21,
  427. 'ku': 3,
  428. 'kw': 7,
  429. 'lag': 18,
  430. 'lb': 3,
  431. 'lg': 3,
  432. 'ln': 4,
  433. 'lo': 0,
  434. 'lt': 10,
  435. 'lv': 6,
  436. 'mas': 3,
  437. 'mg': 4,
  438. 'mk': 16,
  439. 'ml': 3,
  440. 'mn': 3,
  441. 'mo': 9,
  442. 'mr': 3,
  443. 'ms': 0,
  444. 'mt': 15,
  445. 'my': 0,
  446. 'nah': 3,
  447. 'naq': 7,
  448. 'nb': 3,
  449. 'nd': 3,
  450. 'ne': 3,
  451. 'nl': 3,
  452. 'nn': 3,
  453. 'no': 3,
  454. 'nr': 3,
  455. 'nso': 4,
  456. 'ny': 3,
  457. 'nyn': 3,
  458. 'om': 3,
  459. 'or': 3,
  460. 'pa': 3,
  461. 'pap': 3,
  462. 'pl': 13,
  463. 'ps': 3,
  464. 'pt': 3,
  465. 'rm': 3,
  466. 'ro': 9,
  467. 'rof': 3,
  468. 'ru': 11,
  469. 'rwk': 3,
  470. 'sah': 0,
  471. 'saq': 3,
  472. 'se': 7,
  473. 'seh': 3,
  474. 'ses': 0,
  475. 'sg': 0,
  476. 'sh': 11,
  477. 'shi': 19,
  478. 'sk': 12,
  479. 'sl': 14,
  480. 'sma': 7,
  481. 'smi': 7,
  482. 'smj': 7,
  483. 'smn': 7,
  484. 'sms': 7,
  485. 'sn': 3,
  486. 'so': 3,
  487. 'sq': 3,
  488. 'sr': 11,
  489. 'ss': 3,
  490. 'ssy': 3,
  491. 'st': 3,
  492. 'sv': 3,
  493. 'sw': 3,
  494. 'syr': 3,
  495. 'ta': 3,
  496. 'te': 3,
  497. 'teo': 3,
  498. 'th': 0,
  499. 'ti': 4,
  500. 'tig': 3,
  501. 'tk': 3,
  502. 'tl': 4,
  503. 'tn': 3,
  504. 'to': 0,
  505. 'tr': 0,
  506. 'ts': 3,
  507. 'tzm': 22,
  508. 'uk': 11,
  509. 'ur': 3,
  510. 've': 3,
  511. 'vi': 0,
  512. 'vun': 3,
  513. 'wa': 4,
  514. 'wae': 3,
  515. 'wo': 0,
  516. 'xh': 3,
  517. 'xog': 3,
  518. 'yo': 0,
  519. 'zh': 0,
  520. 'zu': 3
  521. };
  522. // utility functions for plural rules methods
  523. function isIn(n, list) {
  524. return list.indexOf(n) !== -1;
  525. }
  526. function isBetween(n, start, end) {
  527. return start <= n && n <= end;
  528. }
  529. // list of all plural rules methods:
  530. // map an integer to the plural form name to use
  531. var pluralRules = {
  532. '0': function(n) {
  533. return 'other';
  534. },
  535. '1': function(n) {
  536. if ((isBetween((n % 100), 3, 10)))
  537. return 'few';
  538. if (n === 0)
  539. return 'zero';
  540. if ((isBetween((n % 100), 11, 99)))
  541. return 'many';
  542. if (n == 2)
  543. return 'two';
  544. if (n == 1)
  545. return 'one';
  546. return 'other';
  547. },
  548. '2': function(n) {
  549. if (n !== 0 && (n % 10) === 0)
  550. return 'many';
  551. if (n == 2)
  552. return 'two';
  553. if (n == 1)
  554. return 'one';
  555. return 'other';
  556. },
  557. '3': function(n) {
  558. if (n == 1)
  559. return 'one';
  560. return 'other';
  561. },
  562. '4': function(n) {
  563. if ((isBetween(n, 0, 1)))
  564. return 'one';
  565. return 'other';
  566. },
  567. '5': function(n) {
  568. if ((isBetween(n, 0, 2)) && n != 2)
  569. return 'one';
  570. return 'other';
  571. },
  572. '6': function(n) {
  573. if (n === 0)
  574. return 'zero';
  575. if ((n % 10) == 1 && (n % 100) != 11)
  576. return 'one';
  577. return 'other';
  578. },
  579. '7': function(n) {
  580. if (n == 2)
  581. return 'two';
  582. if (n == 1)
  583. return 'one';
  584. return 'other';
  585. },
  586. '8': function(n) {
  587. if ((isBetween(n, 3, 6)))
  588. return 'few';
  589. if ((isBetween(n, 7, 10)))
  590. return 'many';
  591. if (n == 2)
  592. return 'two';
  593. if (n == 1)
  594. return 'one';
  595. return 'other';
  596. },
  597. '9': function(n) {
  598. if (n === 0 || n != 1 && (isBetween((n % 100), 1, 19)))
  599. return 'few';
  600. if (n == 1)
  601. return 'one';
  602. return 'other';
  603. },
  604. '10': function(n) {
  605. if ((isBetween((n % 10), 2, 9)) && !(isBetween((n % 100), 11, 19)))
  606. return 'few';
  607. if ((n % 10) == 1 && !(isBetween((n % 100), 11, 19)))
  608. return 'one';
  609. return 'other';
  610. },
  611. '11': function(n) {
  612. if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14)))
  613. return 'few';
  614. if ((n % 10) === 0 ||
  615. (isBetween((n % 10), 5, 9)) ||
  616. (isBetween((n % 100), 11, 14)))
  617. return 'many';
  618. if ((n % 10) == 1 && (n % 100) != 11)
  619. return 'one';
  620. return 'other';
  621. },
  622. '12': function(n) {
  623. if ((isBetween(n, 2, 4)))
  624. return 'few';
  625. if (n == 1)
  626. return 'one';
  627. return 'other';
  628. },
  629. '13': function(n) {
  630. if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14)))
  631. return 'few';
  632. if (n != 1 && (isBetween((n % 10), 0, 1)) ||
  633. (isBetween((n % 10), 5, 9)) ||
  634. (isBetween((n % 100), 12, 14)))
  635. return 'many';
  636. if (n == 1)
  637. return 'one';
  638. return 'other';
  639. },
  640. '14': function(n) {
  641. if ((isBetween((n % 100), 3, 4)))
  642. return 'few';
  643. if ((n % 100) == 2)
  644. return 'two';
  645. if ((n % 100) == 1)
  646. return 'one';
  647. return 'other';
  648. },
  649. '15': function(n) {
  650. if (n === 0 || (isBetween((n % 100), 2, 10)))
  651. return 'few';
  652. if ((isBetween((n % 100), 11, 19)))
  653. return 'many';
  654. if (n == 1)
  655. return 'one';
  656. return 'other';
  657. },
  658. '16': function(n) {
  659. if ((n % 10) == 1 && n != 11)
  660. return 'one';
  661. return 'other';
  662. },
  663. '17': function(n) {
  664. if (n == 3)
  665. return 'few';
  666. if (n === 0)
  667. return 'zero';
  668. if (n == 6)
  669. return 'many';
  670. if (n == 2)
  671. return 'two';
  672. if (n == 1)
  673. return 'one';
  674. return 'other';
  675. },
  676. '18': function(n) {
  677. if (n === 0)
  678. return 'zero';
  679. if ((isBetween(n, 0, 2)) && n !== 0 && n != 2)
  680. return 'one';
  681. return 'other';
  682. },
  683. '19': function(n) {
  684. if ((isBetween(n, 2, 10)))
  685. return 'few';
  686. if ((isBetween(n, 0, 1)))
  687. return 'one';
  688. return 'other';
  689. },
  690. '20': function(n) {
  691. if ((isBetween((n % 10), 3, 4) || ((n % 10) == 9)) && !(
  692. isBetween((n % 100), 10, 19) ||
  693. isBetween((n % 100), 70, 79) ||
  694. isBetween((n % 100), 90, 99)
  695. ))
  696. return 'few';
  697. if ((n % 1000000) === 0 && n !== 0)
  698. return 'many';
  699. if ((n % 10) == 2 && !isIn((n % 100), [12, 72, 92]))
  700. return 'two';
  701. if ((n % 10) == 1 && !isIn((n % 100), [11, 71, 91]))
  702. return 'one';
  703. return 'other';
  704. },
  705. '21': function(n) {
  706. if (n === 0)
  707. return 'zero';
  708. if (n == 1)
  709. return 'one';
  710. return 'other';
  711. },
  712. '22': function(n) {
  713. if ((isBetween(n, 0, 1)) || (isBetween(n, 11, 99)))
  714. return 'one';
  715. return 'other';
  716. },
  717. '23': function(n) {
  718. if ((isBetween((n % 10), 1, 2)) || (n % 20) === 0)
  719. return 'one';
  720. return 'other';
  721. },
  722. '24': function(n) {
  723. if ((isBetween(n, 3, 10) || isBetween(n, 13, 19)))
  724. return 'few';
  725. if (isIn(n, [2, 12]))
  726. return 'two';
  727. if (isIn(n, [1, 11]))
  728. return 'one';
  729. return 'other';
  730. }
  731. };
  732. // return a function that gives the plural form name for a given integer
  733. var index = locales2rules[lang.replace(/-.*$/, '')];
  734. if (!(index in pluralRules)) {
  735. console.warn('plural form unknown for [' + lang + ']');
  736. return function() { return 'other'; };
  737. }
  738. return pluralRules[index];
  739. }
  740. // pre-defined 'plural' macro
  741. gMacros.plural = function(str, param, key, prop) {
  742. var n = parseFloat(param);
  743. if (isNaN(n))
  744. return str;
  745. // TODO: support other properties (l20n still doesn't...)
  746. if (prop != gTextProp)
  747. return str;
  748. // initialize _pluralRules
  749. if (!gMacros._pluralRules) {
  750. gMacros._pluralRules = getPluralRules(gLanguage);
  751. }
  752. var index = '[' + gMacros._pluralRules(n) + ']';
  753. // try to find a [zero|one|two] key if it's defined
  754. if (n === 0 && (key + '[zero]') in gL10nData) {
  755. str = gL10nData[key + '[zero]'][prop];
  756. } else if (n == 1 && (key + '[one]') in gL10nData) {
  757. str = gL10nData[key + '[one]'][prop];
  758. } else if (n == 2 && (key + '[two]') in gL10nData) {
  759. str = gL10nData[key + '[two]'][prop];
  760. } else if ((key + index) in gL10nData) {
  761. str = gL10nData[key + index][prop];
  762. } else if ((key + '[other]') in gL10nData) {
  763. str = gL10nData[key + '[other]'][prop];
  764. }
  765. return str;
  766. };
  767. /**
  768. * l10n dictionary functions
  769. */
  770. // fetch an l10n object, warn if not found, apply `args' if possible
  771. function getL10nData(key, args, fallback) {
  772. var data = gL10nData[key];
  773. if (!data) {
  774. console.warn('#' + key + ' is undefined.');
  775. if (!fallback) {
  776. return null;
  777. }
  778. data = fallback;
  779. }
  780. /** This is where l10n expressions should be processed.
  781. * The plan is to support C-style expressions from the l20n project;
  782. * until then, only two kinds of simple expressions are supported:
  783. * {[ index ]} and {{ arguments }}.
  784. */
  785. var rv = {};
  786. for (var prop in data) {
  787. var str = data[prop];
  788. str = substIndexes(str, args, key, prop);
  789. str = substArguments(str, args, key);
  790. rv[prop] = str;
  791. }
  792. return rv;
  793. }
  794. // replace {[macros]} with their values
  795. function substIndexes(str, args, key, prop) {
  796. var reIndex = /\{\[\s*([a-zA-Z]+)\(([a-zA-Z]+)\)\s*\]\}/;
  797. var reMatch = reIndex.exec(str);
  798. if (!reMatch || !reMatch.length)
  799. return str;
  800. // an index/macro has been found
  801. // Note: at the moment, only one parameter is supported
  802. var macroName = reMatch[1];
  803. var paramName = reMatch[2];
  804. var param;
  805. if (args && paramName in args) {
  806. param = args[paramName];
  807. } else if (paramName in gL10nData) {
  808. param = gL10nData[paramName];
  809. }
  810. // there's no macro parser yet: it has to be defined in gMacros
  811. if (macroName in gMacros) {
  812. var macro = gMacros[macroName];
  813. str = macro(str, param, key, prop);
  814. }
  815. return str;
  816. }
  817. // replace {{arguments}} with their values
  818. function substArguments(str, args, key) {
  819. var reArgs = /\{\{\s*(.+?)\s*\}\}/g;
  820. return str.replace(reArgs, function(matched_text, arg) {
  821. if (args && arg in args) {
  822. return args[arg];
  823. }
  824. if (arg in gL10nData) {
  825. return gL10nData[arg];
  826. }
  827. console.log('argument {{' + arg + '}} for #' + key + ' is undefined.');
  828. return matched_text;
  829. });
  830. }
  831. // translate an HTML element
  832. function translateElement(element) {
  833. var l10n = getL10nAttributes(element);
  834. if (!l10n.id)
  835. return;
  836. // get the related l10n object
  837. var data = getL10nData(l10n.id, l10n.args);
  838. if (!data) {
  839. console.warn('#' + l10n.id + ' is undefined.');
  840. return;
  841. }
  842. // translate element (TODO: security checks?)
  843. if (data[gTextProp]) { // XXX
  844. if (getChildElementCount(element) === 0) {
  845. element[gTextProp] = data[gTextProp];
  846. } else {
  847. // this element has element children: replace the content of the first
  848. // (non-empty) child textNode and clear other child textNodes
  849. var children = element.childNodes;
  850. var found = false;
  851. for (var i = 0, l = children.length; i < l; i++) {
  852. if (children[i].nodeType === 3 && /\S/.test(children[i].nodeValue)) {
  853. if (found) {
  854. children[i].nodeValue = '';
  855. } else {
  856. children[i].nodeValue = data[gTextProp];
  857. found = true;
  858. }
  859. }
  860. }
  861. // if no (non-empty) textNode is found, insert a textNode before the
  862. // first element child.
  863. if (!found) {
  864. var textNode = document.createTextNode(data[gTextProp]);
  865. element.insertBefore(textNode, element.firstChild);
  866. }
  867. }
  868. delete data[gTextProp];
  869. }
  870. for (var k in data) {
  871. element[k] = data[k];
  872. }
  873. }
  874. // webkit browsers don't currently support 'children' on SVG elements...
  875. function getChildElementCount(element) {
  876. if (element.children) {
  877. return element.children.length;
  878. }
  879. if (typeof element.childElementCount !== 'undefined') {
  880. return element.childElementCount;
  881. }
  882. var count = 0;
  883. for (var i = 0; i < element.childNodes.length; i++) {
  884. count += element.nodeType === 1 ? 1 : 0;
  885. }
  886. return count;
  887. }
  888. // translate an HTML subtree
  889. function translateFragment(element) {
  890. element = element || document.documentElement;
  891. // check all translatable children (= w/ a `data-l10n-id' attribute)
  892. var children = getTranslatableChildren(element);
  893. var elementCount = children.length;
  894. for (var i = 0; i < elementCount; i++) {
  895. translateElement(children[i]);
  896. }
  897. // translate element itself if necessary
  898. translateElement(element);
  899. }
  900. return {
  901. // get a localized string
  902. get: function(key, args, fallbackString) {
  903. var index = key.lastIndexOf('.');
  904. var prop = gTextProp;
  905. if (index > 0) { // An attribute has been specified
  906. prop = key.substr(index + 1);
  907. key = key.substring(0, index);
  908. }
  909. var fallback;
  910. if (fallbackString) {
  911. fallback = {};
  912. fallback[prop] = fallbackString;
  913. }
  914. var data = getL10nData(key, args, fallback);
  915. if (data && prop in data) {
  916. return data[prop];
  917. }
  918. return '{{' + key + '}}';
  919. },
  920. // debug
  921. getData: function() { return gL10nData; },
  922. getText: function() { return gTextData; },
  923. // get|set the document language
  924. getLanguage: function() { return gLanguage; },
  925. setLanguage: function(lang, callback) {
  926. loadLocale(lang, function() {
  927. if (callback)
  928. callback();
  929. translateFragment();
  930. });
  931. },
  932. // get the direction (ltr|rtl) of the current language
  933. getDirection: function() {
  934. // http://www.w3.org/International/questions/qa-scripts
  935. // Arabic, Hebrew, Farsi, Pashto, Urdu
  936. var rtlList = ['ar', 'he', 'fa', 'ps', 'ur'];
  937. var shortCode = gLanguage.split('-', 1)[0];
  938. return (rtlList.indexOf(shortCode) >= 0) ? 'rtl' : 'ltr';
  939. },
  940. // translate an element or document fragment
  941. translate: translateFragment,
  942. // this can be used to prevent race conditions
  943. getReadyState: function() { return gReadyState; },
  944. ready: function(callback) {
  945. if (!callback) {
  946. return;
  947. } else if (gReadyState == 'complete' || gReadyState == 'interactive') {
  948. window.setTimeout(function() {
  949. callback();
  950. });
  951. } else if (document.addEventListener) {
  952. document.addEventListener('localized', function once() {
  953. document.removeEventListener('localized', once);
  954. callback();
  955. });
  956. }
  957. }
  958. };
  959. }) (window, document);