Document Service API: locale パラメータ
既定では、Document Service API はドキュメントの既定ロケール版を返します(アプリで別の既定ロケールを設定していなければ 'en'、つまり英語版です。Internationalization (i18n) を参照)。このページでは、特定のロケールだけを対象にデータを取得・更新するために locale をどう使うかを説明します。
findOne() でロケール版を取得する
locale を渡すと、Document Service API の findOne() メソッド は、そのロケールのドキュメント版を返します。
await strapi.documents('api::restaurant.restaurant').findOne({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
locale: 'fr',
});
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: null, // draft version (default)
locale: "fr", // パラメータで指定したロケール
// …
}
status を省略すると、既定で draft 版が返ります。
findFirst() でロケール版を取得する
Document Service API で 最初に一致するドキュメントを取得 するとき、特定のロケール版を返す例です。
const document = await strapi.documents('api::article.article').findFirst({
locale: 'fr',
});
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article"
// …
}
status を省略すると、既定で draft 版が返ります。
findMany() でロケール版を取得する
findMany() メソッド に locale を渡すと、そのロケールが存在するドキュメントがすべて返ります。
status を省略すると、既定で各ドキュメントの draft 版が返ります。
// status: draft が既定
await strapi.documents('api::restaurant.restaurant').findMany({ locale: 'fr' });
[
{
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
name: 'Restaurant Biscotte',
publishedAt: null,
locale: 'fr',
// …
},
// …
];
補足説明:
次の 4 ドキュメントがあり、ロケールの組み合わせがそれぞれ異なるとします。
- ドキュメント A:
- en
fr- it
- ドキュメント B:
- en
- it
- ドキュメント C:
fr
- ドキュメント D:
fr- it
findMany({ locale: 'fr' }) は、fr 版を持つドキュメント A、C、D について、それぞれの下書き版だけを返します。
ロケール向けに create() する
特定ロケール用のドキュメントを作成するには、Document Service API の create メソッド に locale を渡します。
await strapi.documents('api::restaurant.restaurant').create({
locale: 'es', // 省略すると既定ロケールの下書きが作成される
data: { name: 'Restaurante B' },
})
{
documentId: "pw2s0nh5ub1zmnk0d80vgqrh",
name: "Restaurante B",
publishedAt: null,
locale: "es"
// …
}
ロケール版を update() する
ドキュメントの特定ロケール版だけを更新するには、Document Service API の update() メソッド に locale を渡します。
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() する
Document Service API の delete() メソッド で locale を使うと、一部のロケールだけを削除できます。status を明示しない限り、下書きと公開版の両方が削除されます。
1 つのロケール版を削除
await strapi.documents('api::restaurant.restaurant').delete({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
locale: 'es',
});