From 965c7025c89992887a6db4bb6a8f6757406d598a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E5=BF=86?= Date: Fri, 14 Apr 2023 17:30:35 +0800 Subject: [PATCH] Feature: Content language based i18n multilingual support --- static/wikimirror.js | 125 ++++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 61 deletions(-) diff --git a/static/wikimirror.js b/static/wikimirror.js index 0914830..53b037a 100644 --- a/static/wikimirror.js +++ b/static/wikimirror.js @@ -279,7 +279,10 @@ const WikiMirrorPrivateMethod = class WikiMirrorPrivateMethod { nodeValue = this.getRealText(nodeValue); } if (nodeValue.match(RegexOther_2) && this.hasClass('action-view')) { - nodeValue = nodeValue.replace(RegexOther_2, `background$1:url($2$3//upload.${this.MIRROR_DOMAIN}`); + nodeValue = nodeValue.replace( + RegexOther_2, + `background$1:url($2$3//upload.${this.MIRROR_DOMAIN}` + ); } if (node.nodeValue !== nodeValue) { node.nodeValue = nodeValue; @@ -1434,8 +1437,19 @@ const WikiMirrorPrivateMethod = class WikiMirrorPrivateMethod { .replace(/%5B/g, '[') .replace(/%5D/g, ']'); } - hanAssist(language) { - const DefaultFallback = [language, 'en']; + hasClass(name, selector = 'body') { + if (!name || !document.querySelector(selector)) { + return false; + } + for (const dom of document.querySelectorAll(selector)) { + if (dom.classList.contains(name)) { + return true; + } + } + return false; + } + i18n() { + const DefaultFallback = [document.documentElement.lang ?? navigator.language.split('-')[0], 'en']; const FallbackTable = { zh: ['zh', 'hans', 'hant', 'cn', 'tw', 'hk', 'sg', 'mo', 'my', 'en'], 'zh-hans': ['hans', 'cn', 'sg', 'my', 'zh', 'hant', 'tw', 'hk', 'mo', 'en'], @@ -1449,13 +1463,16 @@ const WikiMirrorPrivateMethod = class WikiMirrorPrivateMethod { }; const elect = (candidates, locale) => { for (const key of FallbackTable[locale] ?? DefaultFallback) { - if (key in candidates) { + if (key in candidates && candidates[key] !== undefined) { return candidates[key]; } } return ''; }; return { + content: (candidates) => { + return elect(candidates, this.getConf('wgContentLanguage') ?? ''); + }, localize: (candidates) => { return elect(candidates, this.getConf('wgUserLanguage') ?? this.getConf('wgContentLanguage') ?? ''); }, @@ -1464,17 +1481,6 @@ const WikiMirrorPrivateMethod = class WikiMirrorPrivateMethod { }, }; } - hasClass(name, selector = 'body') { - if (!name || !document.querySelector(selector)) { - return false; - } - for (const dom of document.querySelectorAll(selector)) { - if (dom.classList.contains(name)) { - return true; - } - } - return false; - } localStorage(name, value) { const check = (() => { try { @@ -1914,158 +1920,155 @@ const WikiMirrorPrivateMethod = class WikiMirrorPrivateMethod { } } initMessages() { - const hanAssist = this.hanAssist.call( - this, - document.documentElement.lang ?? navigator.language.split('-')[0] ?? 'en' - ); + const localize = this.i18n().localize; return { ajaxLogin: { - '6-digit number': hanAssist.localize({ + '6-digit number': localize({ ja: '6桁の数字', hans: '6位数字', hant: '6位數字', }), - 'Automatic account creation failed': hanAssist.localize({ + 'Automatic account creation failed': localize({ ja: 'アカウントの自動作成に失敗しました', hans: '自动创建账号失败', hant: '自動創建帳戶失敗', }), - Cancel: hanAssist.localize({ + Cancel: localize({ ja: 'キャンセル', zh: '取消', }), - 'Cross wiki automatic login': hanAssist.localize({ + 'Cross wiki automatic login': localize({ ja: '跨ウィキ自動ログイン', hans: '跨维基自动登录', hant: '跨维基自動登入', }), - 'Enter password': hanAssist.localize({ + 'Enter password': localize({ ja: 'パスワード', hans: '请输入密码', hant: '請輸入密碼', }), - 'Enter 2FA verification code': hanAssist.localize({ + 'Enter 2FA verification code': localize({ en: '2FA verification code', ja: '2FA認証コード', hans: '请输入2FA验证码', hant: '請輸入2FA驗證碼', }), - 'Forgot password?': hanAssist.localize({ + 'Forgot password?': localize({ ja: 'パスワードをお忘れですか?', hans: '忘记密码?', hant: '忘記密碼?', }), - 'Getting login token': hanAssist.localize({ + 'Getting login token': localize({ ja: 'ログイントークンを取得しています', hans: '正在获取登录令牌', hant: '正在獲取登入權杖', }), 'If selected, the login status will be kept for 1 year. If not selected, it will be kept for 1 month.': - hanAssist.localize({ + localize({ ja: 'チェックを入れるとログイン状態が1年間保持され、入れない場合は1ヶ月間保持されます。', hans: '勾选则保持登录状态1年,不勾选则保持1个月。', hant: '勾選则保持登入狀態1年,不勾選則保持1個月。', }), - 'Keep me logged in': hanAssist.localize({ + 'Keep me logged in': localize({ ja: 'ログイン状態を保持', hans: '记住我的登录状态', hant: '記住我的登入狀態', }), - 'Logging in': hanAssist.localize({ + 'Logging in': localize({ ja: 'ログインしています', hans: '正在登录', hant: '正在登入', }), - Login: hanAssist.localize({ + Login: localize({ ja: 'ログイン', hans: '登录', hant: '登入', }), - 'Login cancelled': hanAssist.localize({ + 'Login cancelled': localize({ ja: 'ログインキャンセル', hans: '登录取消', hant: '登入取消', }), - 'Login succeed': hanAssist.localize({ + 'Login succeed': localize({ ja: 'ログインに成功しました', hans: '登录成功', hant: '登入成功', }), - 'Login token getted': hanAssist.localize({ + 'Login token getted': localize({ ja: 'ログイントークンの取得に成功しました', hans: '获取登录令牌成功', hant: '獲取登入權杖成功', }), - 'New password': hanAssist.localize({ + 'New password': localize({ ja: '新しいパスワード', hans: '新密码', hant: '新密碼', }), - 'New password is required': hanAssist.localize({ + 'New password is required': localize({ ja: '新しいパスワードを設定してください', hans: '需要设置新密码', hant: '需要設置新密碼', }), - Password: hanAssist.localize({ + Password: localize({ ja: 'パスワード', hans: '密码', hant: '密碼', }), - 'Reset password': hanAssist.localize({ + 'Reset password': localize({ ja: 'パスワードをリセット', hans: '重置密码', hant: '重新設定密碼', }), - 'Starting automatic login': hanAssist.localize({ + 'Starting automatic login': localize({ ja: '自動ログインが開始されました', hans: '开始自动登录', hant: '开始自動登入', }), - 'The 2FA verification code cannot be empty': hanAssist.localize({ + 'The 2FA verification code cannot be empty': localize({ ja: '2FA認証コードが入力されていません', hans: '2FA验证码不能为空', hant: '2FA驗證碼不能爲空', }), - 'The password cannot be empty': hanAssist.localize({ + 'The password cannot be empty': localize({ ja: 'パスワードが入力されていません', hans: '密码不能为空', hant: '密碼不能爲空', }), - 'The username or password cannot be empty': hanAssist.localize({ + 'The username or password cannot be empty': localize({ ja: '利用者名またはパスワードが入力されていません', cn: '用户名或密码不能为空', hk: '用戶名稱或密碼不能爲空', tw: '使用者名稱或密碼不能爲空', }), - 'The user has been globally locked': hanAssist.localize({ + 'The user has been globally locked': localize({ ja: '利用者はグローバルにロックされています', cn: '用户已被全域锁定', hk: '用戶已被全域封鎖', tw: '使用者已被全域封鎖', }), - 'The user login is too frequent, please try again in five minutes': hanAssist.localize({ + 'The user login is too frequent, please try again in five minutes': localize({ ja: '利用者が頻繁すぎるため、5分後に再試行してください', cn: '用户登录过于频繁,请五分钟后再试', hk: '用戶登入過於頻繁,請五分鐘後再試', tw: '使用者登入过于频繁,请五分钟后再试', }), - 'Unknown API error': hanAssist.localize({ + 'Unknown API error': localize({ ja: '未定義のAPIエラー', hans: '未定义的API错误', hant: '未定義的API錯誤', }), - Username: hanAssist.localize({ + Username: localize({ ja: '利用者名', cn: '用户名', hk: '用戶名稱', tw: '使用者名稱', }), - 'Invalid 2FA verification code': hanAssist.localize({ + 'Invalid 2FA verification code': localize({ ja: '2FA認証コードが間違っています', hans: '2FA验证码错误', hant: '2FA驗證碼錯誤', }), - 'Invalid useruame or password': hanAssist.localize({ + 'Invalid useruame or password': localize({ ja: '利用者名またはパスワードが間違っています', cn: '用户名或密码错误', hk: '用戶名稱或密碼錯誤', @@ -2073,76 +2076,76 @@ const WikiMirrorPrivateMethod = class WikiMirrorPrivateMethod { }), }, confirmLogout: { - 'Confirm logout?': hanAssist.localize({ + 'Confirm logout?': localize({ ja: 'ログアウトしますか?', hans: '您确定要退出吗?', hant: '您確定要登出嗎?', }), }, darkMode: { - 'Dark mode': hanAssist.localize({ + 'Dark mode': localize({ ja: 'ダークモード', hans: '深色主题', hant: '深色主題', }), - 'Light mode': hanAssist.localize({ + 'Light mode': localize({ ja: '通常モード', hans: '浅色主题', hant: '淺色主題', }), - 'Switch to dark mode': hanAssist.localize({ + 'Switch to dark mode': localize({ ja: 'ダークモードに切り替えています', hans: '将镜像站的主题色切换至深色', hant: '將鏡像站的主題色切換至深色', }), - 'Switch to light mode': hanAssist.localize({ + 'Switch to light mode': localize({ ja: '通常モードに切り替えています', hans: '将镜像站的主题色切换至浅色', hant: '將鏡像站的主題色切換至淺色', }), }, diffLink: { - 'Copy the link to the diff version (wiki syntax)': hanAssist.localize({ + 'Copy the link to the diff version (wiki syntax)': localize({ ja: '現在の版間の差分リンクをコピーします(ウィキ記法)', hans: '复制链接到当前差异版本的维基语法', hant: '複製連結到當前差異版本的維基語法', }), - 'Copy the permanent link to the current version (wiki syntax)': hanAssist.localize({ + 'Copy the permanent link to the current version (wiki syntax)': localize({ ja: 'このページのこの版への固定リンクをコピーします(ウィキ記法)', hans: '复制链接到当前修订版本的维基语法', hant: '複製連結到當前修訂版本的維基語法', }), - 'Diff link': hanAssist.localize({ + 'Diff link': localize({ ja: '現在の版間の差分リンク', hans: '当前差异链接', hant: '當前差異連結', }), - 'Permanent link': hanAssist.localize({ + 'Permanent link': localize({ ja: 'この版への固定リンク', hans: '当前修订链接', hant: '當前修訂連結', }), }, showNotice: { - OK: hanAssist.localize({ + OK: localize({ ja: '了解', zh: '了解', }), }, showNetworkErrorNotice: { - 'Network error': hanAssist.localize({ + 'Network error': localize({ ja: 'ネットワークエラー', hans: '网络异常', hant: '網路異常', }), }, showRedirect: { - 'Official site': hanAssist.localize({ + 'Official site': localize({ ja: '公式サイト', hans: '访问官方页面', hant: '造訪官方頁', }), - 'Redirect the current mirror page to its corresponding official page': hanAssist.localize({ + 'Redirect the current mirror page to its corresponding official page': localize({ ja: '現在のミラーページを公式の対応ページにリダイレクトします', hans: '将当前镜像站页面重定向至官方相应页面', hant: '將當前鏡像頁重新導向至官方相應頁',