summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/l10n/loader.js
blob: 60e219e441c3fc40998665534a0eaa7cd76719e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

module.metadata = {
  "stability": "unstable"
};

const { Cc, Ci } = require("chrome");
const { getPreferedLocales, findClosestLocale } = require("./locale");
const { readURI } = require("../net/url");
const { resolve } = require("../core/promise");

function parseJsonURI(uri) {
  return readURI(uri).
    then(JSON.parse).
    then(null, function (error) {
      throw Error("Failed to parse locale file:\n" + uri + "\n" + error);
    });
}

// Returns the array stored in `locales.json` manifest that list available
// locales files
function getAvailableLocales(rootURI) {
  let uri = rootURI + "locales.json";
  return parseJsonURI(uri).then(function (manifest) {
    return "locales" in manifest &&
           Array.isArray(manifest.locales) ?
           manifest.locales : [];
  });
}

// Returns URI of the best locales file to use from the XPI
function getBestLocale(rootURI) {
  // Read localization manifest file that contains list of available languages
  return getAvailableLocales(rootURI).then(function (availableLocales) {
    // Retrieve list of prefered locales to use
    let preferedLocales = getPreferedLocales();

    // Compute the most preferable locale to use by using these two lists
    return findClosestLocale(availableLocales, preferedLocales);
  });
}

/**
 * Read localization files and returns a promise of data to put in `@l10n/data`
 * pseudo module, in order to allow l10n/json/core to fetch it.
 */
exports.load = function load(rootURI) {
  // First, search for a locale file:
  return getBestLocale(rootURI).then(function (bestMatchingLocale) {
    // It may be null if the addon doesn't have any locale file
    if (!bestMatchingLocale)
      return resolve(null);

    let localeURI = rootURI + "locale/" + bestMatchingLocale + ".json";

    // Locale files only contains one big JSON object that is used as
    // an hashtable of: "key to translate" => "translated key"
    // TODO: We are likely to change this in order to be able to overload
    //       a specific key translation. For a specific package, module or line?
    return parseJsonURI(localeURI).then(function (json) {
      return {
        hash: json,
        bestMatchingLocale: bestMatchingLocale
      };
    });
  });
}