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。

4.4 KB
Raw Blame
import { describe, expect, it } from "vitest";
import az from "../locales/az";
import en from "../locales/en";
import es from "../locales/es";
import id from "../locales/id";
import itLocale from "../locales/it";
import ja from "../locales/ja";
import ko from "../locales/ko";
import ptBR from "../locales/pt-BR";
import ru from "../locales/ru";
import tr from "../locales/tr";
import th from "../locales/th";
import zhCN from "../locales/zh-CN";
import zhTW from "../locales/zh-TW";

// English is the source of truth for message parameters: every `t(key, params)`
// call site passes the placeholder names declared by `en`, so a locale that
// introduces an interpolation placeholder English does not declare renders the
// raw `{name}` text to the user (Korea's `grid.jumpToPage` shipped as
// "{page}페이지로 이동" because the aria-label is called without arguments).
//
// Importing `../locales/<name>` yields `withEnglishFallback({...})`, i.e. the
// locale merged on top of `en`. Keys whose merged value equals the English value
// come from the fallback and are skipped, so only locale-declared values are
// inspected here.
const locales: Array<[string, Record<string, unknown>]> = [
  ["az", az as Record<string, unknown>],
  ["es", es as Record<string, unknown>],
  ["id", id as Record<string, unknown>],
  ["it", itLocale as Record<string, unknown>],
  ["ja", ja as Record<string, unknown>],
  ["ko", ko as Record<string, unknown>],
  ["pt-BR", ptBR as Record<string, unknown>],
  ["ru", ru as Record<string, unknown>],
  ["tr", tr as Record<string, unknown>],
  ["th", th as Record<string, unknown>],
  ["zh-CN", zhCN as Record<string, unknown>],
  ["zh-TW", zhTW as Record<string, unknown>],
];

function flattenMessages(value: unknown, prefix = "", acc = new Map<string, string>()): Map<string, string> {
  if (value === null || typeof value !== "object" || Array.isArray(value)) {
    acc.set(prefix, String(value));
    return acc;
  }
  for (const [key, child] of Object.entries(value as Record<string, unknown>)) {
    flattenMessages(child, prefix ? `${prefix}.${key}` : key, acc);
  }
  return acc;
}

/**
 * Collects vue-i18n named interpolation placeholders (`{name}`) from a message.
 * Literal-brace escapes (`{'text'}` / `{'{'}`) are skipped: they render their
 * content verbatim, so they are not parameters.
 */
function interpolationPlaceholders(message: string): Set<string> {
  const found = new Set<string>();
  let index = 0;
  while (index < message.length) {
    if (message[index] !== "{") {
      index += 1;
      continue;
    }
    if (message.startsWith("{'", index)) {
      const literalEnd = message.indexOf("'}", index + 2);
      if (literalEnd === -1) break;
      index = literalEnd + 2;
      continue;
    }
    const end = message.indexOf("}", index + 1);
    if (end === -1) break;
    const inner = message.slice(index + 1, end);
    if (inner && !inner.startsWith("'")) found.add(inner);
    index = end + 1;
  }
  return found;
}

const englishMessages = flattenMessages(en as Record<string, unknown>);

describe("locale interpolation placeholder parity", () => {
  it.each(locales)("%s localizes the routine rename preview", (_name, messages) => {
    const key = "objects.routineRenamePreview";
    const localized = flattenMessages(messages).get(key);
    expect(localized).toBeDefined();
    expect(localized).not.toBe(englishMessages.get(key));
  });

  it.each(locales)("%s localizes the table rename repair flow", (_name, messages) => {
    const localizedMessages = flattenMessages(messages);
    for (const key of ["objects.tableRenameCoverage", "objects.tableRenameObjects", "objects.tableRenameDraft", "objects.tableRenameSuccess"]) {
      expect(localizedMessages.get(key), key).toBeDefined();
      expect(localizedMessages.get(key), key).not.toBe(englishMessages.get(key));
    }
  });

  it.each(locales)("%s declares no placeholder English does not", (_name, messages) => {
    const offenders: string[] = [];
    for (const [key, value] of flattenMessages(messages)) {
      const english = englishMessages.get(key);
      if (english === undefined || value === english) continue;
      const allowed = interpolationPlaceholders(english);
      const extra = [...interpolationPlaceholders(value)].filter((placeholder) => !allowed.has(placeholder));
      if (extra.length > 0) offenders.push(`${key}: {${extra.join("}, {")}} in "${value}"`);
    }
    expect(offenders).toEqual([]);
  });
});