mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-06-24 23:43:50 +08:00
- 新增共享六语言词典、前端 i18n 运行时与语言设置入口 - 推进连接、驱动、数据网格、查询、AI、Redis、表设计等模块文案本地化 - 补充 raw 边界、SQL/驱动/更新场景测试与 i18n 扫描工具
25 lines
897 B
TypeScript
25 lines
897 B
TypeScript
import { DEFAULT_LANGUAGE, resolveLanguage, type SupportedLanguage } from "./locales";
|
|
import { messages } from "./messages";
|
|
|
|
export type I18nParams = Record<string, string | number | boolean | null | undefined>;
|
|
|
|
const interpolate = (template: string, params?: I18nParams): string => {
|
|
if (!params) return template;
|
|
return template.replace(/\{(\w+)\}/g, (match, key) => {
|
|
const value = params[key];
|
|
return value === undefined || value === null ? match : String(value);
|
|
});
|
|
};
|
|
|
|
export const translate = (
|
|
key: string,
|
|
params?: I18nParams,
|
|
language: SupportedLanguage | string = DEFAULT_LANGUAGE,
|
|
): string => {
|
|
const resolvedLanguage = resolveLanguage(language);
|
|
const template =
|
|
(messages[resolvedLanguage] as Record<string, string>)[key] ??
|
|
(messages[DEFAULT_LANGUAGE] as Record<string, string>)[key];
|
|
return template ? interpolate(template, params) : key;
|
|
};
|