Flutter ARB

使用 Lingo.dev CLI 对 Flutter ARB 文件进行 AI 翻译

什么是 Flutter ARB?

Flutter ARB(Application Resource Bundle,应用资源包)是一种基于 JSON 的格式,由 Google 的 Flutter 框架用于管理本地化字符串。它支持元数据、占位符和 ICU 消息格式,用于构建原生编译的应用程序。

什么是 Lingo.dev CLI?

Lingo.dev CLI 是一个免费且开源的命令行工具,利用 AI 翻译应用和内容。它旨在替代传统的翻译管理软件,并可集成到现有的开发流水线中。

如需了解更多,请参见 概述

关于本指南

本指南将介绍如何使用 Lingo.dev CLI 翻译 Flutter ARB 文件。

你将学习如何:

  • 从零创建项目
  • 配置翻译流水线
  • 使用 AI 生成翻译

前置条件

要使用 Lingo.dev CLI,请确保已安装 Node.js v18 及以上版本:

❯ node -v
v22.17.0

步骤 1. 创建项目

在你的项目目录下,创建一个 i18n.json 文件:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {}
}

该文件定义了翻译流水线的行为,包括需要翻译的语言以及本地化内容在文件系统中的位置。

如需了解可用属性,请参见 i18n.json

步骤 2. 配置源语言区域

源语言区域 是指你的内容最初编写时所用的语言和地区。要配置源语言区域,请在 i18n.json 文件中设置 locale.source 属性:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {}
}

源语言区域必须以 BCP 47 语言标签 的形式提供。

如需查看 Lingo.dev CLI 支持的所有语言区域代码,请参见 支持的语言区域代码

步骤 3. 配置目标语言区域

目标语言区域 指的是您希望将内容翻译成的语言和地区。要配置目标语言区域,请在 locale.targets 属性中设置,并写入 i18n.json 文件:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {}
}

步骤 4. 创建源内容

如果尚未创建,请新建一个或多个包含待翻译内容的 ARB 文件。这些文件的路径中必须包含源语言区域(例如,作为目录名 en/,或作为文件名的一部分 messages.en.arb)。

对于 Flutter ARB 文件,有以下要求:

  • 每个消息键都必须有对应的翻译值
  • 元数据项(以 @ 开头)会被保留但不会被翻译
  • @@locale 元数据字段用于标识语言区域
  • 占位符({name})在翻译过程中会被保留
  • 支持 ICU 消息格式的复数和选择语法

例如:

{
  "@@locale": "en",
  "welcome_message": "Welcome to our Flutter app",
  "@welcome_message": {
    "description": "The main welcome message shown on the home screen"
  },
  "login_button": "Log In",
  "greeting_with_name": "Hello {name}!",
  "@greeting_with_name": {
    "placeholders": {
      "name": {
        "type": "String"
      }
    }
  }
}

步骤 5. 创建 bucket

  1. i18n.json 文件中,向 buckets 对象添加一个 "flutter" 对象:

    {
      "$schema": "https://lingo.dev/schema/i18n.json",
      "version": "1.10",
      "locale": {
        "source": "en",
        "targets": ["es"]
      },
      "buckets": {
        "flutter": {}
      }
    }
    
  2. "flutter" 对象中,定义一个或多个 include 模式的数组:

    {
      "$schema": "https://lingo.dev/schema/i18n.json",
      "version": "1.10",
      "locale": {
        "source": "en",
        "targets": ["es"]
      },
      "buckets": {
        "flutter": {
          "include": ["./[locale]/example.arb"]
        }
      }
    }
    

    这些模式定义了需要翻译的文件。

    这些模式本身:

    • 必须包含 [locale] 作为已配置语言区域的占位符
    • 可以指向文件路径(如 "[locale]/config.arb"
    • 可以使用星号作为通配符(如 "[locale]/*.arb"

    不支持递归 glob 模式(如 **/*.arb)。

步骤 6. 配置 LLM

Lingo.dev CLI 使用大型语言模型(LLM)通过 AI 翻译内容。要使用这些模型,您需要从支持的服务商获取 API 密钥。

为了尽快开始使用,我们推荐使用 Lingo.dev Engine —— 我们自有的托管平台,每月可免费使用 10,000 个 token:

  1. 注册一个 Lingo.dev 账号

  2. 运行以下命令:

    npx lingo.dev@latest login
    

    这将打开您的默认浏览器,并要求您进行身份验证。

  3. 按照提示操作。

步骤 7. 生成翻译内容

在包含 i18n.json 文件的目录下,运行以下命令:

npx lingo.dev@latest run

该命令将:

  1. 读取 i18n.json 文件。
  2. 查找需要翻译的文件。
  3. 提取文件中的可翻译内容。
  4. 使用已配置的 LLM 翻译提取的内容。
  5. 将翻译后的内容写回文件系统。

首次生成翻译时,会创建一个 i18n.lock 文件。该文件用于记录已翻译的内容,防止后续运行时重复翻译。

示例

en/example.arb

{
  "@@locale": "en",
  "welcome_message": "Welcome to our Flutter app",
  "@welcome_message": {
    "description": "The main welcome message shown on the home screen",
    "context": "greeting"
  },
  "login_button": "Log In",
  "signup_button": "Sign Up",
  "greeting_with_name": "Hello {name}!",
  "@greeting_with_name": {
    "description": "Greeting message with user's name",
    "placeholders": {
      "name": {
        "type": "String",
        "description": "The user's display name"
      }
    }
  },
  "item_count": "{count, plural, =0 {No items} one {# item} other {# items}}",
  "@item_count": {
    "description": "Message showing the number of items with proper pluralization",
    "placeholders": {
      "count": {
        "type": "int",
        "description": "The number of items"
      }
    }
  }
}

es/example.arb

{
  "@@locale": "es",
  "welcome_message": "Bienvenido a nuestra aplicación Flutter",
  "@welcome_message": {
    "description": "The main welcome message shown on the home screen",
    "context": "greeting"
  },
  "login_button": "Iniciar sesión",
  "signup_button": "Registrarse",
  "greeting_with_name": "¡Hola {name}!",
  "@greeting_with_name": {
    "description": "Greeting message with user's name",
    "placeholders": {
      "name": {
        "type": "String",
        "description": "The user's display name"
      }
    }
  },
  "item_count": "{count, plural, =0 {Sin elementos} one {# elemento} other {# elementos}}",
  "@item_count": {
    "description": "Message showing the number of items with proper pluralization",
    "placeholders": {
      "count": {
        "type": "int",
        "description": "The number of items"
      }
    }
  }
}

i18n.json

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {
    "flutter": {
      "include": ["./[locale]/example.arb"]
    }
  }
}

i18n.lock

version: 1
checksums:
  88559ecadc7cff52625bd3a47e51dd78:
    welcome_message: 9c56d00796c3c7facba6a25275de5b7b
    login_button: 0029e5a35676c0051e761fcd046ef9ee
    signup_button: 0dd2ae69be4618c1f9e615774a4509ca
    error_network: e0becd5fc88e85a97c6987b96c80fb11
    success_message: e6dcd73052dc41dbe05d86af9887d2c4
    user_profile_title: bee775ff7216747b2111e93cefa57ddc
    settings_screen_title: 8df6777277469c1fd88cc18dde2f1cc3
    cancel_action: 2e2a849c2223911717de8caa2c71bade
    confirm_action: 90930b51154032f119fa75c1bd422d8b
    greeting_with_name: 218521f06746e82ba44d68c8a5bb210c
    item_count: d8d083b56bc155cf0997ea6ae989b83f
    user_status: 903b5a6edde5368dd9089accdc1b1f9d