mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Added a language switcher for snowflake.tp.o
Also modified the styling of the page to match the main tp.o page a bit more
This commit is contained in:
parent
f6517f60ce
commit
ab96817381
4 changed files with 8228 additions and 98 deletions
|
@ -1,23 +1,34 @@
|
|||
/* global availableLangs */
|
||||
|
||||
class Messages {
|
||||
constructor(json) {
|
||||
this.json = json;
|
||||
}
|
||||
getMessage(m, ...rest) {
|
||||
if (this.json.hasOwnProperty(m)) {
|
||||
let message = this.json[m].message;
|
||||
return message.replace(/\$(\d+)/g, (...args) => {
|
||||
return rest[Number(args[1]) - 1];
|
||||
});
|
||||
if (Object.prototype.hasOwnProperty.call(this.json, m)) {
|
||||
let message = this.json[m].message;
|
||||
return message.replace(/\$(\d+)/g, (...args) => {
|
||||
return rest[Number(args[1]) - 1];
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
defaultLang = "en_US";
|
||||
var defaultLang = "en_US";
|
||||
|
||||
var getLang = function() {
|
||||
let lang = navigator.language || defaultLang;
|
||||
lang = lang.replace(/-/g, '_');
|
||||
|
||||
//prioritize override language
|
||||
var url_string = window.location.href; //window.location.href
|
||||
var url = new URL(url_string);
|
||||
var override_lang = url.searchParams.get("lang");
|
||||
if (override_lang != null) {
|
||||
lang = override_lang;
|
||||
}
|
||||
|
||||
if (availableLangs.has(lang)) {
|
||||
return lang;
|
||||
}
|
||||
|
@ -31,30 +42,41 @@ var getLang = function() {
|
|||
var fill = function(n, func) {
|
||||
switch(n.nodeType) {
|
||||
case 1: // Node.ELEMENT_NODE
|
||||
{
|
||||
const m = /^__MSG_([^_]*)__$/.exec(n.dataset.msgid);
|
||||
if (m) {
|
||||
val = func(m[1]);
|
||||
if (val != undefined) {
|
||||
n.innerHTML = val
|
||||
}
|
||||
var val = func(m[1]);
|
||||
if (val != undefined) {
|
||||
n.innerHTML = val
|
||||
}
|
||||
}
|
||||
n.childNodes.forEach(c => fill(c, func));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Fetching", `./_locales/${getLang()}/messages.json`);
|
||||
|
||||
fetch(`./_locales/${getLang()}/messages.json`)
|
||||
.then((res) => {
|
||||
if (!res.ok) { return; }
|
||||
return res.json();
|
||||
})
|
||||
.then((json) => {
|
||||
messages = new Messages(json);
|
||||
console.log("Filling document body");
|
||||
fill(document.body, (m) => {
|
||||
console.log("Filling ", m);
|
||||
return messages.getMessage(m);
|
||||
});
|
||||
.then((res) => {
|
||||
if (!res.ok) { return; }
|
||||
return res.json();
|
||||
})
|
||||
.then((json) => {
|
||||
var language = document.getElementById('language-switcher');
|
||||
language.innerText = `${getLang()}`
|
||||
var messages = new Messages(json);
|
||||
fill(document.body, (m) => {
|
||||
return messages.getMessage(m);
|
||||
});
|
||||
});
|
||||
|
||||
// Populate language swticher list
|
||||
availableLangs.forEach(function (lang) {
|
||||
var languageList = document.getElementById('supported-languages');
|
||||
var link = document.createElement('a');
|
||||
link.setAttribute('href', '?lang='+lang);
|
||||
link.setAttribute('class', "dropdown-item");
|
||||
link.innerText = lang;
|
||||
languageList.lastChild.after(link);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue