Skip to content

Instantly share code, notes, and snippets.

@cwchentw
Last active February 9, 2019 07:09
Show Gist options
  • Save cwchentw/f0722b50bd367262edc864d35ea99fac to your computer and use it in GitHub Desktop.
Save cwchentw/f0722b50bd367262edc864d35ea99fac to your computer and use it in GitHub Desktop.
Convert from zh-TW to zh-CN (Apache 2.0)
document.addEventListener("DOMContentLoaded", function () {
var isSimplifiedChinese = function (lang) {
var l = lang.toLowerCase();
return l === "zh-cn" || l === "zh_zn" ||
l === "zh-sg" || l === "zh_sg" ||
l === "zh-hans" || l === "zh_hans" ||
l === "zh";
};
var convertZh = function (selector) {
var zhmap = TongWen_ts;
let es = document.querySelectorAll(selector);
for (let i = 0; i < es.length; i++) {
es[i].innerHTML = es[i].innerHTML.replace(/[^\x00-\xFF]/g, function (s) {
return s in zhmap ? zhmap[s] : s;
});
}
};
var lang = navigator.language || navigator.userLanguage;
if (isSimplifiedChinese(lang)) {
let elements = ["h1", "h2", "p", "li", "a"];
for (let i = 0; i < elements.length; i++) {
convertZh(elements[i]);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment