loader.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "use strict";
  5. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { Cc, Ci } = require("chrome");
  9. const { getPreferedLocales, findClosestLocale } = require("./locale");
  10. const { readURI } = require("../net/url");
  11. const { resolve } = require("../core/promise");
  12. function parseJsonURI(uri) {
  13. return readURI(uri).
  14. then(JSON.parse).
  15. then(null, function (error) {
  16. throw Error("Failed to parse locale file:\n" + uri + "\n" + error);
  17. });
  18. }
  19. // Returns the array stored in `locales.json` manifest that list available
  20. // locales files
  21. function getAvailableLocales(rootURI) {
  22. let uri = rootURI + "locales.json";
  23. return parseJsonURI(uri).then(function (manifest) {
  24. return "locales" in manifest &&
  25. Array.isArray(manifest.locales) ?
  26. manifest.locales : [];
  27. });
  28. }
  29. // Returns URI of the best locales file to use from the XPI
  30. function getBestLocale(rootURI) {
  31. // Read localization manifest file that contains list of available languages
  32. return getAvailableLocales(rootURI).then(function (availableLocales) {
  33. // Retrieve list of prefered locales to use
  34. let preferedLocales = getPreferedLocales();
  35. // Compute the most preferable locale to use by using these two lists
  36. return findClosestLocale(availableLocales, preferedLocales);
  37. });
  38. }
  39. /**
  40. * Read localization files and returns a promise of data to put in `@l10n/data`
  41. * pseudo module, in order to allow l10n/core to fetch it.
  42. */
  43. exports.load = function load(rootURI) {
  44. // First, search for a locale file:
  45. return getBestLocale(rootURI).then(function (bestMatchingLocale) {
  46. // It may be null if the addon doesn't have any locale file
  47. if (!bestMatchingLocale)
  48. return resolve(null);
  49. let localeURI = rootURI + "locale/" + bestMatchingLocale + ".json";
  50. // Locale files only contains one big JSON object that is used as
  51. // an hashtable of: "key to translate" => "translated key"
  52. // TODO: We are likely to change this in order to be able to overload
  53. // a specific key translation. For a specific package, module or line?
  54. return parseJsonURI(localeURI).then(function (json) {
  55. return {
  56. hash: json,
  57. bestMatchingLocale: bestMatchingLocale
  58. };
  59. });
  60. });
  61. }