Paraglide JS 可通过 inlang 插件支持任意消息语法和文件格式。也就是说,你可以使用 JSON、YAML、i18next 格式,或任何有对应插件的其他格式。
选择插件
| 插件 | 适用场景 | 文件格式 |
|---|---|---|
| inlang 消息格式 | 新项目,完整支持 inlang 全部功能 | messages/{locale}.json |
| i18next | 从 i18next 迁移 | locales/{locale}.json |
| ICU MessageFormat | 已有 ICU 消息(FormatJS、svelte-i18n) | messages/{locale}.json |
| JSON | 简单的键值对翻译 | {locale}.json |
前往 inlang.com/c/plugins 浏览全部插件。
消息语法示例
inlang 消息格式
messages/en.json
{
"greeting": "Hello {name}!",
"welcome_message": "Welcome to {appName}, {username}!",
"items_count": [
{
"declarations": ["input count", "local countPlural = count: plural"],
"selectors": ["countPlural"],
"match": {
"countPlural=one": "{count} item",
"countPlural=other": "{count} items"
}
}
]
}
messages/de.json
{
"greeting": "Hallo {name}!",
"welcome_message": "Willkommen bei {appName}, {username}!",
"items_count": [
{
"declarations": ["input count", "local countPlural = count: plural"],
"selectors": ["countPlural"],
"match": {
"countPlural=one": "{count} Artikel",
"countPlural=other": "{count} Artikel"
}
}
]
}
复数形式和其他条件消息通过变体实现。如需按语言区域格式化数字和日期,请参阅格式化。
ICU MessageFormat
ICU MessageFormat 插件可读取 FormatJS 和 svelte-i18n 所用的 ICU MessageFormat v1 字符串。它只读取顶层键,因此请使用扁平键 (键名中可以包含点号) 。
messages/en.json
{
"greeting": "Hello {name}!",
"items_count": "{count, plural, one {# item} other {# items}}"
}
project.inlang/settings.json
{
"baseLocale": "en",
"locales": ["en", "de"],
"modules": ["https://cdn.jsdelivr.net/npm/@inlang/plugin-icu1@1.3.11/dist/index.js"],
"plugin.inlang.icu-messageformat-1": {
"pathPattern": "./messages/{locale}.json"
}
}
i18next 格式
locales/en.json
{
"greeting": "Hello {{name}}!",
"items_count_one": "{{count}} item",
"items_count_other": "{{count}} items"
}
简单 JSON
en.json
{
"greeting": "Hello!",
"goodbye": "Goodbye!"
}
安装插件
将插件 URL 添加到 project.inlang/settings.json 的 modules 数组中:
[!NOTE]具体配置选项请参阅各插件的文档。
{
"baseLocale": "en",
"locales": ["en", "de"],
"modules": [
- "https://cdn.jsdelivr.net/npm/@inlang/plugin-message-format@4.5.0/dist/index.js"
+ "https://cdn.jsdelivr.net/npm/@inlang/plugin-i18next@6.0.11/dist/index.js"
]
}
使用多个插件
同一个项目中可以同时使用多个插件,以支持不同的文件格式:
{
"baseLocale": "en",
"locales": ["en", "de"],
"modules": [
"https://cdn.jsdelivr.net/npm/@inlang/plugin-message-format@4.5.0/dist/index.js",
"https://cdn.jsdelivr.net/npm/@inlang/plugin-i18next@6.0.11/dist/index.js"
]
}
这在从一种格式迁移到另一种格式时很有用,也适用于应用的不同部分使用不同翻译格式的情况。
面向译者的描述
一个 bundle 可以有一条共享描述,用于说明其含义、出现位置以及翻译约束。Paraglide 会将该描述写入生成的 JSDoc 和 TypeScript 声明,因此鼠标悬停在消息函数上时即可看到。描述不会被添加到消息的运行时元数据或翻译输出中。
如果你使用的 inlang SDK 和 message-format 插件支持描述,请在源语言区域文件中使用复杂条目形式编写描述:
{
"checkout_confirm": [
{
"description": "Final checkout button. Confirms payment immediately. Keep it short.",
"match": { "*": "Pay now" }
}
]
}
对于不支持译者元数据的格式 (例如 i18next JSON) ,SDK 会将描述存储在 project.inlang/descriptions.json 中。请将这个配套文件与翻译文件一起纳入版本控制。与修改消息一样,修改描述也会触发生成文档的刷新。
