docs/i18n.md
This document describes the i18n system for Dyad. The goal is to support multiple languages across the Electron renderer and main process with type-safe translation keys, minimal boilerplate, and incremental adoption.
react-i18next + i18nextUse react-i18next (the de facto standard for React i18n) rather than building a custom solution.
Rationale:
i18next)npm install i18next react-i18next
No additional plugins are needed initially. Translation files are bundled with the app (not fetched remotely), so no HTTP backend is required.
src/
i18n/
index.ts # i18next initialization
types.ts # Generated types for key autocompletion
locales/
en/
common.json # Shared strings (buttons, labels, generic)
settings.json # Settings page
chat.json # Chat UI
home.json # Home page
errors.json # Error/toast messages
zh-CN/
common.json
settings.json
...
ja/
common.json
...
Split translations by feature area (namespace = one JSON file). This keeps files manageable and allows lazy-loading namespaces for routes that aren't immediately visible.
| Namespace | Scope |
|---|---|
common | Buttons, generic labels, confirmations, nav |
settings | All settings page sections |
chat | Chat input, messages, streaming indicators |
home | Home page, app list, templates |
errors | Toast messages, error dialogs, validation |
hub | Hub/library/marketplace |
integrations | GitHub, Supabase, Neon, Vercel connectors |
Standard flat-key JSON with nesting where it aids organization:
// en/settings.json
{
"title": "Settings",
"general": {
"title": "General",
"language": "Language",
"zoom": "Zoom Level",
"theme": "Theme"
},
"ai": {
"title": "AI",
"model": "Model",
"provider": "Provider",
"apiKey": "API Key"
},
"agent": {
"toolPermissions": "Configure permissions for Agent built-in tools.",
"permissionOption": {
"ask": "Ask",
"always": "Always allow",
"never": "Never allow"
}
}
}
// en/common.json
{
"save": "Save",
"cancel": "Cancel",
"delete": "Delete",
"confirm": "Confirm",
"loading": "Loading...",
"copyToClipboard": "Copy to clipboard",
"copied": "Copied!",
"itemCount_one": "{{count}} item",
"itemCount_other": "{{count}} items"
}
Pluralization uses i18next's built-in suffix convention (_one, _other, _zero, etc.), which handles most languages. For languages with complex plural rules (e.g., Arabic, Polish), i18next resolves the correct form automatically.
// src/i18n/index.ts
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
// Import all locale bundles (bundled with the app)
import enCommon from "./locales/en/common.json";
import enSettings from "./locales/en/settings.json";
import enChat from "./locales/en/chat.json";
import enHome from "./locales/en/home.json";
import enErrors from "./locales/en/errors.json";
// ... other languages imported similarly
const resources = {
en: {
common: enCommon,
settings: enSettings,
chat: enChat,
home: enHome,
errors: enErrors,
},
// "zh-CN": { ... },
// "ja": { ... },
};
i18n.use(initReactI18next).init({
resources,
lng: "en", // Default; overridden by user setting on startup
fallbackLng: "en",
defaultNS: "common",
ns: ["common", "settings", "chat", "home", "errors"],
interpolation: {
escapeValue: false, // React already escapes
},
});
export default i18n;
Import src/i18n/index.ts at the app entry point (src/main.tsx or equivalent) before rendering.
useTranslation hookimport { useTranslation } from "react-i18next";
function AgentToolsSettings() {
const { t } = useTranslation("settings");
return (
<div>
<p className="text-sm text-muted-foreground">
{t("agent.toolPermissions")}
</p>
<SelectItem value="ask">{t("agent.permissionOption.ask")}</SelectItem>
<SelectItem value="always">
{t("agent.permissionOption.always")}
</SelectItem>
</div>
);
}
const { t } = useTranslation(["settings", "common"]);
// Keys from the first namespace work directly
t("general.title"); // → "General" (from settings)
// Keys from other namespaces use prefix
t("common:save"); // → "Save" (from common)
t("errors:fileNotFound", { path: "/some/file.txt" });
// "File not found: {{path}}" → "File not found: /some/file.txt"
Use the Trans component for strings that contain JSX:
import { Trans } from "react-i18next";
<Trans i18nKey="home:welcome" t={t}>
Welcome to <strong>Dyad</strong>
</Trans>;
Create a type declaration so that t("...") calls get autocompletion and compile-time checking of keys.
// src/i18n/types.ts
import "i18next";
import type enCommon from "./locales/en/common.json";
import type enSettings from "./locales/en/settings.json";
import type enChat from "./locales/en/chat.json";
import type enHome from "./locales/en/home.json";
import type enErrors from "./locales/en/errors.json";
declare module "i18next" {
interface CustomTypeOptions {
defaultNS: "common";
resources: {
common: typeof enCommon;
settings: typeof enSettings;
chat: typeof enChat;
home: typeof enHome;
errors: typeof enErrors;
};
}
}
This gives full autocomplete for t("settings:general.title") etc., and TypeScript errors for invalid keys.
Add a language field to UserSettingsSchema in src/lib/schemas.ts:
// In UserSettingsSchema
language: z.string().default("en"),
Add a language selector to the General settings section (similar to the existing zoom selector):
function LanguageSelector() {
const { t } = useTranslation("settings");
const [settings, setSettings] = useSettings();
const languages = [
{ value: "en", label: "English" },
{ value: "zh-CN", label: "简体中文" },
{ value: "ja", label: "日本語" },
{ value: "ko", label: "한국어" },
{ value: "es", label: "Español" },
{ value: "fr", label: "Français" },
{ value: "de", label: "Deutsch" },
];
const handleChange = (value: string) => {
i18n.changeLanguage(value);
setSettings({ ...settings, language: value });
};
return (
<Select value={settings.language} onValueChange={handleChange}>
{languages.map((lang) => (
<SelectItem key={lang.value} value={lang.value}>
{lang.label}
</SelectItem>
))}
</Select>
);
}
Language labels are shown in their native script (not translated) so users can always find their language regardless of the current UI language.
On app startup, read the persisted language from user settings and call i18n.changeLanguage(savedLanguage) before the first render. This can be done in the settings loading hook or in src/i18n/index.ts by reading the setting synchronously.
Some user-facing strings originate in the main process (e.g., native dialogs, menu items, error messages sent over IPC). For these:
i18next directly (without react-i18next) in main process code.src/main/i18n.ts.// src/main/i18n.ts
import i18n from "i18next";
import enErrors from "../i18n/locales/en/errors.json";
const mainI18n = i18n.createInstance();
mainI18n.init({
resources: { en: { errors: enErrors } },
lng: "en",
fallbackLng: "en",
defaultNS: "errors",
});
export default mainI18n;
When the user changes language in the renderer, send the new language to the main process via IPC so it can call mainI18n.changeLanguage(lng).
Use the browser's Intl API (already available in Electron's Chromium) rather than adding a formatting library:
// Utility in src/i18n/format.ts
export function formatDate(date: Date, locale: string): string {
return new Intl.DateTimeFormat(locale, {
dateStyle: "medium",
timeStyle: "short",
}).format(date);
}
export function formatNumber(value: number, locale: string): string {
return new Intl.NumberFormat(locale).format(value);
}
export function formatRelativeTime(date: Date, locale: string): string {
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
const diffMs = date.getTime() - Date.now();
const diffDays = Math.round(diffMs / (1000 * 60 * 60 * 24));
if (Math.abs(diffDays) < 1) {
const diffHours = Math.round(diffMs / (1000 * 60 * 60));
return rtf.format(diffHours, "hour");
}
return rtf.format(diffDays, "day");
}
The existing date-fns dependency also supports locale-aware formatting if more complex date operations are needed.
Migrating all strings at once is impractical. Instead, adopt incrementally:
src/i18n/ directory structure, initialize i18next.language to UserSettings schema.en/common.json with the most common shared strings (button labels, generic terms).en/settings.json.t() calls.src/lib/toast.tsx and callers.zh-CN) to validate the full loop.en/*.json first.Add a CI check that verifies all keys present in en/*.json exist in every other locale. Missing keys produce warnings (not errors, since fallback handles them), making it easy to see translation coverage.
Consider i18next-parser to scan source files for t("...") calls and auto-generate/update the English JSON files. This catches strings that were added in code but not in the JSON.
// package.json script
"i18n:extract": "i18next-parser 'src/**/*.{ts,tsx}'"
| Convention | Example |
|---|---|
| Namespace maps to feature area | settings, chat, common |
| Nested keys use dot notation | settings:general.title |
| Action labels are imperative | "save": "Save", "delete": "Delete" |
| Descriptions are sentence case | "toolPermissions": "Configure permissions..." |
| Plurals use i18next suffixes | _one, _other |
Interpolation uses {{var}} | "hello": "Hello, {{name}}" |
| No string concatenation | Use interpolation instead of t("a") + value + t("b") |