noob
June 19, 2019, 7:55pm
1
I want to create a Multilanguage Collection. I make the Api request according to the documentation.
fetch('/api/collections/get/posts?token=xxtokenxx', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
filter: {published:true},
fields: {fieldA: 1, fieldB: 1},
limit: 10,
skip: 5,
sort: {_created:-1},
populate: 1, // resolve linked collection items
lang: 'de' // return normalized language fields (fieldA_de => fieldA)
}))
.then(res=>res.json())
.then(res => console.log(res));
But how do I have to create my collection in the backend?
Thank you!
Make sure, that you enabled languages via config file.
i18n: en # default language, if "en", you can skip this one
languages:
default: English # change "default" option in language switch to "English"
de: Deutsch # second language
enable the “Localized” button in the field options on the collection settings page
Don’t forget to translate your collection items
There is a bug if you use the fields projection. You have to select the fields with language suffix, too.
This should work:
fetch('/api/collections/get/posts?token=xxtokenxx', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
filter: {published:true},
fields: {fieldA: 1, fieldA_de: 1 fieldB: 1, fieldB_de: 1}, // I added the fields lang suffix
limit: 10,
skip: 5,
sort: {_created:-1},
populate: 1, // resolve linked collection items
lang: 'de' // return normalized language fields (fieldA_de => fieldA)
}))
.then(res=>res.json())
.then(res => console.log(res));
1 Like
noob
June 20, 2019, 6:15am
3
Thank you very much!
Had it then found out myself.
Important perhaps to mention, default field without “_lang” all others with.
I could not reproduce. Here is my call with Nuxt, works great.
async asyncData({ app }) {
const { data } = await app.$axios.post(process.env.POSTS_URL,
JSON.stringify({
filter: { published: true },
sort: { _created: -1 },
populate: 1,
lang: app.i18n.locale
}),
{
headers: { ‘Content-Type’: ‘application/json’ }
})
return { posts: data.entries }
noob:
I could not reproduce.
This time, you fetched the whole entry. The bug is present if you use the fields projection.