/*
* Site-wide Traditional <-> Simplified Chinese toggle (繁↔簡).
*
* State lives ONLY in localStorage ("preferred_lang") plus, for logged-in
* users, the account record via GET/POST /api/lang. It is never read from or
* written to the URL, so the language cannot be triggered by typing a URL.
*
* Usage: include static/vendor/opencc/full.js then this file at the end of
*
. If the page has a #btn-translate button it is reused (personal
* page navbar); otherwise a small fixed button is injected top-right.
*
* Pages that build chart configs in JS should wrap Chinese labels with
* window.langText(...) so canvas-drawn text follows the language too.
*/
(function () {
'use strict';
var LANG_KEY = 'preferred_lang';
var TW = 'zh-TW';
var CN = 'zh-CN';
if (!window.OpenCC) { // library blocked/missing: leave the page untouched
window.langText = function (s) { return s; };
return;
}
var t2s = OpenCC.Converter({ from: 'hk', to: 'cn' });
var s2t = OpenCC.Converter({ from: 'cn', to: 'hk' });
function currentLang() {
return localStorage.getItem(LANG_KEY) === CN ? CN : TW;
}
// Convert a string into the current language (identity in zh-TW mode).
window.langText = function (s) {
return (currentLang() === CN && typeof s === 'string') ? t2s(s) : s;
};
// JS-built dialogs follow the language too.
var nativeAlert = window.alert.bind(window);
var nativeConfirm = window.confirm.bind(window);
window.alert = function (msg) { return nativeAlert(window.langText(String(msg))); };
window.confirm = function (msg) { return nativeConfirm(window.langText(String(msg))); };
function convertTextNode(node) {
var parent = node.parentElement;
if (!parent) return;
var tag = parent.tagName;
if (tag === 'SCRIPT' || tag === 'STYLE') return;
// An