2018-08-14 12:11:49 +00:00
|
|
|
/*
|
|
|
|
* noVNC: HTML5 VNC client
|
2019-06-24 11:26:25 +00:00
|
|
|
* Copyright (C) 2018 The noVNC Authors
|
2018-08-14 12:11:49 +00:00
|
|
|
* Licensed under MPL 2.0 (see LICENSE.txt)
|
|
|
|
*
|
|
|
|
* See README.md for usage and integration instructions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Localization Utilities
|
|
|
|
*/
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export class Localizer {
|
|
|
|
constructor() {
|
|
|
|
// Currently configured language
|
|
|
|
this.language = 'en';
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
// Current dictionary of translations
|
|
|
|
this.dictionary = undefined;
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Configure suitable language based on user preferences
|
2019-06-24 11:26:25 +00:00
|
|
|
setup(supportedLanguages) {
|
2018-08-14 12:11:49 +00:00
|
|
|
this.language = 'en'; // Default: US English
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Navigator.languages only available in Chrome (32+) and FireFox (32+)
|
|
|
|
* Fall back to navigator.language for other browsers
|
|
|
|
*/
|
2019-06-24 11:26:25 +00:00
|
|
|
let userLanguages;
|
2018-08-14 12:11:49 +00:00
|
|
|
if (typeof window.navigator.languages == 'object') {
|
|
|
|
userLanguages = window.navigator.languages;
|
|
|
|
} else {
|
|
|
|
userLanguages = [navigator.language || navigator.userLanguage];
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
for (let i = 0;i < userLanguages.length;i++) {
|
|
|
|
const userLang = userLanguages[i]
|
|
|
|
.toLowerCase()
|
|
|
|
.replace("_", "-")
|
|
|
|
.split("-");
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Built-in default?
|
2018-08-27 12:30:50 +00:00
|
|
|
if ((userLang[0] === 'en') &&
|
|
|
|
((userLang[1] === undefined) || (userLang[1] === 'us'))) {
|
2018-08-14 12:11:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First pass: perfect match
|
2019-06-24 11:26:25 +00:00
|
|
|
for (let j = 0; j < supportedLanguages.length; j++) {
|
|
|
|
const supLang = supportedLanguages[j]
|
|
|
|
.toLowerCase()
|
|
|
|
.replace("_", "-")
|
|
|
|
.split("-");
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
if (userLang[0] !== supLang[0]) {
|
2018-08-27 12:30:50 +00:00
|
|
|
continue;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
|
|
|
if (userLang[1] !== supLang[1]) {
|
2018-08-27 12:30:50 +00:00
|
|
|
continue;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
this.language = supportedLanguages[j];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass: fallback
|
2019-06-24 11:26:25 +00:00
|
|
|
for (let j = 0;j < supportedLanguages.length;j++) {
|
|
|
|
const supLang = supportedLanguages[j]
|
|
|
|
.toLowerCase()
|
|
|
|
.replace("_", "-")
|
|
|
|
.split("-");
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
if (userLang[0] !== supLang[0]) {
|
2018-08-27 12:30:50 +00:00
|
|
|
continue;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
|
|
|
if (supLang[1] !== undefined) {
|
2018-08-27 12:30:50 +00:00
|
|
|
continue;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
this.language = supportedLanguages[j];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Retrieve localised text
|
2019-06-24 11:26:25 +00:00
|
|
|
get(id) {
|
2018-08-14 12:11:49 +00:00
|
|
|
if (typeof this.dictionary !== 'undefined' && this.dictionary[id]) {
|
|
|
|
return this.dictionary[id];
|
|
|
|
} else {
|
|
|
|
return id;
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Traverses the DOM and translates relevant fields
|
|
|
|
// See https://html.spec.whatwg.org/multipage/dom.html#attr-translate
|
2019-06-24 11:26:25 +00:00
|
|
|
translateDOM() {
|
|
|
|
const self = this;
|
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
function process(elem, enabled) {
|
|
|
|
function isAnyOf(searchElement, items) {
|
|
|
|
return items.indexOf(searchElement) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateAttribute(elem, attr) {
|
2019-06-24 11:26:25 +00:00
|
|
|
const str = self.get(elem.getAttribute(attr));
|
2018-08-14 12:11:49 +00:00
|
|
|
elem.setAttribute(attr, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateTextNode(node) {
|
2019-06-24 11:26:25 +00:00
|
|
|
const str = self.get(node.data.trim());
|
2018-08-14 12:11:49 +00:00
|
|
|
node.data = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elem.hasAttribute("translate")) {
|
|
|
|
if (isAnyOf(elem.getAttribute("translate"), ["", "yes"])) {
|
|
|
|
enabled = true;
|
|
|
|
} else if (isAnyOf(elem.getAttribute("translate"), ["no"])) {
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled) {
|
2018-08-27 12:30:50 +00:00
|
|
|
if (elem.hasAttribute("abbr") &&
|
|
|
|
elem.tagName === "TH") {
|
2018-08-14 12:11:49 +00:00
|
|
|
translateAttribute(elem, "abbr");
|
|
|
|
}
|
2018-08-27 12:30:50 +00:00
|
|
|
if (elem.hasAttribute("alt") &&
|
|
|
|
isAnyOf(elem.tagName, ["AREA", "IMG", "INPUT"])) {
|
2018-08-14 12:11:49 +00:00
|
|
|
translateAttribute(elem, "alt");
|
|
|
|
}
|
2018-08-27 12:30:50 +00:00
|
|
|
if (elem.hasAttribute("download") &&
|
|
|
|
isAnyOf(elem.tagName, ["A", "AREA"])) {
|
2018-08-14 12:11:49 +00:00
|
|
|
translateAttribute(elem, "download");
|
|
|
|
}
|
2018-08-27 12:30:50 +00:00
|
|
|
if (elem.hasAttribute("label") &&
|
|
|
|
isAnyOf(elem.tagName, ["MENUITEM", "MENU", "OPTGROUP",
|
2019-06-24 11:26:25 +00:00
|
|
|
"OPTION", "TRACK"])) {
|
2018-08-14 12:11:49 +00:00
|
|
|
translateAttribute(elem, "label");
|
|
|
|
}
|
|
|
|
// FIXME: Should update "lang"
|
2018-08-27 12:30:50 +00:00
|
|
|
if (elem.hasAttribute("placeholder") &&
|
|
|
|
isAnyOf(elem.tagName, ["INPUT", "TEXTAREA"])) {
|
2018-08-14 12:11:49 +00:00
|
|
|
translateAttribute(elem, "placeholder");
|
|
|
|
}
|
|
|
|
if (elem.hasAttribute("title")) {
|
|
|
|
translateAttribute(elem, "title");
|
|
|
|
}
|
2018-08-27 12:30:50 +00:00
|
|
|
if (elem.hasAttribute("value") &&
|
|
|
|
elem.tagName === "INPUT" &&
|
|
|
|
isAnyOf(elem.getAttribute("type"), ["reset", "button", "submit"])) {
|
2018-08-14 12:11:49 +00:00
|
|
|
translateAttribute(elem, "value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
for (let i = 0; i < elem.childNodes.length; i++) {
|
|
|
|
const node = elem.childNodes[i];
|
2018-08-14 12:11:49 +00:00
|
|
|
if (node.nodeType === node.ELEMENT_NODE) {
|
|
|
|
process(node, enabled);
|
|
|
|
} else if (node.nodeType === node.TEXT_NODE && enabled) {
|
|
|
|
translateTextNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process(document.body, true);
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export const l10n = new Localizer();
|
2018-08-27 12:30:50 +00:00
|
|
|
export default l10n.get.bind(l10n);
|