键锁定

Lingo.dev CLI 允许您锁定特定的翻译键,以确保它们的值在所有语言中保持一致。当您锁定键时,CLI 会将其从翻译处理中排除,并将源值直接复制到所有目标文件中。

设置键锁定

i18n.json 的存储桶配置中添加 lockedKeys

{
  "locale": {
    "source": "en",
    "targets": ["es", "fr", "de"]
  },
  "buckets": {
    "json": {
      "include": ["locales/[locale].json"],
      "lockedKeys": ["system/component", "config/engine", "modules/processor"]
    }
  }
}

lockedKeys 数组使用正斜杠(/)表示法来指定嵌套键。

键锁定的工作原理

在翻译处理中,Lingo.dev CLI

  1. 识别配置中的锁定键
  2. 将其从翻译中排除——锁定的内容不会被发送到 AI 模型
  3. 将源值直接复制到所有目标文件
  4. 在所有语言中保持一致性

示例工作流程:

// locales/en.json (源文件)
{
  "welcome": "Welcome to our platform",
  "system": {
    "component": "Lingo.dev CLI",
    "processor": "Translation Engine"
  },
  "config": {
    "engine": "Lingo.dev Engine"
  }
}

使用锁定键配置:

{
  "lockedKeys": ["system/component", "system/processor", "config/engine"]
}

生成的西班牙语翻译:

// locales/es.json (生成文件)
{
  "welcome": "Bienvenido a nuestra plataforma",
  "system": {
    "component": "Lingo.dev CLI",
    "processor": "Translation Engine"
  },
  "config": {
    "engine": "Lingo.dev Engine"
  }
}

只有 welcome 被翻译,而锁定的键保持与源文件一致。

如果没有键锁定,"Lingo.dev Engine" 可能会被错误翻译为西班牙语的 "Motor de Lingo.dev" 或日语的 "Lingo.devエンジン",这在此示例中是我们不希望发生的。

嵌套键路径

使用正斜杠(/)表示法可以锁定任意深度的键:

{
  "lockedKeys": [
    "system/engine/component",
    "modules/ai/processor",
    "config/translation/handler"
  ]
}

此表示法适用于复杂的嵌套结构:

// 源结构
{
  "system": {
    "engine": {
      "component": "Lingo.dev Engine"
    }
  }
}

路径 system/engine/component 锁定了组件名称的值。

含有点号的键

正斜杠表示法可以处理名称中包含点号的键:

// 含有点号键名的源文件
{
  "modules": {
    "ai.translation": "AI 翻译",
    "content.processor": "内容处理器"
  }
}

锁定这些键:

{
  "lockedKeys": ["modules/ai.translation", "modules/content.processor"]
}

多种存储桶类型

不同的文件格式可以有不同的锁定键:

{
  "buckets": {
    "json": {
      "include": ["locales/[locale].json"],
      "lockedKeys": ["config/engine", "system/component"]
    },
    "yaml": {
      "include": ["translations/[locale].yml"],
      "lockedKeys": ["service/name", "module/title"]
    }
  }
}

每种存储桶类型根据内容结构维护其自己的锁定键列表。