keepassxc-browser/keepassxc-browser/translate.js
2019-04-17 20:12:30 +02:00

34 lines
1,012 B
JavaScript

'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 ];
}