Markdown

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

什么是 Markdown?

Markdown 是一种轻量级标记语言,使用纯文本格式语法来创建结构化文档。它广泛应用于文档编写、README 文件、博客和内容管理系统。

例如:

---
title: "Product Launch Guide"
description: "Everything you need to know about our latest product features"
author: "Product Team"
---

# Welcome to Our New Dashboard

Discover powerful new features designed to streamline your workflow and boost productivity.

## Getting Started

Follow these simple steps to set up your account and begin using our platform effectively.

![Dashboard overview screenshot](image.jpg)

什么是 Lingo.dev CLI?

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

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

关于本指南

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

你将学到如何:

  • 从零创建项目
  • 配置翻译流水线
  • 使用 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. 创建源内容

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

步骤 5. 创建 bucket

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

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

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

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

    这些模式:

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

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

步骤 6. 配置 LLM

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

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

  1. 注册 Lingo.dev 账号

  2. 运行以下命令:

    npx lingo.dev@latest login
    

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

  1. 按照提示操作。

步骤 7. 生成翻译内容

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

npx lingo.dev@latest run

该命令将:

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

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

示例

en/example.md

---
title: "Product Launch Guide"
description: "Everything you need to know about our latest product features"
author: "Product Team"
date: 2024-01-15
tags: ["apples", "bananas", "pears"]
---

# Welcome to Our New Dashboard

Discover powerful new features designed to streamline your workflow and boost productivity.

## Getting Started

Follow these simple steps to set up your account and begin using our platform effectively.

---

Our advanced analytics help you make data-driven decisions with confidence.

![Dashboard overview screenshot](image.jpg)

The intuitive interface makes it easy to navigate between different features and tools.

[View documentation](https://example.com)

Need help getting started? Our support team is available 24/7 to assist you.

***

Join thousands of satisfied customers who have transformed their business with our platform.

es/example.md

---
title: Guía de lanzamiento de producto
description: Todo lo que necesitas saber sobre las últimas características de
  nuestro producto
author: Equipo de producto
date: 2024-01-15
tags:
  - apples
  - bananas
  - pears
---

# Bienvenido a nuestro nuevo panel de control

Descubre nuevas y potentes funciones diseñadas para optimizar tus flujos de trabajo y aumentar la productividad.

## Primeros pasos

Sigue estos sencillos pasos para configurar tu cuenta y comenzar a utilizar nuestra plataforma de manera efectiva.

---

Nuestros análisis avanzados te ayudan a tomar decisiones basadas en datos con confianza.

![Captura de pantalla de la visión general del panel](image.jpg)

La interfaz intuitiva facilita la navegación entre diferentes funciones y herramientas.

[Ver documentación](https://example.com)

¿Necesitas ayuda para empezar? Nuestro equipo de soporte está disponible 24/7 para asistirte.

---

Únete a miles de clientes satisfechos que han transformado su negocio con nuestra plataforma.

i18n.json

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

i18n.lock

version: 1
checksums:
  eed9ef23a605b747d38b8916cee1d500:
    md-section-0: d53f61b8c8922fb62d9df5678d9b44a8
    md-section-1: 98aec271471bedce0e12b530c7060827
    md-section-2: 9e5a786192608844493dfbb6e4100886
    md-section-3: 1a5299c38bb20c1b8af0e64e33d7b2b0
    md-section-4: 51adf33450cab2ef392e93147386647c
    md-section-5: e56cc804e3e06b5f5fb2484e88c18adc
    md-section-6: 0ea86a3338305070c865e8fe138da890
    md-section-7: bbabf7f391569a72099001e3d81eb251
    md-section-8: 36cbfd93f42528edce4faac2ac3c2c12
    md-section-9: a1c50054ab23d70be8d453789b214580
    md-section-10: 51adf33450cab2ef392e93147386647c
    md-section-11: cb596c9608828f7b87a0ab8fa37beb07
    fm-attr-title: f3469c4e3d3377c39a705c844930b3a5
    fm-attr-description: 2e988d98001e44997a3f5fa3fb487ca6
    fm-attr-author: ec8c8711fce61265a4fe296ce2ba3b6f
    fm-attr-tags: 313ac6f17ee08e4f4a6a2ca95e5ae024