diff --git a/book.toml b/book.toml index 33c0b56..5992cb0 100644 --- a/book.toml +++ b/book.toml @@ -6,5 +6,5 @@ language = "zh-CN" [output.html] 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" diff --git a/language.js b/language.js new file mode 100644 index 0000000..86a19de --- /dev/null +++ b/language.js @@ -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 = ' ' + word + ''; + var insertNode = document.getElementsByClassName('right-buttons'); + if (insertNode.length > 0 && link != "") { + var html = insertNode[0].innerHTML; + insertNode[0].innerHTML = html + node; + } +})()