Skip to main content

文档服务 API:使用 locale 参数

🌐 Document Service API: Using the locale parameter

Page summary:

文档服务 API 中的 locale 参数允许你使用 findOne()findMany()update()delete() 等方法查询、创建、更新、删除、发布和取消发布特定语言版本的文档。

🌐 The locale parameter in the Document Service API lets you query, create, update, delete, publish, and unpublish documents for specific language versions using methods like findOne(), findMany(), update(), and delete().

默认情况下,Document Service 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()

strapi.documents().findOne()

Get a locale version with findOne()

Pass a locale to findOne() to get the version of the document for that locale.

如果没有传递 status 参数,则默认返回 draft 版本。

🌐 If no status parameter is passed, the draft version is returned by default.

获取带有 findFirst() 的本地化版本

🌐 Get a locale version with findFirst()

strapi.documents().findFirst()

Get a locale version with findFirst()

Pass a locale to findFirst() to return documents matching that locale.

如果没有传递 status 参数,则默认返回 draft 版本。

🌐 If no status parameter is passed, the draft version is returned by default.

使用 findMany() 获取本地化版本

🌐 Get locale versions with findMany()

如果没有传递 status 参数,则默认返回 draft 版本。

🌐 If no status parameter is passed, the draft versions are returned by default.

strapi.documents().findMany()

Get locale versions with findMany()

Pass a locale to findMany() to return all documents that have this locale available.

解释:

给定以下 4 个具有不同语言环境的文档:

🌐 Given the following 4 documents that have various locales:

  • 文件A:
    • en
    • fr
    • it
  • 文件 B:
    • en
    • it
  • 文件 C:
    • fr
  • 文件 D:
    • fr
    • it

findMany({ locale: 'fr' }) 只会返回那些有 'fr' 语言版本的文档的草稿版本,即文档 A、C 和 D。

create() 为一个区域创建文档

🌐 create() a document for a locale

strapi.documents().create()

Create a document for a locale

Pass a locale to create() to create the document for that specific locale.

update() 本地化版本

🌐 update() a locale version

strapi.documents().update()

Update a locale version

Pass a locale to update() to update only that specific locale version of a document.

delete() 语言区域版本

🌐 delete() locale versions

使用 Document Service API 的 delete() 方法locale 参数仅删除某些语言版本。除非传入特定的 status 参数,否则这将删除草稿和已发布版本。

🌐 Use the locale parameter with the delete() method of the Document Service API to delete only some locales. Unless a specific status parameter is passed, this deletes both the draft and published versions.

删除一个语言版本

🌐 Delete a locale version

strapi.documents().delete()

删除一个语言版本

Pass a locale to delete() to delete only that specific locale version of a document.

删除所有语言版本

🌐 Delete all locale versions

strapi.documents().delete()

删除所有语言版本

Use the * wildcard with the locale parameter to delete all locale versions of a document.

publish() 语言版本

🌐 publish() locale versions

要使用文档服务 API 的 publish() 方法 仅发布文档的特定语言版本,请将 locale 作为参数传递:

🌐 To publish only specific locale versions of a document with the publish() method of the Document Service API, pass locale as a parameter:

发布本地化版本

🌐 Publish a locale version

strapi.documents().publish()

发布本地化版本

Pass a locale to publish() to publish only that specific locale version of a document.

发布所有语言版本

🌐 Publish all locale versions

strapi.documents().publish()

发布所有语言版本

Use the * wildcard with the locale parameter to publish all locale versions of a document.

unpublish() 语言版本

🌐 unpublish() locale versions

要使用文档服务 API 的 unpublish() 方法 仅发布文档的特定语言版本,请将 locale 作为参数传递:

🌐 To publish only specific locale versions of a document with the unpublish() method of the Document Service API, pass locale as a parameter:

取消发布本地版本

🌐 Unpublish a locale version

strapi.documents().unpublish()

取消发布本地版本

Pass a locale to unpublish() to unpublish only that specific locale version of a document.

取消发布所有语言版本

🌐 Unpublish all locale versions

strapi.documents().unpublish()

取消发布所有语言版本

Use the * wildcard with the locale parameter to unpublish all locale versions of a document.

strapi.documents().unpublish()

Unpublish with fields selection

Unpublish a document while selecting specific fields to return.

discardDraft() 用于本地化版本

🌐 discardDraft() for locale versions

要仅丢弃某些语言版本文档的草稿数据,使用文档服务 API 的 discardDraft() 方法,传递 locale 作为参数:

🌐 To discard draft data only for some locales versions of a document with the discardDraft() method of the Document Service API, pass locale as a parameter:

丢弃本地化版本的草稿

🌐 Discard draft for a locale version

strapi.documents().discardDraft()

丢弃本地化版本的草稿

Pass a locale to discardDraft() to discard draft data for that specific locale version.

丢弃所有语言版本的草稿

🌐 Discard drafts for all locale versions

strapi.documents().discardDraft()

丢弃所有语言版本的草稿

Use the * wildcard with the locale parameter to discard drafts for all locale versions of a document.

count() 个本地化文档

🌐 count() documents for a locale

要统计特定语言环境的文档,请将 locale 与其他参数一起传递给文档服务 API 的 count() 方法

🌐 To count documents for a specific locale, pass the locale along with other parameters to the count() method of the Document Service API.

如果没有传递 status 参数,则统计草稿文档(即该语言环境下可用文档的总数,因为即使已发布的文档也会被计为有草稿版本):

🌐 If no status parameter is passed, draft documents are counted (which is the total of available documents for the locale since even published documents are counted as having a draft version):

// Count number of published documents in French
strapi.documents('api::restaurant.restaurant').count({ locale: 'fr' });