|
| 1 | +(() => { |
| 2 | + const STORAGE_KEY = 'epub2txt-lang'; |
| 3 | + const path = window.location.pathname || '/'; |
| 4 | + |
| 5 | + const normalizeLang = (value) => { |
| 6 | + if (!value) return null; |
| 7 | + const lower = String(value).toLowerCase(); |
| 8 | + if (lower.startsWith('ja')) return 'ja'; |
| 9 | + if (lower.startsWith('zh')) return 'zh'; |
| 10 | + return 'en'; |
| 11 | + }; |
| 12 | + |
| 13 | + const getBasePath = () => { |
| 14 | + if (path.includes('/ja/')) { |
| 15 | + return `${path.split('/ja/')[0]}/`; |
| 16 | + } |
| 17 | + if (path.includes('/zh/')) { |
| 18 | + return `${path.split('/zh/')[0]}/`; |
| 19 | + } |
| 20 | + const withoutFile = path.endsWith('/index.html') |
| 21 | + ? path.slice(0, path.lastIndexOf('/') + 1) |
| 22 | + : path; |
| 23 | + return withoutFile.endsWith('/') ? withoutFile : `${withoutFile}/`; |
| 24 | + }; |
| 25 | + |
| 26 | + const normalizePath = (value) => { |
| 27 | + let normalized = value.endsWith('/index.html') |
| 28 | + ? value.slice(0, value.lastIndexOf('/') + 1) |
| 29 | + : value; |
| 30 | + if (!normalized.endsWith('/')) { |
| 31 | + normalized += '/'; |
| 32 | + } |
| 33 | + return normalized; |
| 34 | + }; |
| 35 | + |
| 36 | + const basePath = getBasePath(); |
| 37 | + const currentLang = path.includes('/ja/') |
| 38 | + ? 'ja' |
| 39 | + : path.includes('/zh/') |
| 40 | + ? 'zh' |
| 41 | + : 'en'; |
| 42 | + |
| 43 | + const targetFor = (lang) => { |
| 44 | + if (lang === 'ja') return `${basePath}ja/`; |
| 45 | + if (lang === 'zh') return `${basePath}zh/`; |
| 46 | + return basePath; |
| 47 | + }; |
| 48 | + |
| 49 | + const redirectIfNeeded = (lang) => { |
| 50 | + const target = targetFor(lang); |
| 51 | + if (normalizePath(path) === normalizePath(target)) return; |
| 52 | + window.location.replace(target); |
| 53 | + }; |
| 54 | + |
| 55 | + const safeStorageGet = (key) => { |
| 56 | + try { |
| 57 | + return localStorage.getItem(key); |
| 58 | + } catch { |
| 59 | + return null; |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + const safeStorageSet = (key, value) => { |
| 64 | + try { |
| 65 | + localStorage.setItem(key, value); |
| 66 | + } catch { |
| 67 | + // Ignore storage failures to keep redirects functional. |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + const saved = normalizeLang(safeStorageGet(STORAGE_KEY)); |
| 72 | + if (saved && saved !== currentLang) { |
| 73 | + redirectIfNeeded(saved); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (!saved) { |
| 78 | + const languageList = (navigator.languages && navigator.languages.length) |
| 79 | + ? navigator.languages |
| 80 | + : [navigator.language || navigator.userLanguage || '']; |
| 81 | + let detected = 'en'; |
| 82 | + for (const lang of languageList) { |
| 83 | + const normalized = normalizeLang(lang); |
| 84 | + if (normalized === 'ja') { |
| 85 | + detected = 'ja'; |
| 86 | + break; |
| 87 | + } |
| 88 | + if (normalized === 'zh') { |
| 89 | + detected = 'zh'; |
| 90 | + } |
| 91 | + } |
| 92 | + if (detected !== currentLang) { |
| 93 | + redirectIfNeeded(detected); |
| 94 | + return; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const detectLangFromHref = (href) => { |
| 99 | + if (!href) return 'en'; |
| 100 | + let pathname = ''; |
| 101 | + try { |
| 102 | + pathname = new URL(href, window.location.href).pathname.toLowerCase(); |
| 103 | + } catch { |
| 104 | + pathname = String(href).toLowerCase(); |
| 105 | + } |
| 106 | + if (pathname.includes('/ja/')) return 'ja'; |
| 107 | + if (pathname.includes('/zh/')) return 'zh'; |
| 108 | + return 'en'; |
| 109 | + }; |
| 110 | + |
| 111 | + document.addEventListener('DOMContentLoaded', () => { |
| 112 | + const links = document.querySelectorAll('.lang-switch'); |
| 113 | + const savePreference = (link) => { |
| 114 | + const href = link.getAttribute('href') || ''; |
| 115 | + const preference = detectLangFromHref(href); |
| 116 | + safeStorageSet(STORAGE_KEY, preference); |
| 117 | + }; |
| 118 | + links.forEach((link) => { |
| 119 | + link.addEventListener('click', () => savePreference(link)); |
| 120 | + link.addEventListener('pointerdown', () => savePreference(link)); |
| 121 | + link.addEventListener('keydown', (event) => { |
| 122 | + if (event.key === 'Enter' || event.key === ' ') { |
| 123 | + savePreference(link); |
| 124 | + } |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +})(); |
0 commit comments