# General Translation Integrations: 查询翻译文档 URL: https://generaltranslation.com/zh/docs/integrations/sanity/guides/querying-translations.mdx --- title: 查询翻译文档 description: 如何使用 GROQ 和 General Translation 的文档级本地化模型查询已翻译的 Sanity 文档。 related: links: - /docs/integrations/sanity/guides/translating-content - /docs/integrations/sanity/guides/managing-translations - /docs/integrations/sanity/guides/configuring-sanity --- General Translation 会将 Sanity 的翻译内容存储为独立文档,并为每个文档标记一个语言字段 (默认为 `language`) 。 只需按该字段筛选,即可查询所需的版本。你现有的源语言查询仍可继续使用。 ## 查询源内容 [#query-source] 默认情况下,source document 不会设置 语言字段,因此现有的源查询无需修改。 ```text *[_type == "article"]{ title, slug, body } ``` ## 查询目标区域设置 [#query-locale] 按语言字段筛选。 ```text *[_type == "article" && language == "es"]{ title, slug, body } ``` ## 查询单个翻译后的文档 [#query-one] ```text *[_type == "article" && slug.current == "hello-world" && language == "es"][0]{ title, body } ``` 如果你对翻译后的 slug 进行去重,slug 会包含区域设置后缀,例如 `hello-world-es`。 ## 查询所有语言版本 [#query-all] ```text *[_type == "article"]{ title, slug, body, language } ``` ## 同时查询源文档和译文 [#query-both] 由于源文档可能未定义语言字段,因此请同时包含源区域设置和未定义值。 ```text *[_type == "article" && (language == "en" || !defined(language))]{ title, slug, body } ``` ## 使用自定义 语言字段 [#custom-field] 如果插件配置中设置了 `languageField: 'locale'`,请查询 `locale`,而不是 `language`。schema 字段和 query 字段必须与配置的值一致。 ```text *[_type == "article" && locale == "es"]{ title, slug, body } ``` ## 查询翻译后的引用 [#query-references] 在引用完成修补后,翻译后的文档会指向所引用文档的翻译版本。像平常一样查询即可。 ```text *[_type == "article" && language == "es"]{ title, "author": author->{ name, language } } ``` 有关引用如何被重写,请参见[补丁引用](/docs/integrations/sanity/guides/managing-translations#patch-references)。 ## Next steps - /docs/integrations/sanity/guides/translating-content - /docs/integrations/sanity/guides/managing-translations - /docs/integrations/sanity/guides/configuring-sanity