blob: 601b92b1e83e811a74077170f91cd689b4d3beed (
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
|
'use strict';
const items = document.querySelectorAll('[data-i18n]');
for (const item of items) {
let key = item.getAttribute('data-i18n');
if (key) {
let attr = '';
[ attr, key ] = trAttribute(key);
const placeholder = item.getAttribute('i18n-placeholder');
const translation = placeholder ? browser.i18n.getMessage(key, placeholder) : browser.i18n.getMessage(key);
if (attr) {
item.setAttribute(attr, translation);
} else if (item.hasAttribute('href')) {
item.text = translation;
} else {
item.innerHTML = translation;
}
// Remove the translation attribute from the HTML element
item.removeAttribute('data-i18n');
}
}
// Check if a custom attribute is being translated using [attribute]key
function trAttribute(key) {
let attr = /\[(\w+)\]/.exec(key);
if (attr) {
attr = attr[1];
key = key.slice(key.indexOf(']') + 1);
}
return [ attr, key ];
}
|