mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Hook up localized messages.json to website
Right now we use the navigator language to determine localization and replace the website contents with translated strings.
This commit is contained in:
parent
9140c7648c
commit
f6517f60ce
3 changed files with 65 additions and 2 deletions
|
@ -94,6 +94,8 @@ task('build', 'build the snowflake proxy', function() {
|
||||||
execSync(`cp -r ${STATIC}/ ${outDir}/`);
|
execSync(`cp -r ${STATIC}/ ${outDir}/`);
|
||||||
copyTranslations(outDir);
|
copyTranslations(outDir);
|
||||||
concatJS(outDir, 'badge', 'embed.js', availableLangs());
|
concatJS(outDir, 'badge', 'embed.js', availableLangs());
|
||||||
|
writeFileSync(`${outDir}/index.js`, availableLangs(), 'utf8');
|
||||||
|
execSync(`cat ${STATIC}/index.js >> ${outDir}/index.js`);
|
||||||
console.log('Snowflake prepared.');
|
console.log('Snowflake prepared.');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
<p class="diagram"><img src="https://trac.torproject.org/projects/tor/raw-attachment/wiki/doc/Snowflake/snowflake-schematic.png" alt="Diagram" /></p>
|
<p class="diagram"><img src="https://trac.torproject.org/projects/tor/raw-attachment/wiki/doc/Snowflake/snowflake-schematic.png" alt="Diagram" /></p>
|
||||||
|
|
||||||
<p data-mdgid="__MSG_websiteIntro__">Snowflake is a system to defeat internet censorship. People who are
|
<p data-msgid="__MSG_websiteIntro__">Snowflake is a system to defeat internet censorship. People who are
|
||||||
censored can use Snowflake to access the internet. Their connection goes
|
censored can use Snowflake to access the internet. Their connection goes
|
||||||
through Snowflake proxies, which are run by volunteers. For more detailed
|
through Snowflake proxies, which are run by volunteers. For more detailed
|
||||||
information about how Snowflake works see our
|
information about how Snowflake works see our
|
||||||
|
@ -96,5 +96,6 @@
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
<script src="index.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
60
proxy/static/index.js
Normal file
60
proxy/static/index.js
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
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];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
defaultLang = "en_US";
|
||||||
|
|
||||||
|
var getLang = function() {
|
||||||
|
let lang = navigator.language || defaultLang;
|
||||||
|
lang = lang.replace(/-/g, '_');
|
||||||
|
if (availableLangs.has(lang)) {
|
||||||
|
return lang;
|
||||||
|
}
|
||||||
|
lang = lang.split('_')[0];
|
||||||
|
if (availableLangs.has(lang)) {
|
||||||
|
return lang;
|
||||||
|
}
|
||||||
|
return defaultLang;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue