document-internationalization
document-internationalization copied to clipboard
Is it possible to programmatically create translated documents?
I'm looking for a way to programmatically add pages to our Sanity project, which uses document-internationalization plugin for translations. Every page would have a parent page in English, and a translation in Dutch, only translating some fields (assets, for example, shouldn't be different from the parent page).
Currently, I'm trying to get it to work by using the JavaScript client for Sanity. My input is based on the data structure of this plugin, as found here.
This is my current solution, but it doesn't seem to create a proper connection like creating a new translation through the UI does.
// Create an English version of a page with this input 👇
const englishDoc = {
__i18n_lang: "en",
_id: 'english-id',
_type: "test-item",
title: 'English title',
globalField: 'Global value (same throughout translation)',
urlSlug: {
_type: "slug",
current: 'english-title',
},
}
await sanityClient.createOrReplace(englishDoc)
// Create a Dutch version of the page based on English doc with this input 👇
const dutchDoc = {
__i18n_lang: "en",
_id: 'english-id__18n_nl',
__i18n_base: {
_ref: 'english-id',
_type: "reference",
},
_type: "test-item",
title: 'Dutch title',
urlSlug: {
_type: "slug",
current: 'dutch-title',
},
}
This doesn't resolve any errors, but it doesn't seem to trigger every action needed to make the connection, because:
- In the UI, I still see the option to add a Dutch version of the English page, as though the connection doesn't work properly
- I can see the Dutch document is created (when querying the doc with GROQ), but the globalField isn't visible
🙋♂️ Is there anything I can do to make this work programmatically?
@jorrit91 Did you ever find a solution for this?