Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

5.7 KB
Raw Blame
import { createI18n } from "vue-i18n";
import en from "./locales/en";
import { safeLocalStorageGet } from "@/lib/backend/safeStorage";
import { persistAppLocale } from "@/lib/app/appAppearance";

export type Locale = "az" | "en" | "es" | "id" | "it" | "ja" | "ko" | "pt-BR" | "pl" | "ru" | "th" | "tr" | "zh-CN" | "zh-TW";
type LocaleMessages = Record<string, unknown>;
type I18nGlobal = {
  locale: { value: Locale };
  setLocaleMessage: (locale: Locale, messages: LocaleMessages) => void;
};

const supportedLocales: Locale[] = ["az", "en", "es", "id", "it", "ja", "ko", "pt-BR", "pl", "ru", "th", "tr", "zh-CN", "zh-TW"];
const defaultLocale: Locale = "en";
const loadedLocales = new Set<Locale>([defaultLocale]);
const localeLoaders: Record<Exclude<Locale, "en">, () => Promise<{ default: LocaleMessages }>> = {
  az: () => import("./locales/az"),
  es: () => import("./locales/es"),
  id: () => import("./locales/id"),
  it: () => import("./locales/it"),
  ja: () => import("./locales/ja"),
  ko: () => import("./locales/ko"),
  "pt-BR": () => import("./locales/pt-BR"),
  pl: () => import("./locales/pl"),
  ru: () => import("./locales/ru"),
  th: () => import("./locales/th"),
  tr: () => import("./locales/tr"),
  "zh-CN": () => import("./locales/zh-CN"),
  "zh-TW": () => import("./locales/zh-TW"),
};

export function normalizeLocale(value: string | null): Locale | null {
  if (value && supportedLocales.includes(value as Locale)) {
    return value as Locale;
  }
  return null;
}

export function localeFromLanguageTag(value: string | null | undefined): Locale | null {
  if (!value) return null;
  const normalized = value.replace("_", "-").toLowerCase();
  if (normalized === "zh" || normalized.startsWith("zh-")) {
    if (normalized.includes("hant") || normalized.startsWith("zh-tw") || normalized.startsWith("zh-hk") || normalized.startsWith("zh-mo")) {
      return "zh-TW";
    }
    return "zh-CN";
  }
  if (normalized === "az" || normalized.startsWith("az-")) return "az";
  if (normalized === "en" || normalized.startsWith("en-")) return "en";
  if (normalized === "es" || normalized.startsWith("es-")) return "es";
  if (normalized === "id" || normalized.startsWith("id-")) return "id";
  if (normalized === "it" || normalized.startsWith("it-")) return "it";
  if (normalized === "ja" || normalized.startsWith("ja-")) return "ja";
  if (normalized === "ko" || normalized.startsWith("ko-")) return "ko";
  if (normalized === "pt" || normalized.startsWith("pt-")) return "pt-BR";
  if (normalized === "pl" || normalized.startsWith("pl-")) return "pl";
  if (normalized === "ru" || normalized.startsWith("ru-")) return "ru";
  if (normalized === "tr" || normalized.startsWith("tr-")) return "tr";
  if (normalized === "th" || normalized.startsWith("th-")) return "th";
  return null;
}

export function detectLocaleFromLanguages(languages: readonly string[]): Locale {
  for (const language of languages) {
    const locale = normalizeLocale(language) ?? localeFromLanguageTag(language);
    if (locale) return locale;
  }
  return defaultLocale;
}

function detectUserLocale(): Locale {
  try {
    const languages = globalThis.navigator?.languages;
    const language = globalThis.navigator?.language;
    const candidates = Array.isArray(languages) ? [...languages] : [];
    if (language) candidates.push(language);
    return detectLocaleFromLanguages(candidates);
  } catch {
    return defaultLocale;
  }
}

const savedLocale = normalizeLocale(safeLocalStorageGet("dbx-locale"));
const initialLocale = savedLocale ?? detectUserLocale();
let persistedLocale = initialLocale;
let localeRequestId = 0;

const i18n = createI18n({
  legacy: false,
  locale: initialLocale,
  fallbackLocale: defaultLocale,
  messages: {
    en,
  },
});
const i18nGlobal = i18n.global as unknown as I18nGlobal;

export async function loadLocaleMessages(locale: Locale) {
  if (loadedLocales.has(locale)) return;
  const loader = localeLoaders[locale as Exclude<Locale, "en">];
  if (!loader) return;
  const messages = await loader();
  i18nGlobal.setLocaleMessage(locale, messages.default);
  loadedLocales.add(locale);
}

export async function loadSavedLocale() {
  await loadLocaleMessages(initialLocale);
  persistAppLocale(initialLocale);
}

async function applyTransientLocale(locale: Locale) {
  const requestId = ++localeRequestId;
  await loadLocaleMessages(locale);
  if (requestId !== localeRequestId) return;
  i18nGlobal.locale.value = locale;
}

// Temporary previews deliberately skip persistence and backend synchronization.
// The request id makes a slow locale import unable to overwrite a newer hover,
// a restore, or an explicitly selected locale.
export async function previewLocale(locale: Locale) {
  await applyTransientLocale(locale);
}

export async function restoreLocalePreview() {
  await applyTransientLocale(persistedLocale);
}

export async function setLocale(locale: Locale) {
  // Record a user selection before awaiting a lazy locale import. A following
  // close/restore must use the selected locale rather than an older preview.
  persistedLocale = locale;
  ++localeRequestId;
  // An explicit selection is durable immediately; only the visible locale
  // waits for its lazy message bundle. Preview paths never reach this branch.
  persistAppLocale(locale);
  await loadLocaleMessages(locale);
  // A later explicit selection wins; hover/restore requests must not undo it.
  if (persistedLocale !== locale) return;
  i18nGlobal.locale.value = locale;
}

export function currentLocale(): Locale {
  return i18nGlobal.locale.value;
}

export function nextLocale(current: Locale): Locale {
  const index = supportedLocales.indexOf(current);
  const nextIndex = index === -1 ? 0 : (index + 1) % supportedLocales.length;
  return supportedLocales[nextIndex];
}

export default i18n;