summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/parent/ext-i18n.js
blob: 167a1d16c2554c34ca838c07ae927beeb9b8df28 (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
/* 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";

ChromeUtils.defineESModuleGetters(this, {
  LanguageDetector:
    "resource://gre/modules/translation/LanguageDetector.sys.mjs",
});

this.i18n = class extends ExtensionAPI {
  getAPI(context) {
    let { extension } = context;
    return {
      i18n: {
        getMessage: function (messageName, substitutions) {
          return extension.localizeMessage(messageName, substitutions, {
            cloneScope: context.cloneScope,
          });
        },

        getAcceptLanguages: function () {
          let result = extension.localeData.acceptLanguages;
          return Promise.resolve(result);
        },

        getUILanguage: function () {
          return extension.localeData.uiLocale;
        },

        detectLanguage: function (text) {
          return LanguageDetector.detectLanguage(text).then(result => ({
            isReliable: result.confident,
            languages: result.languages.map(lang => {
              return {
                language: lang.languageCode,
                percentage: lang.percent,
              };
            }),
          }));
        },
      },
    };
  }
};