t-comm
Version:
专业、稳定、纯粹的工具库
71 lines (70 loc) • 2.47 kB
TypeScript
import { TI18N_KEYS as DEFAULT_TI18N_KEYS, LANG_NAME_TO_TI18N_KEY as DEFAULT_LANG_NAME_TO_TI18N_KEY, type Ti18nKey } from './config';
export { Ti18nKey, DEFAULT_TI18N_KEYS as TI18N_KEYS, DEFAULT_LANG_NAME_TO_TI18N_KEY as LANG_NAME_TO_TI18N_KEY };
/**
* 将语言名称映射为 ti18n key
* @param name Excel 首行的语言名称
* @param langNameMapping 语言名称到 ti18n key 的映射表,默认使用 LANG_NAME_TO_TI18N_KEY
* @returns 对应的 ti18n key,无法识别返回 null
* @example
* ```ts
* mapLangNameToKey('中文'); // 'zh'
* mapLangNameToKey('English'); // 'en'
* mapLangNameToKey('未知'); // null
*
* // 自定义映射
* mapLangNameToKey('日语', { '日语': 'ja' }); // 'ja'
* ```
*/
export declare function mapLangNameToKey(name: string | null | undefined, langNameMapping?: Record<string, Ti18nKey>): Ti18nKey | null;
interface ConvertResult {
jsonData: Record<string, string>[];
activeKeys: string[];
}
export interface ConvertExcelOptions {
/** 支持的 ti18n keys 列表,默认使用 TI18N_KEYS */
ti18nKeys?: readonly string[];
/** 语言名称到 ti18n key 的映射表,默认使用 LANG_NAME_TO_TI18N_KEY */
langNameMapping?: Record<string, Ti18nKey>;
}
/**
* 读取 Excel,替换首行 key 为 ti18n 标准 key,输出新 Excel
* @param inputPath 输入 Excel 路径
* @param outputPath 输出 Excel 路径
* @param options 转换配置选项
* @returns 转换结果,包含 jsonData 和 activeKeys
* @example
* ```ts
* // 使用默认配置
* convertExcelToTi18n('./input.xlsx', './output.xlsx');
*
* // 自定义支持的语言列表
* convertExcelToTi18n('./input.xlsx', './output.xlsx', {
* ti18nKeys: ['zh', 'en', 'ja', 'ko'], // 只支持中英日韩
* });
*
* // 自定义语言名称映射
* convertExcelToTi18n('./input.xlsx', './output.xlsx', {
* langNameMapping: {
* ...DEFAULT_LANG_NAME_TO_TI18N_KEY,
* '日语': 'ja',
* 'japanese': 'ja',
* '韩语': 'ko',
* 'korean': 'ko',
* },
* });
*
* // 完全自定义配置
* convertExcelToTi18n('./input.xlsx', './output.xlsx', {
* ti18nKeys: ['zh', 'en', 'ja'],
* langNameMapping: {
* 'zh': 'zh',
* 'en': 'en',
* 'ja': 'ja',
* '中文': 'zh',
* '英文': 'en',
* '日文': 'ja',
* },
* });
* ```
*/
export declare function convertExcelToTi18n(inputPath: string, outputPath: string, options?: ConvertExcelOptions): ConvertResult;