文档服务 API:使用 locale 参数
¥Document Service API: Using the locale parameter
默认情况下,文档服务 API 返回文档的默认语言环境版本(即 'en',即英语版本,除非为应用设置了其他默认语言环境,请参阅 国际化 (i18n) 功能)。本页介绍如何使用 locale 参数仅针对特定语言环境获取或操作数据。
¥By default the Document Service API returns the default locale version of documents (which is 'en', i.e. the English version, unless another default locale has been set for the application, see Internationalization (i18n) feature). This page describes how to use the locale parameter to get or manipulate data only for specific locales.
获取使用 findOne() 的语言环境版本
¥Get a locale version with findOne()
如果传递了 locale,则文档服务 API 的 findOne() 方法 将返回此语言环境的文档版本:
¥If a locale is passed, the findOne() method of the Document Service API returns the version of the document for this locale:
await strapi.documents('api::restaurant.restaurant').findOne({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
locale: 'fr',
});
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: null, // draft version (default)
locale: "fr", // as asked from the parameters
// …
}
如果没有传递 status 参数,则默认返回 draft 版本。
¥If no status parameter is passed, the draft version is returned by default.
获取使用 findFirst() 的语言环境版本
¥Get a locale version with findFirst()
要使用文档服务 API 返回特定语言环境,同时 查找第一个文档 与参数匹配:
¥To return a specific locale while finding the first document matching the parameters with the Document Service API:
const document = await strapi.documents('api::article.article').findFirst({
locale: 'fr',
});
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article"
// …
}
如果没有传递 status 参数,则默认返回 draft 版本。
¥If no status parameter is passed, the draft version is returned by default.
获取使用 findMany() 的语言环境版本
¥Get locale versions with findMany()
当将 locale 传递给文档服务 API 的 findMany() 方法 时,响应将返回所有具有此语言环境的文档。
¥When a locale is passed to the findMany() method of the Document Service API, the response will return all documents that have this locale available.