Add language.js

This commit is contained in:
Aaran Xu 2021-05-10 00:14:03 +08:00
parent 9344fcaf19
commit 53b811b041
2 changed files with 34 additions and 1 deletions

View File

@ -6,5 +6,5 @@ language = "zh-CN"
[output.html] [output.html]
additional-css = ["ferris.css", "theme/2018-edition.css"] additional-css = ["ferris.css", "theme/2018-edition.css"]
additional-js = ["ferris.js"] additional-js = ["ferris.js", "language.js"]
git-repository-url = "https://github.com/rust-lang-cn/book-cn" git-repository-url = "https://github.com/rust-lang-cn/book-cn"

33
language.js Normal file
View File

@ -0,0 +1,33 @@
(function () {
var url = window.location.href;
var host = window.location.host;
var search = {
en: "/en/",
zh_CN: "/zh-CN/"
}
var replaceWith = {
en: "/zh-CN/",
zh_CN: "/en/"
}
var link = "";
var word = "";
if (url.indexOf(search.en) != -1 && url.indexOf(search.en) === (url.indexOf(host) + host.length)) {
link = url.replace(search.en, replaceWith.en);
word = "简体中文";
} else if (url.indexOf(search.zh_CN) != -1 && url.indexOf(search.zh_CN) === (url.indexOf(host) + host.length)) {
link = url.replace(search.zh_CN, replaceWith.zh_CN);
word = "English";
}
var node = '<a href="' + link + '"><i id="print-button" class="fa fa-language"> ' + word + '</i></a>';
var insertNode = document.getElementsByClassName('right-buttons');
if (insertNode.length > 0 && link != "") {
var html = insertNode[0].innerHTML;
insertNode[0].innerHTML = html + node;
}
})()