Vue i18n 区块
使用 Lingo.dev CLI 对 Vue i18n 区块进行 AI 翻译
什么是 Vue i18n 区块?
Vue i18n 区块是在 Vue 单文件组件(SFC)中包含 JSON 格式翻译内容的特殊 <i18n> 区段。它们允许你将翻译内容与组件代码放在一起。
例如:
<template>
<div class="container">
<h1>{{ $t('welcome') }}</h1>
<button @click="handleClick">{{ $t('button.submit') }}</button>
</div>
</template>
<i18n>
{
"en": {
"welcome": "Hello, world!",
"description": "A simple demo app",
"button": {
"submit": "Submit",
"cancel": "Cancel"
}
}
}
</i18n>
什么是 Lingo.dev CLI?
Lingo.dev CLI 是一个免费开源的命令行工具,用于通过 AI 翻译应用和内容。它旨在替代传统的翻译管理软件,并可集成到现有的开发流水线中。
了解更多信息,请参见 概述。
关于本指南
本指南介绍如何使用 Lingo.dev CLI 翻译 Vue i18n 区块。
你将学习如何:
- 从零创建项目
- 配置翻译流水线
- 使用 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 步:创建源内容
如果尚未创建,请新建一个包含 <i18n> 区块的 Vue 组件文件,并在其中添加需要翻译的内容。
注意: 在翻译过程中,源内容文件将被覆盖,包含翻译后的内容(以及源内容)。
第 5 步:创建 bucket
-
在
i18n.json文件中,向buckets对象添加一个"vue-json"对象:{ "$schema": "https://lingo.dev/schema/i18n.json", "version": "1.10", "locale": { "source": "en", "targets": ["es"] }, "buckets": { "vue-json": {} } } -
在
"vue-json"对象中,定义一个或多个include模式的数组:{ "$schema": "https://lingo.dev/schema/i18n.json", "version": "1.10", "locale": { "source": "en", "targets": ["es"] }, "buckets": { "vue-json": { "include": ["./example.vue"] } } }这些模式用于定义需要翻译的文件,可以:
- 指定具体的文件路径(如
"some/dir/file.vue") - 使用星号作为通配符(如
"some/dir/*.vue")
不支持递归 glob 模式(如
**/*.vue)。 - 指定具体的文件路径(如
第 6 步:配置 LLM
Lingo.dev CLI 使用大型语言模型(LLM)通过 AI 翻译内容。要使用这些模型,您需要从支持的服务商获取 API 密钥。
为了尽快开始使用,我们推荐选择 Lingo.dev Engine —— 我们自有的托管平台,每月可免费使用 10,000 个 token:
-
运行以下命令:
npx lingo.dev@latest login这将打开您的默认浏览器,并提示您进行身份验证。
-
按照提示操作。
步骤 7. 生成翻译内容
在包含 i18n.json 文件的目录下,运行以下命令:
npx lingo.dev@latest run
该命令将执行以下操作:
- 读取
i18n.json文件。 - 查找需要翻译的文件。
- 提取文件中的可翻译内容。
- 使用已配置的 LLM 翻译提取的内容。
- 将翻译后的内容写回文件系统。
首次生成翻译时,会创建一个 i18n.lock 文件。该文件用于记录已翻译的内容,防止后续运行时重复翻译。
示例
example.vue(翻译前)
<i18n>
{
"en": {
"welcome": "Hello, world!",
"description": "A simple demo app",
"button": {
"submit": "Submit",
"cancel": "Cancel"
}
}
}
</i18n>
example.vue(翻译后)
<i18n>
{
"en": {
"welcome": "Hello, world!",
"description": "A simple demo app",
"button": {
"submit": "Submit",
"cancel": "Cancel"
}
},
"es": {
"welcome": "¡Hola, mundo!",
"description": "Una aplicación de demostración simple",
"button": {
"submit": "Enviar",
"cancel": "Cancelar"
}
}
}
</i18n>
i18n.json
{
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {
"vue-json": {
"include": ["./example.vue"]
}
},
"$schema": "https://lingo.dev/schema/i18n.json"
}
i18n.lock
version: 1
checksums:
7142a39dd2be0c1e12089c922ab4fdb5:
welcome: 1308168cca4fa5d8d7a0cf24e55e93fc
description: 36349ca88e416a6f2d6ac8742f0a963c
button/submit: dabdff794b5804b8f22ecab13f37fb7d
button/cancel: efdc4af6863f1e503a7f9961be721eed
messages/0: f77d23a0a4b7f8fc7f8fd458921b90dd
messages/1: e841c4139402ded42079401299e4fa1e
0378a0d09447bf0944e842f7e54d3ec2:
welcome: 0468579ef2fbc83c9d520c2f2f1c5059
description: 49f8864eb0e53903f04532bf33e1e4fa
button/submit: 7c91ef5f747eea9f77a9c4f23e19fb2e
button/cancel: 2e2a849c2223911717de8caa2c71bade
messages/0: 97a8db12c3955a85c4f50e3951c91a40
messages/1: 986a434e3895c8ee0b267df95cc40051