# General Translation Integrations: 管理翻译 URL: https://generaltranslation.com/zh/docs/integrations/sanity/guides/managing-translations.mdx --- title: 管理翻译 description: "如何管理 General Translation 的 Sanity 翻译:本指南涵盖导入、引用修复、批量发布和字段行为。" related: links: - /docs/integrations/sanity/guides/translating-content - /docs/integrations/sanity/guides/querying-translations - /docs/integrations/sanity/guides/configuring-sanity --- 完成设置后,您可以使用插件的翻译工具来管理已生成的 Sanity 翻译。 本指南涵盖全站范围的 **Translations** 工具、引用修复、批量发布、字段行为以及自定义序列化。 ## 使用 Translations 工具 [#translations-tool] 该插件会注册一个站点级的 **Translations** 工具,列出所有可翻译的文档。你可以在这里: * 为所有文档生成翻译 (**Translate All**) 。 * 导入所有已就绪的翻译,并覆盖现有内容 (**Import All**) 。 * 仅导入尚未记录到源文档元数据中的翻译 (**Import Missing**) 。 * 修复翻译后的文档之间的引用 (**Patch Document References**) 。 * 发布其源文档已发布的翻译后的文档 (**Publish Translations**) 。 开启 **Auto-refresh** 可轮询翻译状态,或使用 **Refresh Status** 检查一次。 ## 导入翻译 [#import] 生成的翻译必须先导入 Sanity,才会显示为翻译后的文档。导入时,插件会将已翻译的字段与源文档合并,因此未发送翻译的字段也会一并保留。 * 对于单个文档,可通过 **Translate** 操作或可选的 **General Translation** 选项卡进行导入。 * 对于多个文档,请在 Translations 工具中使用 **Import All** 或 **Import Missing**。 ## 修复引用 [#patch-references] 翻译后的文档可能会引用其他文档。引用修复会重写每个引用 `_ref`:如果被引用文档在同一区域设置下存在对应译文,就将其指向该译文。插件会从 `translation.metadata` 文档中解析这些引用。 在 Translations 工具中使用 **Patch Document References**,即可对现有的翻译后的文档执行此操作。在单文档对话框中,导入后也会自动运行 (此处默认启用自动修复) 。 ## Publish Translations [#publish] 使用 **Publish Translations** 可批量发布翻译后的文档。该插件只会发布其源文档已发布的翻译后的文档。这在导入大量译文后,或在某个区域设置中批量修补引用之后非常有用。 ## 管理字段行为 [#field-behavior] 当某些字段不应按常规方式进行翻译时,请使用字段匹配器。每个匹配器都通过 JSONPath `property` 表达式匹配字段,并且还可以选择指定文档 `_id`。 ### 复制字段而不进行翻译 对于应从源文档复制、但不发送到翻译 API 的字段,请使用 `ignoreFields`,例如类别、标签或内部元数据。 ```ts gtPlugin({ sourceLocale: 'en', locales: ['es', 'zh', 'ja'], ignoreFields: [ { fields: [{ property: '$.category' }] }, { fields: [{ property: '$..linkType' }] }, ], }); ``` ### 复制字段并设为唯一 对于需要以源值为起点、但随后在每个区域设置中保持唯一的字段,请使用 `dedupeFields`。这种情况在 slug 字段中很常见。 ```ts gtPlugin({ sourceLocale: 'en', locales: ['es', 'zh', 'ja'], dedupeFields: [{ fields: [{ property: '$.slug', type: 'slug' }] }], }); ``` 对于 Sanity 的 slug,`{ _type: 'slug', current: 'about' }` 在西班牙语中会变成 `{ _type: 'slug', current: 'about-es' }`。如果之后有编辑者修改了翻译后的 slug,后续导入时会保留该修改后的值。 ### 从翻译中排除字段 对于完全不应复制到翻译后的文档中的字段,请使用 `skipFields`,例如仅用于 source 的元数据,或需要编辑者按语言手动设置的 slug。 ```ts gtPlugin({ sourceLocale: 'en', locales: ['es', 'zh', 'ja'], skipFields: [ { fields: [{ property: '$.slug', type: 'slug' }] }, { fields: [{ property: '$.canonicalUrl' }] }, ], }); ``` 完整类型请参见[字段匹配器参考](/docs/integrations/sanity/reference/plugin-configuration#field-matchers)。 ## 阻止自定义类型被翻译 [#stop-types] 默认情况下,plugin 会保留一组不会被翻译的 schema 类型。使用 `additionalStopTypes` 添加你自己的自定义类型。 ```ts gtPlugin({ sourceLocale: 'en', locales: ['es', 'zh', 'ja'], additionalStopTypes: ['codeBlock', 'mux.video', 'mux.videoAsset'], }); ``` 请参阅完整的[默认 stop types](/docs/integrations/sanity/reference/plugin-configuration#stop-types)。 ## 自定义序列化 [#serialization] 该 plugin 会先将文档转换为 HTML 以便翻译,之后再转换回来。大多数项目都不需要修改这部分。只有当你的 schema 中包含默认序列化器无法处理的自定义 marks 或 block types 时,才需要使用自定义序列化器。 ```ts title="sanity.config.ts" import { attachGTData, gtPlugin } from 'gt-sanity'; gtPlugin({ sourceLocale: 'en', locales: ['es', 'zh', 'ja'], additionalSerializers: { marks: { link: ({ value, children }) => attachGTData(`${children}`, value, 'markDef'), inlineMath: ({ value, children }) => attachGTData(`${children}`, value, 'markDef'), }, }, }); ``` `attachGTData(html, data, 'markDef')` 会将该标记的数据嵌入序列化后的 HTML 中,以便 plugin 在将翻译合并回来时重建该标记。请参阅[序列化参考](/docs/integrations/sanity/reference/plugin-configuration#serialization)。 ## Next steps - /docs/integrations/sanity/guides/translating-content - /docs/integrations/sanity/guides/querying-translations - /docs/integrations/sanity/guides/configuring-sanity