Paraglide JS

使用 inlang check 可以找出未使用的翻译键和缺失的翻译,并在缺少翻译时让 CI 失败。它适用于任何 Paraglide JS 项目,例如 SvelteKit、React、TanStack Start、Next.js 和 Astro。

inlang CLI 的 check 命令会在翻译上线前发现其中的问题:

  • 缺失或为空的翻译及形式
  • 缺失或未知的变量,以及缺失的标记
  • 缺失的复数或 select 形式及选择器
  • 源代码中已不再使用的消息
npx @inlang/cli check --project ./project.inlang
Checked project.inlang · 7 messages · locales en-US, pt-BR · 2 source files in ./

missing-translation (1)
  welcome_back  pt-BR  no translation

missing-variable (2)
  cart_items  pt-BR  missing {count} (countPlural=other)
  greeting    pt-BR  missing {name}

3 findings
  missing-translation  1
  missing-variable     2

默认会运行所有检查。传入一个或多个检查标志 (例如 --missing-translations 或 --unused-messages) ,即可只运行对应的检查。全部检查项和选项请参阅 CLI 文档。

check 需要 @inlang/cli 3.4.0 或更高版本。如果项目的 devDependencies 中装有旧版 @inlang/cli,npx 会运行这个旧版本:请升级它,或改用 npx @inlang/cli@latest check。

如需翻译消息文件,可通过 inlang CLI 使用 General Translation (npx @inlang/cli machine translate --project ./project.inlang) 。配置方法及其他选项请参阅翻译消息。

[!NOTE]inlang validate 和 inlang lint 已弃用。inlang lint 不会执行任何操作,直接以 0 退出,因此请在脚本和 CI 中将其替换为 inlang check。

未使用的消息

未使用的消息是指源代码中没有任何文件引用的翻译键。如需查找未使用的翻译键,只需单独运行以下检查:

npx @inlang/cli check --project ./project.inlang --unused-messages
Checked project.inlang · 7 messages · locales en-US, pt-BR · 12 source files in ./

unused-message (2)
  legacy_banner
  password_label

2 findings
  unused-message  2

check 会在项目父目录下的源文件中查找消息的使用位置。传入 --source 可只搜索部分文件或目录,例如 --source ./src。被 Git 忽略的文件、node_modules、Paraglide 的编译产物以及 vite.config.ts 等构建工具配置都会被跳过。详情请参阅 --source。

只有当搜索范围内源代码中的每一处使用都能被解析时,消息才会被报告为未使用。“未使用”仅指在所搜索的源代码中未被使用:即使某条消息被其他仓库或 --source 范围之外的代码使用,它仍可能被报告为未使用。

前提条件:m-function 匹配器 2.3.0 或更高版本

未使用消息检查要求在 project.inlang/settings.json 的 modules 中配置 @inlang/plugin-m-function-matcher 2.3.0 或更高版本。若未配置,check 会跳过未使用消息的检查,并提示你添加该插件;若版本过旧,则会提示你更新匹配器:

unused-message not checked: The installed @inlang/plugin-m-function-matcher can't analyze usages.
  Unused-message check needs @inlang/plugin-m-function-matcher ≥ 2.3.0, update the module URL in settings.json: https://cdn.jsdelivr.net/npm/@inlang/plugin-m-function-matcher@2.2.6/dist/index.js

更新模块 URL 中的版本号 (如果匹配器是从 node_modules 加载的,则改为更新已安装的包) :

{
  "modules": [
    "https://cdn.jsdelivr.net/npm/@inlang/plugin-message-format@4.5.0/dist/index.js",
-   "https://cdn.jsdelivr.net/npm/@inlang/plugin-m-function-matcher@2.2.6/dist/index.js"
+   "https://cdn.jsdelivr.net/npm/@inlang/plugin-m-function-matcher@2.3.0/dist/index.js"
  ]
}

确保分析精确

匹配器会分析 JavaScript、TypeScript、JSX、TSX 和 Svelte 文件。以下用法可被完整解析:

import { m } from "./paraglide/messages.js";

m.welcome_back(); // 直接调用
m["nav.home"](); // 字符串字面量键
const label = m.save_button; // 引用,之后再调用

在运行时动态拼接消息键会导致分析不完整,因为分析器无法确定该键指向哪些消息:

m[`${fieldName}_label`](); // ❌ 动态键
m[key](); // ❌ 动态键
type Key = keyof typeof m; // ❌ 可能依赖全部消息

这样,check 会指出具体的文件、行号和原因,而不是只列出未使用的消息:

unused-message incomplete: unused messages can't be determined because:
  src/Field.tsx:14:8  m[`${fieldName}_label`]  Dynamic message access cannot be resolved.
  Unused messages are only reported when every usage can be resolved, e.g. m.some_key().

要让分析更精确,请直接引用每条消息,不要动态拼接它的键。例如,将值映射到消息函数:

const labels = {
  email: m.email_label,
  phone: m.phone_label,
};

labels[fieldName](); // ✅ 每条消息都是直接引用的

这样消息也能保持可 tree-shake,与动态消息中的情况相同。

有些写法只要出现在任何被分析的文件中,即使与消息无关,也会导致分析不完整,因为它们可能在分析器看不到的地方加载或传递消息模块:从其他模块重新导出 (export * from "./x"、export { A } from "./A") 、动态 import()、import.meta.glob、require 和 eval。匹配器无法分析的文件 (如 .vue、.astro、.svx、.mdx 或 CommonJS 文件) 同样会导致分析不完整。输出会列出每个相关位置,你可以通过 --source 传入使用消息的目录,或者接受该项目不会报告未使用的消息。

在 CI 中运行检查

如需在缺少翻译时让 CI 失败,请在拉取请求工作流中运行 check。只要报告了检查结果或项目错误,check 就会以 1 退出,因此无需额外配置即可让 CI 任务失败。使用 --locales 可以只在你发布的语言区域出现问题时判定失败。不属于任何语言区域的检查结果 (例如未使用的消息) 以及项目错误始终都会报告:

# .github/workflows/i18n.yml
name: i18n
on: pull_request
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npx @inlang/cli check --project ./project.inlang --locales de,fr
  • --no-fail 即使有发现项也以 0 退出,适用于生成报告等场景。
  • --format json 输出完整报告,供其他工具使用:包括所有发现项、每项检查的状态,以及每处无法分析的用法的位置。
  • 无法运行或未能完成的检查 (例如使用动态键的未使用消息) 会在报告中列出,但不会导致命令失败。

在可信的工作流中使用 General Translation 凭据生成翻译,审阅并提交更新后的消息文件,然后在拉取请求上运行这些检查。设置方法请参阅翻译消息。

详见 CLI 文档中的退出码和 JSON 输出。