import * as en from './en'; type I18nParams = readonly unknown[] | Record; /** * 读取语言文本,并替换位置占位符({0})或命名占位符({name})。 */ export function t(path: string, ...params: unknown[]): string { const value = path.split('.').reduce((current, key) => { if (!current || typeof current !== 'object') return undefined; return (current as Record)[key]; }, en); if (typeof value !== 'string') return path; if (params.length === 0) return value; const replacements = getReplacements(params); return value.replace(/\{([^{}]+)\}/g, (placeholder, key) => { const replacement = Array.isArray(replacements) ? replacements[Number(key)] : replacements[key]; return replacement === undefined ? placeholder : String(replacement); }); } function getReplacements(params: unknown[]): I18nParams { const [firstParam] = params; if (params.length === 1 && Array.isArray(firstParam)) return firstParam; if (params.length === 1 && firstParam && typeof firstParam === 'object') { return firstParam as Record; } return params; }