import { Messages as Messages$1, GenerateConfig as GenerateConfig$1, TransformConfig as TransformConfig$1, UploadConfig as UploadConfig$1 } from '@higgins-mmt/core'; import { NodePath } from '@babel/traverse'; import { StringLiteral, TemplateLiteral } from '@babel/types'; /** * 国际化消息集合 * @interface Messages * @example * ```typescript * const messages: Messages = { * 'hello': '你好', * 'welcome': { * 'morning': '早上好', * 'evening': '晚上好' * } * } * ``` */ interface Messages { [key: string]: string | Messages; } /** * 生成国际化文件的配置 * @interface GenerateConfig * @property {string} filename - 基准语言文件名,如 'zh-CN.json' * @property {string} path - 生成文件的目标路径,如 './locales' * @property {string[]} langList - 需要生成的语言列表,如 ['en-US', 'ja-JP'] * @example * ```typescript * const config: GenerateConfig = { * filename: 'zh-CN.json', * path: './locales', * langList: ['en-US', 'ja-JP'] * } * ``` */ interface GenerateConfig { filename: string; path: string; langList: string[]; } /** * @module generator * @description * 国际化文件生成模块,负责管理和生成国际化资源文件。 * * 主要功能: * 1. 收集和管理需要国际化的文本 * 2. 生成基准语言文件 * 3. 生成多语言翻译文件 * 4. 保持已有翻译不被覆盖 * * @example * ```typescript * import { setMessage, generate } from './generator' * * // 收集需要国际化的文本 * setMessage('hello', '你好') * setMessage('welcome', '欢迎') * * // 生成国际化文件 * generate({ * filename: 'zh-CN.json', * path: './locales', * langList: ['en-US', 'ja-JP'] * }) * ``` */ /** * 生成国际化文件 * @param {GenerateConfig} config - 生成配置 * @throws {Error} 当文件操作失败时抛出错误 * * @description * 1. 如果目标目录不存在,会自动创建 * 2. 生成基准语言文件,包含所有原始文本 * 3. 为每个目标语言生成对应的翻译文件 * 4. 如果目标语言文件已存在,会保留已有的翻译 */ declare function generate(config: GenerateConfig): void; declare function ignoreAutoI18n(val: T): T; /** 生成i18n key的函数类型 */ type GenerateKey = (value: string, node: NodePath['node'] | NodePath['node'], messages: Messages$1) => string; /** 转换器主配置接口 */ interface TransformConfig { /** 是否转换 */ open?: boolean; /** 要包含的文件glob模式 */ include?: string[]; /** 要排除的文件glob模式 */ exclude?: string[]; /** i18n函数名,如't'或'i18n' */ i18nCallee?: string; /** 匹配需要转换文本的正则,如中文匹配 */ localePattern?: RegExp; /** 自定义key生成函数 */ generateKey?: GenerateKey; /** i18n库的导入配置 */ dependency?: { /** 包名,如'i18next' */ path: string; /** 导入的变量名 */ name: string; /** 模块系统类型 */ module: 'commonjs' | 'esm'; /** 是否使用解构导入 */ objectPattern?: boolean; }; } /** * i18n转换器主入口模块 * 负责将源代码转换为国际化版本,主要功能包括: * 1. 自动识别和转换中文字符串 * 2. 处理普通字符串和模板字符串 * 3. 自动注入i18n依赖 * 4. 保持源代码格式 */ /** * 转换源代码为国际化版本 */ declare function transform({ id, code, }: { id: string; code: string; }, config: TransformConfig): { code: string; map: { version: number; sources: string[]; names: string[]; sourceRoot?: string | undefined; sourcesContent?: string[] | undefined; mappings: string; file: string; } | null; }; /** * 上次至翻译平台配置 * 该模块功能是将翻译资源上传至翻译平台 * 与后端强相关,需要后端支持 */ type AppType = 'FE_VUE2' | 'FE_VUE3'; /** * 上传策略 * - INSERT_ONLY:只插入新键,原有键不作变更 * - INSERT_UPDATE:不仅插入新键,还覆盖更新原有键的文本值 * - INSERT_CLEAN:只插入新键,并删去此次请求中未出现的键 * - UPSERT_CLEAN:不仅插入新键,还覆盖更新原有键的文本值,并删去此次请求中未出现的键 * * 在服务器端构建翻译资源时,请选择UPSERT_CLEAN以清理冗余的翻译资源 */ type UploadStrategy = 'INSERT_ONLY' | 'INSERT_UPDATE' | 'INSERT_CLEAN' | 'UPSERT_CLEAN'; /** 上传至翻译平台配置 */ interface UploadConfig { /** 标识应用,每个应用唯一 */ app: string; /** 翻译平台接口的url */ url: string; /** 应用类型 */ appType: AppType; /** 本地语言文件路径 */ localePath?: string; /** 本地语言文件配置,key为对应的语种,value为对应语种翻译文件名,可传多个 */ localeConfig?: Record; uploadStrategy?: UploadStrategy; } interface LangItem { /** 语种 */ locale: string; /** 语种翻译内容 */ json: Messages$1; } interface UploadParams { app: string; appType: AppType; codeSource: 'FE_SCAN_UPLOAD' | 'FE_GENERATE_UPLOAD'; langList: LangItem[]; strategy?: UploadStrategy; } interface UploadResponse { code: number; message: string; success: boolean; } declare function upload(uploadConfig: UploadConfig, generateConfig: GenerateConfig$1): Promise; type UploadOptions = Omit & { appType?: 'FE_VUE2' | 'FE_VUE3'; }; interface I18nPluginOptions { transformConfig?: TransformConfig$1; uploadConfig: UploadOptions; generateConfig: GenerateConfig$1; open?: boolean; } export { generate, ignoreAutoI18n, transform, upload }; export type { GenerateConfig, GenerateKey, I18nPluginOptions, LangItem, Messages, TransformConfig, UploadConfig, UploadParams, UploadResponse, UploadStrategy };