Skip to content

Commit a9c299f

Browse files
committed
Site update
1 parent 1109694 commit a9c299f

File tree

5 files changed

+137
-4
lines changed

5 files changed

+137
-4
lines changed

app/lang-redirect.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
})();

css/style.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ header {
4747
font-weight: 700;
4848
font-size: 1.25rem;
4949
letter-spacing: -0.025em;
50+
color: #fff;
51+
text-decoration: none;
5052
}
5153

5254
.header-links {
@@ -479,4 +481,4 @@ footer a:hover {
479481
.text-link:hover {
480482
color: white;
481483
border-bottom-color: var(--accent-color);
482-
}
484+
}

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<link rel="preconnect" href="https://fonts.googleapis.com">
3434
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
3535
<link rel="stylesheet" href="css/style.css">
36+
<script src="app/lang-redirect.js"></script>
3637
<!-- Umami Analytics -->
3738
<script defer src="https://cloud.umami.is/script.js" data-website-id="8771ed9f-edd3-4665-bf16-1bc95940af08"></script>
3839
<!-- JSZip & Custom Script -->
@@ -44,7 +45,7 @@
4445

4546
<div class="container">
4647
<header>
47-
<div class="logo">epub2txt</div>
48+
<a class="logo" href="./">epub2txt</a>
4849
<div class="header-links">
4950
<div class="lang-group">
5051
<a href="index.html" class="lang-switch active">EN</a>

ja/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<link rel="preconnect" href="https://fonts.googleapis.com">
3434
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
3535
<link rel="stylesheet" href="../css/style.css">
36+
<script src="../app/lang-redirect.js"></script>
3637
<!-- Umami Analytics -->
3738
<script defer src="https://cloud.umami.is/script.js" data-website-id="8771ed9f-edd3-4665-bf16-1bc95940af08"></script>
3839
<!-- JSZip & Custom Script -->
@@ -44,7 +45,7 @@
4445

4546
<div class="container">
4647
<header>
47-
<div class="logo">epub2txt</div>
48+
<a class="logo" href="./">epub2txt</a>
4849
<div class="header-links">
4950
<div class="lang-group">
5051
<a href="../" class="lang-switch">EN</a>

zh/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<link rel="preconnect" href="https://fonts.googleapis.com">
3434
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
3535
<link rel="stylesheet" href="../css/style.css">
36+
<script src="../app/lang-redirect.js"></script>
3637
<!-- Umami Analytics -->
3738
<script defer src="https://cloud.umami.is/script.js" data-website-id="8771ed9f-edd3-4665-bf16-1bc95940af08"></script>
3839
<!-- JSZip & Custom Script -->
@@ -44,7 +45,7 @@
4445

4546
<div class="container">
4647
<header>
47-
<div class="logo">epub2txt</div>
48+
<a class="logo" href="./">epub2txt</a>
4849
<div class="header-links">
4950
<div class="lang-group">
5051
<a href="../" class="lang-switch">EN</a>

0 commit comments

Comments
 (0)