Skip to content

Instantly share code, notes, and snippets.

@cwchentw

cwchentw/zhConvert.js

Last active Feb 9, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.