Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions gui/src/i18n/interpolate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { TKey } from "./en";

export type Vars = Record<string, string | number>;
export type TFn = (key: TKey, vars?: Vars) => string;

export function interpolate(s: string, vars?: Vars): string {
if (!vars) return s;
let out = s;
for (const k of Object.keys(vars)) out = out.split(`{${k}}`).join(String(vars[k]));
return out;
}
13 changes: 3 additions & 10 deletions gui/src/i18n/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { ko } from "./ko";
import { zh } from "./zh";
import { ru } from "./ru";
import { ja } from "./ja";
import type { TFn } from "./interpolate";

export { interpolate, type TFn, type Vars } from "./interpolate";

export type Locale = "en" | "de" | "ko" | "zh" | "ru" | "ja";
export type { TKey };
Expand Down Expand Up @@ -47,20 +50,10 @@ export function setActiveLocale(locale: Locale): void {
activeLocale = locale;
}

export type Vars = Record<string, string | number>;
export type TFn = (key: TKey, vars?: Vars) => string;

export interface I18nContextValue { locale: Locale; setLocale: (l: Locale) => void; t: TFn }

export const I18nContext = createContext<I18nContextValue | null>(null);

export function interpolate(s: string, vars?: Vars): string {
if (!vars) return s;
let out = s;
for (const k of Object.keys(vars)) out = out.split(`{${k}}`).join(String(vars[k]));
return out;
}

export function useI18n(): I18nContextValue {
const ctx = useContext(I18nContext);
if (!ctx) throw new Error("useI18n must be used within LanguageProvider");
Expand Down
2 changes: 1 addition & 1 deletion tests/provider-workspace-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
providerIconSrc,
} from "../gui/src/provider-icons";
import { en } from "../gui/src/i18n/en";
import { interpolate, type TFn } from "../gui/src/i18n/shared";
import { interpolate, type TFn } from "../gui/src/i18n/interpolate";
import {
bucketPresets,
filterPresets,
Expand Down
2 changes: 1 addition & 1 deletion tests/tencent-siliconflow-providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PROVIDER_REGISTRY } from "../src/providers/registry";
import { routeModel } from "../src/router";
import type { OcxConfig } from "../src/types";
import { en } from "../gui/src/i18n/en";
import { interpolate, type TFn } from "../gui/src/i18n/shared";
import { interpolate, type TFn } from "../gui/src/i18n/interpolate";
import { formatProviderDisplayName, isCatalogProviderId } from "../gui/src/provider-icons";

const englishT: TFn = (key, vars) => interpolate(en[key], vars);
Expand Down
2 changes: 1 addition & 1 deletion tests/volcengine-providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { routeModel } from "../src/router";
import type { OcxConfig } from "../src/types";
import { buildProviderPostBody } from "../gui/src/provider-payload";
import { en } from "../gui/src/i18n/en";
import { interpolate, type TFn } from "../gui/src/i18n/shared";
import { interpolate, type TFn } from "../gui/src/i18n/interpolate";
import { formatProviderDisplayName, isCatalogProviderId } from "../gui/src/provider-icons";
import { withTestTranslatorBudget } from "./helpers/translator-budget";

Expand Down
Loading