属性文件

使用 Lingo.dev CLI 进行 Java Properties 文件的 AI 翻译

什么是 Properties 文件?

Properties 文件是简单的键值对文本文件,通常用于 Java 应用程序的配置和本地化。

例如:

app.title=MyApp
app.description=A simple demo application

user.greeting=Hello, world!
user.farewell=Thanks for using MyApp

error.message=Something went wrong
error.notFound=Page not found

什么是 Lingo.dev CLI?

Lingo.dev CLI 是一个免费的开源命令行工具,用于通过 AI 翻译应用程序和内容。它旨在取代传统的翻译管理软件,同时与现有的流水线集成。

要了解更多,请参阅 概述

关于本指南

本指南解释了如何使用 Lingo.dev CLI 翻译 Properties 文件。

您将学习如何:

  • 从头开始创建项目
  • 配置翻译流水线
  • 使用 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 步:配置目标语言环境

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

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

第 4 步:创建源内容

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

第 5 步:创建一个存储桶

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

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

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

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

    模式本身:

    • 必须包含 [locale] 作为配置语言环境的占位符
    • 可以指向文件路径(例如,"[locale]/messages.properties"
    • 可以使用星号作为通配符占位符(例如,"[locale]/*.properties"

    不支持递归的 glob 模式(例如,**/*.properties)。

第 6 步:配置 LLM

Lingo.dev CLI 使用大型语言模型(LLM)通过 AI 翻译内容。要使用这些模型之一,您需要一个支持提供商的 API 密钥。

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

  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.properties

app.title=MyApp
app.description=A simple demo application

user.greeting=Hello, world!
user.farewell=Thanks for using MyApp

error.message=Something went wrong
error.notFound=Page not found

database.host=localhost
database.port=5432

notification.success=Changes saved!
notification.warning=Please check your input

es/example.properties

app.title=MyApp
app.description=Una aplicación de demostración simple

user.greeting=¡Hola, mundo!
user.farewell=Gracias por usar MyApp

error.message=Algo salió mal
error.notFound=Página no encontrada

database.host=localhost
database.port=5432

notification.success=¡Cambios guardados!
notification.warning=Por favor verifica tu entrada

i18n.json

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

i18n.lock

version: 1
checksums:
  2250baa513dddb2c691cb62115d5e257:
    app.title: 7dc70110429d46e3685f385bd2cc941c
    app.description: e13baa1e885129d9328e216ff534761b
    user.greeting: 0468579ef2fbc83c9d520c2f2f1c5059
    user.farewell: 118794a2b84f7bfb4b4ce602ed463b0f
    error.message: a3cd2f01c073f1f5ff436d4b132d39cf
    error.notFound: 97612e6230bc7a1ebd99380bf561b732
    database.host: da86e4fc0c04d82c87006dc71cea7e97
    notification.success: 3b7a8b0aa23977592d4270ea136a390c
    notification.warning: c38895f731311cefacee9e8d7d10fc49