Files
GitLite/src/lib/i18n.svelte.ts
T
Christoph 3e87c8f6a9 feat(confirm): centralize confirmation dialogs and add i18n
Introduce a generic ConfirmDialog and a promise-based requestConfirmation API in
App.svelte so callers can await user responses instead of using window.confirm.
Provide helper builders (branchDeleteConfirmRequest, discardConfirmRequest) to
create dialog content for common cases. Many call sites were switched to use
requestConfirmation and now render the in-app ConfirmDialog; the previous
specialized confirm components (BranchDeleteConfirmDialog, DiscardConfirmDialog)
were removed.

Add lightweight i18n support (setLanguage, t()) and new messages/i18n modules,
and replace hardcoded English strings in several components (e.g. AiSettingsPage,
BlameDialog and many confirmation prompts) with translated keys.

Summary of effects:
- Replaces native window.confirm with awaitable in-app ConfirmDialog dialogs.
- Centralizes confirmation UI and content construction in App.svelte.
- Adds i18n plumbing and updates UI text to use t().
- Removes two specialized confirm dialog components and adds src/lib/components/ConfirmDialog.svelte.
2026-09-17 21:41:35 +02:00

46 lines
1.5 KiB
TypeScript

/**
* Central translation helper.
*
* The active language lives in module scope so components can call `t(...)`
* without threading a `language` prop through every layer. Reading `t(...)`
* inside markup registers a dependency on `activeLanguage`, so switching the
* language in settings re-renders every translated string.
*/
import { messages, type MessageEntry, type MessageKey } from "./messages";
import type { AppLanguage } from "./types";
let activeLanguage = $state<AppLanguage>("en");
export function setLanguage(next: AppLanguage) {
activeLanguage = next;
}
export function getLanguage(): AppLanguage {
return activeLanguage;
}
export function isGermanLanguage(): boolean {
return activeLanguage === "de";
}
export type TranslationValues = Record<string, string | number>;
/** Look up `key` in the active language and fill in `{placeholders}`. */
export function t(key: MessageKey, values?: TranslationValues): string {
const entry: MessageEntry | undefined = messages[key];
let text: string = entry ? entry[activeLanguage] ?? entry.en : key;
if (values) {
for (const [name, value] of Object.entries(values)) {
text = text.split(`{${name}}`).join(String(value));
}
}
return text;
}
/** Pick the singular or plural key based on `count` and pass it as `{count}`. */
export function tPlural(one: MessageKey, many: MessageKey, count: number, values?: TranslationValues): string {
return t(count === 1 ? one : many, { count, ...values });
}