forked from Happy-Ferret/weh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweh-i18n.js
More file actions
63 lines (56 loc) · 1.62 KB
/
weh-i18n.js
File metadata and controls
63 lines (56 loc) · 1.62 KB
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
/*
* weh - WebExtensions Helper
*
* @summary workflow and base code for developing WebExtensions browser add-ons
* @author Michel Gutierrez
* @link https://github.com/mi-g/weh
*
* 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/.
*/
const { browser } = require('weh');
var customStrings = {}
const SUBST_RE = new RegExp("\\$[a-zA-Z]*([0-9]+)\\$","g");
function Load() {
try {
customStrings = JSON.parse(window.localStorage.getItem("wehI18nCustom"));
if(customStrings===null) {
customStrings = {};
browser.storage.local.get("wehI18nCustom")
.then((result)=>{
var weCustomStrings = result.wehI18nCustom;
if(weCustomStrings)
Object.assign(customStrings,weCustomStrings);
});
}
} catch(e) {
customStrings = {};
}
}
Load();
function GetMessage(messageName,substitutions) {
if(/-/.test(messageName)) {
var fixedName = messageName.replace(/-/g,"_");
console.warn("Wrong i18n message name. Should it be",fixedName,"instead of",messageName,"?");
messageName = fixedName;
}
var custom = customStrings[messageName];
if(custom && custom.message.length>0) {
if(!Array.isArray(substitutions))
substitutions = [ substitutions ];
var str = (custom.message || "").replace(SUBST_RE,(ph)=>{
var m = SUBST_RE.exec(ph);
if(m)
return substitutions[parseInt(m[1])-1] || "??";
else
return "??";
});
return str;
}
return browser.i18n.getMessage.apply(browser.i18n,arguments);
}
module.exports = {
getMessage: GetMessage,
reload: Load
}