Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
export const LOCALE_STORAGE_KEY = "nanobot.locale";
export const supportedLocales = [
{ code: "en", nativeLabel: "English" },
{ code: "zh-CN", nativeLabel: "简体中文" },
{ code: "zh-TW", nativeLabel: "繁體中文" },
{ code: "fr", nativeLabel: "Français" },
{ code: "ja", nativeLabel: "日本語" },
{ code: "ko", nativeLabel: "한국어" },
{ code: "es", nativeLabel: "Español" },
{ code: "pt-BR", nativeLabel: "Português (Brasil)" },
{ code: "vi", nativeLabel: "Tiếng Việt" },
{ code: "id", nativeLabel: "Bahasa Indonesia" },
] as const;
export type SupportedLocale = (typeof supportedLocales)[number]["code"];
export const defaultLocale: SupportedLocale = "en";
export const fallbackLocale: SupportedLocale = "en";
export function normalizeLocale(
input: string | null | undefined,
): SupportedLocale {
if (!input) return defaultLocale;
const trimmed = input.trim();
if (!trimmed) return defaultLocale;
const exact = supportedLocales.find((locale) => locale.code === trimmed);
if (exact) return exact.code;
const lower = trimmed.toLowerCase();
if (lower === "zh" || lower.startsWith("zh-cn") || lower.startsWith("zh-sg")) {
return "zh-CN";
}
if (
lower.startsWith("zh-tw") ||
lower.startsWith("zh-hk") ||
lower.startsWith("zh-mo") ||
lower.startsWith("zh-hant")
) {
return "zh-TW";
}
if (lower === "pt" || lower.startsWith("pt-")) {
return "pt-BR";
}
const base = lower.split("-")[0];
const baseMatch = supportedLocales.find(
(locale) => locale.code.toLowerCase() === base,
);
return baseMatch?.code ?? defaultLocale;
}
function readStoredLocale(): SupportedLocale | null {
if (typeof window === "undefined") return null;
try {
const raw = window.localStorage.getItem(LOCALE_STORAGE_KEY);
return raw ? normalizeLocale(raw) : null;
} catch {
return null;
}
}
export function resolveInitialLocale(): SupportedLocale {
return readStoredLocale() ?? defaultLocale;
}
export function persistLocale(locale: SupportedLocale): void {
if (typeof window === "undefined") return;
try {
window.localStorage.setItem(LOCALE_STORAGE_KEY, locale);
} catch {
// ignore storage errors
}
}
export function applyDocumentLocale(locale: SupportedLocale): void {
if (typeof document === "undefined") return;
document.documentElement.lang = locale;
}