将流水线中的 CLI v0 迁移至新版 CLI

将 GitHub Actions、GitLab CI、Buildkite 或 Bitbucket Pipelines 中的 CLI v0 步骤替换为 @lingo.dev/cli,保留现有翻译。

迁移后,你的 CI 流水线会使用 @lingo.dev/cli 翻译每次变更,并提交翻译结果。

开始之前

  • 一个已配置本地化引擎的 Lingo.dev 组织
  • 已提交到 Git 的项目 i18n.lock。如果该文件缺失,请从 Git 恢复。没有它,v0 就无法区分过时翻译和最新翻译,即使翻译已过时,--frozen 也会通过检查。
  • Node.js 和 npm
  • CI 配置及其密钥的访问权限

迁移 #

  1. 完成 v0 的收尾工作#

    最后运行一次 v0

    新版 CLI 不会补齐现有目标文件中缺失的键,因此请先用 v0 将所有目标文件更新到最新状态。

    使用 package.json 中的 lingo.dev 版本,或最新的 0.138.x 版本,翻译待处理内容:

    终端
    npx lingo.dev@0.138 run

    然后检查是否还有遗漏:

    终端
    npx lingo.dev@0.138 run --frozen

    如果任一目标文件缺少键、源内容自 i18n.lock 以来发生了变化,或目标文件包含源文件中已不存在的键,检查就会失败,并显示“目标文件缺少翻译”。请修复报告的问题,再次运行,直到检查通过。

    提交源文件、目标文件和 i18n.lock。迁移完成前,请勿编辑源文件。

  2. 安装新版 CLI 并登录#

    两个软件包都会安装 lingo 命令,因此请先移除全局安装的 v0:

    终端
    npm uninstall --global lingo.dev
    npm install --global @lingo.dev/cli
    lingo --version
    lingo login

    lingo --version 会输出 lingo v1.26.0 或更高版本。如果项目将 lingo.dev 作为依赖项,请运行 npx @lingo.dev/cli,而不是 lingo。

  3. 转换 i18n.json#

    在新分支中,从 i18n.json 所在的文件夹运行以下命令:

    终端
    lingo migrate

    它会在 i18n.json 所在的目录中生成 .lingo/config.json,不会更改其他文件。如果输出的“未转换”部分列出了设置,请在转换 i18n.json中逐一查阅。如果仓库中有多个 i18n.json,请参阅多个 i18n.json 文件。

  4. 将项目关联到你的组织和引擎。如果 lingo migrate 输出了类似 lingo link --engine eng_abc123 的命令,请改为运行该命令,因为它指定了 v0 使用的引擎:

    终端
    lingo link

    提交 .lingo/config.json。

  5. 执行首次推送#

    从 .lingo/config.json 所在的文件夹运行:

    终端
    lingo push --backfill-missing --wait

    它只会翻译尚不存在的目标文件,保留已有文件,并写入 .lingo/lock.json,作为后续推送的基准。Lockfile 会精确记录源文件的字节内容,因此源文件必须使用 LF 换行符;补齐缺失的键中提供了 .gitattributes 配置行。当引擎将任务交由人工审核时,--wait 会在仅剩审核任务时返回;请在审核者完成审核后运行 lingo pull。

    审核差异,然后将目标文件与 .lingo/config.json 和 .lingo/lock.json 一起提交。此后,lingo push --wait 会翻译自上次推送以来发生变化的内容。

  6. 重命名 API 密钥#

    新版 CLI 仅读取 LINGO_API_KEY。请在配置所关联的组织中创建一个 API 密钥,并在所有原先保存 LINGODOTDEV_API_KEY 的 CI 系统和密钥存储中,将其保存为 LINGO_API_KEY。在完成密钥重命名之前,请在作业中进行映射:

    GitHub Actions
    env:
      LINGO_API_KEY: ${{ secrets.LINGODOTDEV_API_KEY }}
  7. 替换流水线步骤#

    删除运行 v0 的步骤或工作流,添加一个作业,用于运行新版 CLI,并将有变更的目标文件与 .lingo/lock.json 一起提交。展开适用于你的 CI 的作业配置,将 locales 替换为你的目标路径:

    GitHub Actions
    YAML
    name: Translate
    on:
      push:
        branches: [main]
    permissions:
      contents: write
    jobs:
      translate:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: 22
          - run: npx -y @lingo.dev/cli@latest push --wait
            env:
              LINGO_API_KEY: ${{ secrets.LINGO_API_KEY }}
          - run: |
              git add -- locales .lingo/lock.json
              git diff --cached --quiet && exit 0
              git config user.name "Localization bot"
              git config user.email "localization@example.com"
              git commit -m "Update translations"
              git push
    GitLab CI
    YAML
    translate:
      image: node:22
      resource_group: lingo-translations
      rules:
        - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      script:
        - git fetch --quiet "$CI_REPOSITORY_URL" "$CI_DEFAULT_BRANCH"
        - if [ "$(git rev-parse FETCH_HEAD)" != "$CI_COMMIT_SHA" ]; then exit 0; fi
        - npx -y @lingo.dev/cli@latest push --wait
        - git add -- locales .lingo/lock.json
        - if git diff --cached --quiet; then exit 0; fi
        - git config user.name "Localization bot"
        - git config user.email "localization@example.com"
        - git commit -m "Update translations"
        - >-
          git push --force
          -o ci.skip
          -o merge_request.create
          -o merge_request.target="$CI_DEFAULT_BRANCH"
          -o merge_request.title="Update translations"
          "https://oauth2:${GITLAB_PUSH_TOKEN}@${CI_SERVER_FQDN}/${CI_PROJECT_PATH}.git"
          "HEAD:refs/heads/lingo/translations"
    Buildkite
    YAML
    steps:
      - label: "Translate"
        if: build.branch == pipeline.default_branch && build.pull_request.id == null
        concurrency: 1
        concurrency_group: "lingo-translations"
        secrets:
          - LINGO_API_KEY
        command: |
          npx -y @lingo.dev/cli@latest push --wait
          git add -- locales .lingo/lock.json
          if git diff --cached --quiet; then exit 0; fi
          git config user.name "Localization bot"
          git config user.email "localization@example.com"
          git commit -m "[skip ci] Update translations"
          git push origin "HEAD:refs/heads/$${BUILDKITE_BRANCH}"
    Bitbucket Pipelines
    YAML
    image: node:22
    
    pipelines:
      branches:
        main:
          - step:
              name: Translate and commit
              auth:
                system:
                  scopes:
                    - read:git:bitbucket
                    - write:git:bitbucket
              script:
                - npx -y @lingo.dev/cli@latest push --wait
                - git add -- locales .lingo/lock.json
                - if git diff --cached --quiet; then exit 0; fi
                - git config user.name "Localization bot"
                - git config user.email "localization@example.com"
                - git commit -m "[skip ci] Update translations"
                - git push origin "HEAD:$BITBUCKET_BRANCH"

    请保留 --wait:没有它,推送命令会在运行启动后立即返回,不会写入任何文件。请继续提交 Lockfile:没有它,每次运行都会被视为首次运行。v0 Action 中的提交和拉取请求输入参数需改由你自己的 git 步骤处理,working-directory 也需移至该步骤。GitLab 作业使用 GITLAB_PUSH_TOKEN 变量进行推送,并创建合并请求。GitHub Actions、GitLab CI/CD、Buildkite 和 Bitbucket Pipelines 的配置示例详细说明了各个作业,包括如何通过拉取请求交付结果。

  8. 检查拉取请求#

    在每个拉取请求或合并请求中,将 --frozen 检查替换为 lingo check。它不需要 API 密钥;如果源内容自 .lingo/lock.json 以来发生了变化、源内容从未推送过,或目标文件缺失,检查就会失败。它不会报告现有目标文件中缺失的键。展开适用于你的 CI 的作业配置:

    GitHub Actions
    YAML
    name: Check translations
    on: pull_request
    jobs:
      check:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - run: npx -y @lingo.dev/cli@latest check
    GitLab CI
    YAML
    check_translations:
      image: node:22
      rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      script:
        - npx -y @lingo.dev/cli@latest check
    Buildkite
    YAML
    steps:
      - label: "Check translations"
        if: build.pull_request.id != null
        command:
          - npx -y @lingo.dev/cli@latest check
    Bitbucket Pipelines
    YAML
    image: node:22
    
    pipelines:
      pull-requests:
        "**":
          - step:
              name: Check translations
              script:
                - npx -y @lingo.dev/cli@latest check

    请在翻译与源内容更改一同提交的环节添加此检查。如果流水线在合并到默认分支后才进行翻译,功能拉取请求中的源内容更改会先于翻译提交,因此这些请求无法通过检查。

  9. 移除 v0#

    首次推送的更改提交且新作业运行后,即可清理 v0 的剩余内容。删除 v0 配置和 Lockfile:

    终端
    git rm i18n.json i18n.lock

    当所有作业都不再读取 LINGODOTDEV_API_KEY 密钥时,将其移除,同时移除仅供 v0 使用的提供商密钥。除非应用程序导入了 lingo.dev/sdk,否则也请从依赖项中移除 lingo.dev:

    终端
    npm uninstall lingo.dev

    然后搜索剩余的 v0 相关内容:

    终端
    git grep -nE 'lingo\.dev (run|i18n|ci)|lingodotdev/lingo\.dev@|LINGODOTDEV_API_KEY'

    参照映射 v0 命令和标志,将每个匹配项(例如运行 lingo.dev run 的 npm 脚本)替换为对应的新用法。在重命名旧密钥之前,只可保留它的 LINGO_API_KEY 映射。

确认迁移成功 #

  • lingo --version 输出 1.26.0 或更高版本,lingo whoami 显示你的组织和引擎;如果你使用 lingo login 登录,还会显示你的账户。
  • lingo check 以退出码 0 退出。
  • 更改一个源字符串后,流水线会将新翻译写入每个目标文件;再次运行 lingo push --wait 时,会输出“没有可推送的内容”。

如果出现问题,请参阅故障排查。

如果你使用了 --target-locale、--file、自己的模型密钥、多个 i18n.json 文件或其他非常规配置,请参阅非常规配置。如果某个步骤不适用于你的配置,请发送邮件至 support@lingo.dev。