I am trying to update a record using the API, following the guidelines
My record has several languages available, only 'es-ES' mandatory.
Iām first creating the record in Spanish, and then trying to update it to add its English 'en' version.
When I perform a simple update operation, the function in returning following response:
Iām trying to reproduce the error locally, but havenāt been able to yet.
Could you please verify for me that:
Your project only has the two locales, es-ES and en? No other ones?
Your modelās configuration for āAll locales required?ā is set to OFF
Youāre using an appropriate API key with no locale restrictions?
Also, could you please share with me your projectās ID / dashboard URL so I can look around inside? You can DM me here (hover over my avatar by this post) or email me at r.tuan@datocms.com if you donāt want to make it public.
Thanks! As soon as I can get a little more detail, Iāll keep looking.
@fruizdiego, I apologize. Please disregard my previous post.
After some internal investigation, my colleague @m.finamor made me aware of an underlying issue (and a workaround!)
Despite what the error message says right now*, this is actually likely caused by having other localized fields in a model, but not providing data for them in the CMA update request.
If your model has localized fields title, description, extra_field, then:
Will unfortunately fail, with that cryptic error message. You actually just need to provide data (or nulls) for the other localized fields in that same request, like:
And that should go through. Sorry about that! (And thank you @m.finamor for clarifying that!)
* So if the issue is that all localized fields must be explicitly added to an update request (even just with nulls), why doesnāt the error message say that instead of suggesting thereās something wrong with title when there isnāt?
Good question. Itās a likely bug / oversight on our part. Sorry! Iāll flag it for the developersā attention immediately
In the meantime, please let us know if providing the additional fields in your request helps at all.
I have tried what you suggested and it actually works, thanks! It will do for now although itās not the ideal situation. Here is why:
Our blog is multilanguage. Available locales are:
'es-ES'
en
fr
it
de
'pt-PT'
nl
Not all the records are translated to all languages, but this approach means every post (~550) will be translated to every language and will be there (with null in every field), in the Admin panel.
For clarity and simplicity, the ideal situation for us would be to just have the translation of each post if it has actual content.
I have been checking the docs and you canāt remove a translation via API, can you?
Thanks! I mean itās not the end of the world and the current workaround will do, but this little improvement will make it perfect.
You can actually leave out the locales youāre not working on. HOWEVER, SEE WARNING BELOW.
WARNING: Only follow these instructions if your other locales are EMPTY, or this will clear the other locales.
For example, if your project has en, es-ES, and it, and only es-ES has data right now, you can leave out the others. Whenever you update es-ES, you have to provide content or nulls for all the es-ES localized fields. But you donāt have to provide anything for en or it in those records, and they wonāt show up in the admin panel.
If your other locales already have data, you have to fetch them first and then include their existing data back in your PUT request, even if youāre not touching those other locales. (I know, itās silly.)
For example, if es-ES and it both have data already, you have to do something like this:
const existingRecord = await client.items.find(itemId)
/* existingRecord looks like this:
"field1_localized": {
"it": "italian title",
"es-ES": "original spanish title"
},
"field2_localized": {
"it": "",
"es-ES": "original spanish title for field 2"
}
*/
let modifiedRecord = existingRecord
existingRecord['field1_localized']['es-ES'] = 'newer spanish title'
existingRecord['field2_localized']['es-ES'] = 'newer spanish title for field 2'
client.items.update(itemId,modifiedRecord)
That will update the es-ES fields but leave it with whatever it was before, either filled out or not. en will remain empty in this example because it didnāt have data previously.
I have been checking the docs and you canāt remove a translation via API, can you?
Actually, you can, by just updating that record and leaving out a locale altogether. (Hence the warning). To be clear, if you update es-ES and it and leave out en, en will be removed from that record altogether. Thatās why you have to make sure to fetch a record and pass along its existing data if en DOES have data already.
Thanks! I mean itās not the end of the world and the current workaround will do, but this little improvement will make it perfect.
Sorry itās all a bit confusing! Hopefully the above clears it up a little bit. I did flag this for developer review, though, so letās see if they can make it any better.