AiI18nOptions

@ai-i18n/vite 导入:

import type { AiI18nOptions } from '@ai-i18n/vite';

定义

interface AiI18nOptions {
  framework?: AiI18nFramework;
  autoImport?: boolean;
  dts?: string | false;
  sourceLang: string;
  defaultLang?: string;
  locales: readonly LangOption[];
  persist?: boolean | AiI18nPersistOptions;
  loading?: AiI18nLocaleLoadingOptions;
  translationMemory?: AiI18nTranslationMemoryOptions;
  provider?: AiI18nProviderOptions;
  directory?: string;
  cleanup?: AiI18nCleanupOptions;
  html?: boolean | HtmlExtractorOptions;
}

字段

字段类型必填默认值作用
sourceLangstring源码文案所属语言。
localesreadonly LangOption[]项目支持的语言列表。
defaultLangstringsourceLang没有有效持久化值时使用的初始语言。
persistbooleanAiI18nPersistOptionsfalse使用 localStorage 保存语言偏好。
loadingAiI18nLocaleLoadingOptions全语言注册按 locale 拆分语言资产。
frameworkAiI18nFramework自动检测指定 Vanilla、Vue 或 React 模式。
autoImportbooleanfalse自动注入当前框架模式的 Runtime API。
dtsstring | false'src/ai-i18n.d.ts'设置声明文件路径,或关闭生成。
directorystring'i18n'设置协议目录;相对路径基于 Vite root
providerAiI18nProviderOptions不调用模型配置自动翻译函数、缓存与调度策略。
htmlboolean | HtmlExtractorOptionsfalse开启 index.html 文本和属性提取。
translationMemoryAiI18nTranslationMemoryOptions分片 JSON选择存储方式,并按需限制历史译文容量。
cleanupAiI18nCleanupOptions保留默认清理策略控制失效提取文件和孤立消息的清理。

语言约束

locales 至少包含一项,且每一项的 value 必须唯一。sourceLangdefaultLang 必须匹配 某个 value

目标语言缺译或值为 null 时,Runtime 返回 source 文案。插件不会为 sourceLang 生成重复的 目标语言文件。

初始语言按以下顺序选择:

  1. localStorage 中有效的持久化值;
  2. defaultLang
  3. 省略 defaultLang 时使用 sourceLang

框架与自动导入

省略 framework 时,插件读取 Vite 最终插件列表:

检测结果模式
存在 vite:vuevite:vue-jsxvue
存在 vite:react*react
都不存在vanilla

同一个 build 同时出现 Vue 与 React 插件时会报错。显式设置 framework 不会绕过这项检查。 显式值只能是 'vanilla''vue''react';JavaScript 配置中的其他值也会在启动时被拒绝。

autoImport: true 注入的 API 取决于最终模式。三种模式都有 tsetLanggetLanggetLangsgetLangLoadStatesubscribe;Vue 额外注入 useI18ntRefi18nComputedtComputed,React 额外注入 useI18n

完整接入方法见自动导入

路径

directorydts 的相对路径都基于 Vite root 解析;传入绝对路径时直接使用该路径。

相关内容