Document Service API: Using the locale parameter
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.
Get a locale version with 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
// …
}
If no status parameter is passed, the draft version is returned by default.
Get a locale version with findFirst()
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"
// …
}
If no status parameter is passed, the draft version is returned by default.
Get locale versions with 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.
If no status parameter is passed, the draft versions are returned by default.
// Defaults to status: draft
await strapi.documents('api::restaurant.restaurant').findMany({ locale: 'fr' });
[
{
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
name: 'Restaurant Biscotte',
publishedAt: null,
locale: 'fr',
// …
},
// …
];
Explanation:
Given the following 4 documents that have various locales:
- Document A:
- en
fr- it
- Document B:
- en
- it
- Document C:
fr
- Document D:
fr- it
findMany({ locale: 'fr' }) would only return the draft version of the documents that have a ‘fr’ locale version, that is documents A, C, and D.
create() a document for a locale
To create a document for specific locale, pass the locale as a parameter to the create method of the Document Service API:
await strapi.documents('api::restaurant.restaurant').create({
locale: 'es' // if not passed, the draft is created for the default locale
data: { name: 'Restaurante B' }
})
{
documentId: "pw2s0nh5ub1zmnk0d80vgqrh",
name: "Restaurante B",
publishedAt: null,
locale: "es"
// …
}
update() a locale version
To update only a specific locale version of a document, pass the locale parameter to the update() method of the Document Service API:
await strapi.documents('api::restaurant.restaurant').update({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
locale: 'es',
data: { name: 'Nuevo nombre del restaurante' },
});
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Nuevo nombre del restaurante",
locale: "es",
publishedAt: null,
// …
}
delete() locale versions
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.