REST API:locale
国际化 (i18n) 功能 为 REST API 添加了新功能。
¥The Internationalization (i18n) feature adds new abilities to the REST API.
要使用针对某个语言环境的 API 内容,请确保该语言环境已经是 添加到管理面板中的 Strapi。
¥To work with API content for a locale, please ensure the locale has been already added to Strapi in the admin panel.
locale
接口参数 可用于处理仅针对指定语言环境的文档。locale
将语言环境代码作为值(参见 可用语言环境的完整列表)。
¥The locale
API parameter can be used to work with documents only for a specified locale. locale
takes a locale code as a value (see full list of available locales).
如果未定义 locale
参数,它将被设置为默认语言环境。en
是创建新 Strapi 项目时的默认语言环境,但管理面板中的另一个语言环境可以是 设置为默认区域设置。
¥If the locale
parameter is not defined, it will be set to the default locale. en
is the default locale when a new Strapi project is created, but another locale can be set as the default locale in the admin panel.
例如,默认情况下,对 /api/restaurants
的 GET 请求将返回与对 /api/restaurants?locale=en
的请求相同的响应。
¥For instance, by default, a GET request to /api/restaurants
will return the same response as a request to /api/restaurants?locale=en
.
下表列出了 i18n 添加到 REST API 的新可能用例并给出了语法示例(你可以单击请求跳转到包含更多详细信息的相应部分):
¥The following table lists the new possible use cases added by i18n to the REST API and gives syntax examples (you can click on requests to jump to the corresponding section with more details):
- For collection types
- For single types
使用案例 | 语法示例 和更多信息的链接 |
---|---|
获取特定语言环境中的所有文档 | GET /api/restaurants?locale=fr |
获取文档的特定语言环境版本 | GET /api/restaurants/abcdefghijklmno456?locale=fr |
为默认语言环境创建新文档 | POST /api/restaurants + 在请求正文中传递属性 |
为特定语言环境创建新文档 | POST /api/restaurants + 在请求正文中传递属性和语言环境 |
为现有文档创建新的或更新现有的语言环境版本 | PUT /api/restaurants/abcdefghijklmno456?locale=fr + 在请求正文中传递属性 |
删除文档的特定语言环境版本 | DELETE /api/restaurants/abcdefghijklmno456?locale=fr |
使用案例 | 语法示例 和更多信息的链接 |
---|---|
获取文档的特定语言环境版本 | GET /api/homepage?locale=fr |
为现有文档创建新的或更新现有的语言环境版本 | PUT /api/homepage?locale=fr + 在请求正文中传递属性 |
删除文档的特定语言环境版本 | DELETE /api/homepage?locale=fr |
GET
获取特定语言环境中的所有文档
¥GET
Get all documents in a specific locale
GET http://localhost:1337/api/restaurants?locale=fr
{
"data": [
{
"id": 5,
"documentId": "h90lgohlzfpjf3bvan72mzll",
"Title": "Meilleures pizzas",
"Body": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "On déguste les meilleures pizzas de la ville à la Pizzeria Arrivederci."
}
]
}
],
"createdAt": "2024-03-06T22:08:59.643Z",
"updatedAt": "2024-03-06T22:10:21.127Z",
"publishedAt": "2024-03-06T22:10:21.130Z",
"locale": "fr"
}
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 1
}
}
}
GET
获取特定语言环境中的文档
¥GET
Get a document in a specific locale
要获取给定语言环境中的特定文档,请在查询的 locale
参数之后添加:
¥To get a specific document in a given locale, add the locale
parameter to the query:
使用案例 | 语法格式和更多信息的链接 |
---|---|
在集合类型中 | GET /api/content-type-plural-name/document-id?locale=locale-code |
在单一类型中 | GET /api/content-type-singular-name?locale=locale-code |