引入
ts
import {
TI18N_KEYS,
getI18nToken,
mapLangNameToKey,
convertExcelToTi18n
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
TI18N_KEYS,
getI18nToken,
mapLangNameToKey,
convertExcelToTi18n
} from 't-comm/lib/i18n/index';
// 只支持 ESM 的项目
import {
TI18N_KEYS,
getI18nToken,
mapLangNameToKey,
convertExcelToTi18n
} from 't-comm/es/i18n/index';TI18N_KEYS
描述:
i18n 国际化配置
参数:
getI18nToken(appId, appKey)
描述:
获取 i18n token
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| appId | string | appId |
| appKey | string | appKey |
返回: Promise.<string>
token
示例
ts
getI18nToken('appId', 'appKey').then(token => {
console.log('token', token)
})mapLangNameToKey(name, langNameMapping)
描述:
将语言名称映射为 ti18n key
参数:
| 参数名 | 描述 |
|---|---|
| name | Excel 首行的语言名称 |
| langNameMapping | 语言名称到 ti18n key 的映射表,默认使用 LANG_NAME_TO_TI18N_KEY |
返回:
对应的 ti18n key,无法识别返回 null
convertExcelToTi18n(inputPath, outputPath, options)
描述:
读取 Excel,替换首行 key 为 ti18n 标准 key,输出新 Excel
参数:
| 参数名 | 描述 |
|---|---|
| inputPath | 输入 Excel 路径 |
| outputPath | 输出 Excel 路径 |
| options | 转换配置选项 |
返回:
转换结果,包含 jsonData 和 activeKeys
示例
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',
},
});