Skip to content

Commit 20f2147

Browse files
committed
fix: i18n for EN mode - theme toggle, section headers, date formats
1 parent 730b1ef commit 20f2147

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/components/NavBar.astro

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const langSwitch = locale === "en"
5757
<span>GitHub</span>
5858
</a>
5959
<!-- Theme toggle button -->
60-
<button id="theme-toggle" class="flex items-center justify-center w-8 h-8 rounded-lg text-text-dim hover:text-text hover:bg-accent-glow transition-all" title="切换主题" aria-label="切换主题">
60+
<button id="theme-toggle" class="flex items-center justify-center w-8 h-8 rounded-lg text-text-dim hover:text-text hover:bg-accent-glow transition-all" title={locale === "en" ? "Toggle theme" : "切换主题"} aria-label={locale === "en" ? "Toggle theme" : "切换主题"}>
6161
<svg id="icon-moon" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
6262
<svg id="icon-sun" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" class="hidden"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg>
6363
</button>
@@ -89,10 +89,10 @@ const langSwitch = locale === "en"
8989
</svg>
9090
GitHub
9191
</a>
92-
<button id="mobile-theme-toggle" class="flex items-center gap-1.5 text-text-dim text-sm hover:text-text" aria-label="切换主题">
92+
<button id="mobile-theme-toggle" class="flex items-center gap-1.5 text-text-dim text-sm hover:text-text" aria-label={locale === "en" ? "Toggle theme" : "切换主题"}>
9393
<svg class="mobile-icon-moon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
9494
<svg class="mobile-icon-sun hidden" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg>
95-
<span class="mobile-theme-label">切换主题</span>
95+
<span class="mobile-theme-label">{locale === "en" ? "Toggle theme" : "切换主题"}</span>
9696
</button>
9797
<a href={langSwitch.href}
9898
class="text-sm text-text-muted hover:text-text">
@@ -135,19 +135,20 @@ const langSwitch = locale === "en"
135135
var mobileMoonIcon = document.querySelector('.mobile-icon-moon');
136136
var mobileSunIcon = document.querySelector('.mobile-icon-sun');
137137
var mobileThemeLabel = document.querySelector('.mobile-theme-label');
138+
var themeLabel = document.documentElement.lang === 'en' ? 'Toggle theme' : '切换主题';
138139
function updateIcons(isDark) {
139140
if (isDark) {
140141
moonIcon.classList.add('hidden');
141142
sunIcon.classList.remove('hidden');
142143
if (mobileMoonIcon) mobileMoonIcon.classList.add('hidden');
143144
if (mobileSunIcon) mobileSunIcon.classList.remove('hidden');
144-
if (mobileThemeLabel) mobileThemeLabel.textContent = '切换主题';
145+
if (mobileThemeLabel) mobileThemeLabel.textContent = themeLabel;
145146
} else {
146147
moonIcon.classList.remove('hidden');
147148
sunIcon.classList.add('hidden');
148149
if (mobileMoonIcon) mobileMoonIcon.classList.remove('hidden');
149150
if (mobileSunIcon) mobileSunIcon.classList.add('hidden');
150-
if (mobileThemeLabel) mobileThemeLabel.textContent = '切换主题';
151+
if (mobileThemeLabel) mobileThemeLabel.textContent = themeLabel;
151152
}
152153
}
153154
function toggleTheme() {

src/components/SectionHeader.astro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ interface Props {
77
title: string;
88
href: string;
99
accentColor?: "accent" | "pink";
10+
locale?: "zh" | "en";
1011
}
1112
12-
const { title, href, accentColor = "accent" } = Astro.props;
13+
const { title, href, accentColor = "accent", locale = "zh" } = Astro.props;
14+
import { t } from "../i18n/utils.ts";
1315
const accentClass = accentColor === "pink" ? "bg-pink" : "bg-accent";
1416
---
1517

@@ -19,6 +21,6 @@ const accentClass = accentColor === "pink" ? "bg-pink" : "bg-accent";
1921
{title}
2022
</h2>
2123
<a href={href} class="text-sm text-text-muted hover:text-accent-light transition-colors no-underline">
22-
<slot name="more">更多 →</slot>
24+
<slot name="more">{t("home.more", locale)}</slot>
2325
</a>
2426
</div>

src/pages/en/index.astro

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Card from "../../components/Card.astro";
44
import SectionHeader from "../../components/SectionHeader.astro";
55
import { t } from "../../i18n/utils.ts";
66
import { getCollection } from "astro:content";
7-
import { extractSummary, extractTags, TAG_MAP_EN, formatDateCn } from "../../lib/content.ts";
7+
import { extractSummary, extractTags, TAG_MAP_EN, formatWeekDateEn } from "../../lib/content.ts";
88
99
const allEn = await getCollection("en");
1010
const allDaily = allEn.filter((e) => e.id.startsWith("daily/"));
@@ -22,7 +22,7 @@ function buildCard(article: any, type: "daily" | "weekly") {
2222
return {
2323
type,
2424
title: article.data.title,
25-
date: formatDateCn(article.data.date),
25+
date: type === "weekly" ? formatWeekDateEn(article.data.date) : article.data.date,
2626
summary: extractSummary(article.body),
2727
tags: extractTags(article.body, TAG_MAP_EN),
2828
href: `/en/${article.id.replace(".md", "")}`,
@@ -98,7 +98,7 @@ const recentTopics = topicsList.slice(1, 4).map((a) => ({
9898
<!-- Latest Three -->
9999
{(latestDaily || latestWeekly || latestTopic) && (
100100
<section class="mt-8 sm:mt-12">
101-
<SectionHeader title={t("home.section_latest", "en")} href="/en/industry" />
101+
<SectionHeader title={t("home.section_latest", "en")} href="/en/industry" locale="en" />
102102
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 sm:gap-4">
103103
{latestDaily && (
104104
<Card type={latestDaily.type} title={latestDaily.title} date={latestDaily.date} summary={latestDaily.summary} tags={latestDaily.tags} href={latestDaily.href} locale="en" />
@@ -116,7 +116,7 @@ const recentTopics = topicsList.slice(1, 4).map((a) => ({
116116
<!-- Industry News (Daily + Weekly combined) -->
117117
{(recentDaily.length > 0 || recentWeekly.length > 0) && (
118118
<section class="mt-10 sm:mt-14">
119-
<SectionHeader title={t("home.section_industry", "en")} href="/en/industry" />
119+
<SectionHeader title={t("home.section_industry", "en")} href="/en/industry" locale="en" />
120120
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 sm:gap-4">
121121
{[...recentDaily.map((item) => ({ ...item, type: "daily" as const })), ...recentWeekly.map((item) => ({ ...item, type: "weekly" as const }))]
122122
.sort((a, b) => b.date.localeCompare(a.date))
@@ -131,7 +131,7 @@ const recentTopics = topicsList.slice(1, 4).map((a) => ({
131131
<!-- Recent Discussions -->
132132
{recentTopics.length > 0 && (
133133
<section class="mt-10 sm:mt-14">
134-
<SectionHeader title={t("home.section_discussions", "en")} href="/en/topics" accentColor="pink" />
134+
<SectionHeader title={t("home.section_discussions", "en")} href="/en/topics" accentColor="pink" locale="en" />
135135
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 sm:gap-4">
136136
{recentTopics.map((item) => (
137137
<Card type={item.type} title={item.title} date={item.date} summary={item.summary} tags={item.tags} href={item.href} locale="en" />

src/pages/en/industry/index.astro

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Card from "../../../components/Card.astro";
66
import "../../../styles/global.css";
77
import { t } from "../../../i18n/utils.ts";
88
import { getCollection } from "astro:content";
9-
import { extractSummary, extractTags, TAG_MAP_EN } from "../../../lib/content.ts";
9+
import { extractSummary, extractTags, TAG_MAP_EN, formatWeekDateEn } from "../../../lib/content.ts";
1010
1111
const allEn = await getCollection("en");
1212
const allDaily = allEn.filter((e) => e.id.startsWith("daily/"));
@@ -27,10 +27,10 @@ const items = [
2727
href: `/en/${a.id.replace(".md", "")}`,
2828
})),
2929
...allWeekly.map(a => ({
30-
type: "weekly" as const,
31-
title: a.data.title,
32-
date: a.data.date,
33-
dateRaw: a.data.date,
30+
type: "weekly" as const,
31+
title: a.data.title,
32+
date: formatWeekDateEn(a.data.date),
33+
dateRaw: a.data.date,
3434
summary: extractSummary(a.body),
3535
tags: extractTags(a.body, TAG_MAP_EN),
3636
href: `/en/${a.id.replace(".md", "")}`,

0 commit comments

Comments
 (0)