Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

3.5 KB
Raw Blame
import type { EnglishCatalog } from "./locales/en/index.js";

export const defaultLocale = "en";

/**
 * Shipped UI locales. Native names are endonyms and must stay untranslated
 * in the picker (D073). English names are the stable sort/search labels.
 */
export const supportedLocales = [
  { id: "en", nativeName: "English", englishName: "English" },
  { id: "zh-CN", nativeName: "简体中文", englishName: "Chinese (Simplified)" },
  { id: "zh-TW", nativeName: "繁體中文", englishName: "Chinese (Traditional)" },
  { id: "de", nativeName: "Deutsch", englishName: "German" },
  { id: "es", nativeName: "Español", englishName: "Spanish" },
  { id: "tr", nativeName: "Türkçe", englishName: "Turkish" },
  { id: "fr", nativeName: "Français", englishName: "French" },
  { id: "ko", nativeName: "한국어", englishName: "Korean" },
  { id: "pt-BR", nativeName: "Português (Brasil)", englishName: "Portuguese (Brazil)" },
] as const;

export type AppLocale = (typeof supportedLocales)[number]["id"];
export type AppLanguageSetting = "auto" | AppLocale;
export type { EnglishCatalog };

export function isAppLocale(value: string | null | undefined): value is AppLocale {
  return supportedLocales.some((locale) => locale.id === value);
}

export function isAppLanguageSetting(
  value: string | null | undefined,
): value is AppLanguageSetting {
  return value === "auto" || isAppLocale(value);
}

export function localeInfo(id: AppLocale) {
  return supportedLocales.find((locale) => locale.id === id)!;
}

/** English first, then the other shipped locales by English name. */
export function listedLocales(): (typeof supportedLocales)[number][] {
  const english = supportedLocales.find((locale) => locale.id === "en")!;
  const rest = supportedLocales
    .filter((locale) => locale.id !== "en")
    .slice()
    .sort((a, b) => a.englishName.localeCompare(b.englishName, "en"));
  return [english, ...rest];
}

export function resolveLocale(input?: string | null): AppLocale {
  const raw = (input || "").trim();
  if (!raw) return "en";
  const lower = raw.replaceAll("_", "-").toLowerCase();
  if (
    lower === "zh-tw" ||
    lower.startsWith("zh-tw-") ||
    lower === "zh-hant" ||
    lower.startsWith("zh-hant-") ||
    lower === "zh-hk" ||
    lower.startsWith("zh-hk-") ||
    lower === "zh-mo" ||
    lower.startsWith("zh-mo-")
  ) {
    return "zh-TW";
  }
  if (lower === "zh" || lower.startsWith("zh-")) return "zh-CN";
  if (lower === "tr" || lower.startsWith("tr-")) return "tr";
  if (lower === "de" || lower.startsWith("de-")) return "de";
  if (lower === "es" || lower.startsWith("es-")) return "es";
  if (lower === "fr" || lower.startsWith("fr-")) return "fr";
  if (lower === "ko" || lower.startsWith("ko-")) return "ko";
  if (lower === "pt" || lower.startsWith("pt-")) return "pt-BR";
  const exact = supportedLocales.find((locale) => locale.id.toLowerCase() === lower);
  if (exact) return exact.id;
  const prefix = supportedLocales.find(
    (locale) =>
      locale.id !== "zh-CN" && lower.startsWith(`${locale.id.toLowerCase()}-`),
  );
  if (prefix) return prefix.id;
  return "en";
}

export function flattenCatalog(
  obj: Record<string, unknown>,
  prefix = "",
): Record<string, string> {
  const out: Record<string, string> = {};
  for (const [key, value] of Object.entries(obj)) {
    const path = prefix ? `${prefix}.${key}` : key;
    if (typeof value === "string") {
      out[path] = value;
    } else if (value && typeof value === "object") {
      Object.assign(out, flattenCatalog(value as Record<string, unknown>, path));
    }
  }
  return out;
}