Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to programmatically create translated documents? #125

Open
jorrit91 opened this issue May 16, 2023 · 1 comment
Open

Is it possible to programmatically create translated documents? #125

jorrit91 opened this issue May 16, 2023 · 1 comment

Comments

@jorrit91
Copy link

jorrit91 commented May 16, 2023

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?

@Pjaerr
Copy link

Pjaerr commented Feb 28, 2024

@jorrit91 Did you ever find a solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants