- chore: correct stale comments and contributor commands - docs: sync docs/00–16 to site v1.1 (suite 2.0) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 KiB
07 — Internationalization (EN/VI)
1. Model
Two locales, equal content, no machine translation. English is the
default and canonical locale served unprefixed; Vietnamese mirrors every
page under /vi/… (decision D13). A third locale (JP) is a v1.2 candidate —
the architecture below adds a locale by adding one dictionary + data fields,
nothing structural.
Three i18n layers:
| Layer | Lives in | Examples |
|---|---|---|
| UI strings | src/i18n/en.json, vi.json (163 keys each, 2026-10-21) |
buttons, badges, security-line copy, gate copy, error pages |
| Content data | each app file (summary.en/.vi), category registry (label.en/.vi) |
app summaries, category names |
| Legal | src/components/legal/*.astro, EN and VI in one file per document (docs/14 Part 3) |
Disclaimer, Privacy, Terms… |
Error, 404 and offline pages are the exception to "one page per locale":
each is one bilingual document (an EN and a lang="vi" panel), because
an error can be reached from either locale — and /offline with no network
at all — so there is no safe moment to pick one (docs/06 §5).
2. Routing
Astro built-in i18n (config in docs/02 §4): defaultLocale: 'en',
prefixDefaultLocale: false.
-
/apps/7-zip(EN) ⇄/vi/apps/7-zip(VI) — slugs never localize. -
VI pages are real files under
src/pages/vi/**re-exporting shared page components withlocale="vi"— thin wrappers, logic lives once. -
getStaticPathsis shared, never copy-pasted. Astro requires it in each dynamic route file, and there are two per collection (EN + VI). The path builders are defined once insrc/lib/paths.ts— one factory per collection, instantiated per locale — and re-exported:// src/pages/vi/apps/[slug].astro — the EN twin re-exports appPathsEn export { appPathsVi as getStaticPaths } from '../../../lib/paths';Duplicating the logic means a filter added to one locale silently skips the other, which breaks the parity guarantee CI cannot check (docs/10 §7).
-
No automatic locale redirect. Bots and users always get the URL they asked for. Instead: a one-time dismissible banner — "Trang này có bản tiếng Việt." with [Chuyển sang tiếng Việt] [Giữ tiếng Anh], always in Vietnamese (
t('vi', …)) because it only ever shows to a Vietnamese browser — renders whennavigator.languagestarts withvi∧ current locale is EN ∧sh:langunset. Switching writes the chosen locale; dismissing writesen; either way the banner never returns.sh:langdoes nothing else — it never redirects (src/lib/lang.ts).
3. Dictionaries & typed helper
Flat dot-keys, alphabetized, identical key sets:
// src/i18n/en.json (excerpt)
{
"card.checked": "checked {date}",
"card.listed": "listed {date}",
"card.security.clean": "No known warnings",
"card.security.unverified": "Not yet verified",
"detail.developer": "Developer",
"filters.clear": "Clear all",
"filters.show": "Show {n} apps",
"gate.accept": "I understand and accept",
"search.placeholder": "Search apps… ( / )"
}
// src/i18n/index.ts
import en from './en.json';
import vi from './vi.json';
import { type Locale } from '../lib/locale'; // routing lives there
export type Key = keyof typeof en; // en is the source of truth
const dict: Record<Locale, Record<Key, string>> = { en, vi };
export function t(locale: Locale, key: Key,
vars: Record<string, string | number> = {}): string {
let s: string = dict[locale][key] ?? dict.en[key] ?? key;
for (const [k, v] of Object.entries(vars)) s = s.replaceAll(`{${k}}`, String(v));
return s;
}
/** "1 app" / "326 apps" / "326 ứng dụng" — plural rule + digit grouping. */
export function appCount(locale: Locale, n: number): string {
const lang = locale === 'vi' ? 'vi-VN' : 'en-GB';
const one = new Intl.PluralRules(lang).select(n) === 'one';
return t(locale, one ? 'apps.count.one' : 'apps.count',
{ n: new Intl.NumberFormat(lang).format(n) });
}
appCount() is the one way a count reaches a page, so no page can print
"1 apps". Islands never import the dictionaries (that would bundle every
key): they receive their strings as props, and where they must pluralise on
the client — the grid count, the filter sheet's "Show N apps" — the server
leaves one template per plural category and the island picks with
Intl.PluralRules(<html lang>) (docs/05 §A3). Routing helpers
(localePath, localePair, canonicalPath) live in src/lib/locale.ts,
which imports no dictionary, so islands can use them for free;
src/i18n/index.ts re-exports them.
CI parity check — its own script, scripts/check-i18n-parity.mjs, run by
pnpm i18n:check (and as the second step of pnpm build). It is
deliberately not folded into validate-data.mjs: that script is
triggered by src/data/**, so an edit touching only src/i18n/*.json
would skip the check entirely. Ported from
E:\qoute\scripts\check-i18n-parity.ts (flatten → bidirectional key diff →
error list → non-zero exit), and since extended: an empty object counts as
a key, {placeholder} sets must match between locales (a dropped one
would render literal braces), keys must be in alphabetical order in
every dictionary, and a UTF-8 BOM is stripped instead of crashing
JSON.parse.
vi.json must contain exactly the keys of en.json — missing or extra
keys fail the build (hard rule 6). No exceptions, including metadata or
glossary keys (§9). Runtime fallback to EN exists only as a belt-and-braces
guard.
4. Locale-aware rendering rules
- Every page reads
Astro.currentLocale; components takelocaleas a prop — no global mutable locale. - Dates:
new Intl.DateTimeFormat(locale === 'vi' ? 'vi-VN' : 'en-GB', { dateStyle: 'medium', timeZone: 'UTC' })—en-GBchosen for unambiguous15 Jul 2026; UTC so aYYYY-MM-DDnever shifts a day. - Numbers (counts):
Intl.NumberFormatper locale; plurals viaappCount()(§3) or the per-plural templates islands use. - Sorting app names:
compareNames()—Intl.Collator('en', { sensitivity: 'base', numeric: true })overnormalizeViet(name)— identical order in both locales, and independent of the visitor's browser locale and the build machine's ICU default (docs/05 §A3). A barelocaleCompare()let a Czech-locale browser reorder 81 of 326 cards. - Category chips, filter labels and error copy are localized too; only the language switch's endonyms and the VI suggestion banner are deliberately fixed-language.
<html lang>=en/vi; embedded other-language strings carrylang— the VI panel on bilingual error pages, the VI suggestion banner on EN pages, the endonyms in the switcher.
5. Language switcher (ShLangSwitch)
A segmented EN | VI control in the header (client:idle). The current
language is a <span aria-current="true"> (the filled segment), the other
a link with hreflang and lang. Accessible names keep the visible code
and append the endonym, each inside an element with the matching lang:
"EN — English" and "VI — Tiếng Việt" — never an aria-label that
replaces "EN"/"VI", so voice-control users can say what they see (WCAG
2.5.3).
Behavior: the counterpart URL is server-rendered from the same
localePair(path) result Base.astro writes into the hreflang alternates,
so the switcher href always equals the hreflang alternate (before v1.1 the
island guessed until it hydrated, and every VI page shipped HTML marking
English as current). On mount it appends location.search +
location.hash, so a filtered /apps?… view survives a switch (docs/05
§A3). Clicking persists the choice to sh:lang and is an ordinary full
navigation — there is no <ClientRouter /> (docs/02 §6). Every page has a
counterpart except the single bilingual error/offline pages, which emit no
alternates (alternates={false}); there the switcher leads to the other
locale's home.
6. SEO
Every page emits:
<link rel="alternate" hreflang="en" href="https://softharbor.net/apps/7-zip" />
<link rel="alternate" hreflang="vi" href="https://softharbor.net/vi/apps/7-zip" />
<link rel="alternate" hreflang="x-default" href="https://softharbor.net/apps/7-zip" />
@astrojs/sitemap i18n option (docs/02 §4) mirrors the same pairs into the
sitemap. Canonical = self per locale (no cross-locale canonicals). Meta
description = the locale's summary on app pages, a per-page dictionary
string elsewhere (docs/16 §7). Error and offline pages emit no
alternates: advertising them pointed crawlers at /vi/404 and
/vi/offline, which do not exist.
Never pass Astro.url.pathname straight into a canonical or an alternate.
With build.format: 'file' — which the trailing-slash fix requires (docs/02
§4) — it is the emitted file name: /apps.html, /legal/disclaimer.html.
Feeding that through shipped 52 pages whose canonical disagreed with both the
sitemap and the URL actually being served, which is precisely the
duplicate-content signal docs/16 exists to prevent. Everything user-facing goes
through canonicalPath() in src/lib/locale.ts (found and fixed in M4;
regression-tested in src/i18n/index.test.ts).
7. Search across locales
The index stores summaryEn and summaryVi for every app (docs/05
§A2), both normalized by A1. Consequence: a user on the EN site typing
trinh duyet still finds browsers, and an VI user typing screenshot
finds ShareX — queries work in either language regardless of UI locale.
This is deliberate; do not "optimize" it away by indexing only the active
locale.
8. Legal locale policy
Legal drafts ship in both languages (docs/14 §3). The English text is canonical; each VI page carries the fixed notice: "Bản tiếng Việt chỉ nhằm mục đích tham khảo; nếu có khác biệt, bản tiếng Anh được ưu tiên áp dụng." The legal-gate dialog shows the active locale's summary with links to that locale's legal pages.
9. Translation workflow & quality bar
VI is written by the maintainer (native), not machine-translated. Style: xưng hô trung tính ("bạn"), thuật ngữ giữ nguyên tiếng Anh khi đã thông dụng (open source, subscription → "thuê bao" ok, "mua đứt" for one-time), summaries ≤ 160 chars in both locales (schema-enforced).
The glossary lives here, in this document — never inside vi.json. §3's
parity rule is absolute: vi.json must contain exactly en.json's keys, so
a _glossary key would fail the very check it was meant to support, and JSON
has no comments to hide it in. Keep the dictionaries pure data.
| EN | VI |
|---|---|
| free | miễn phí |
| one-time purchase | mua đứt một lần |
| checked | đã kiểm tra |
| open source | open source (kept in English) |
| subscription | thuê bao |
| developer | nhà phát triển |
| download | tải về |
| browser | trình duyệt |