Issues with `i18n_lang` field in Sanity document internationalization plugin

14 replies
Last updated: Feb 15, 2022
I am implementing the
sanity-io/document-internationalization
plugin for the first time and am have issues where any english pages have a
i18n_lang
value of
null
. Then the created french ones are set to
fr
. This causes difficulties in my queries on the front end. Is there any sort of setting in the plugin to set the
i18n_lang
field to be the defaultLanguage value?
Feb 15, 2022, 6:50 PM
{
  "idStructure": "subpath",
  "referenceBehavior": "weak",
  "base": "en",
  "languages": ["en", "fr"],
  "fieldNames": {
    "lang": "i18n_lang",
    "references": "i18n_refs"
  }
}
I have it set up like this with a field for i18n_lang added in each document. I get the sense that I may need to make that field have an
initialValue
of my base language and manually update all the existing documents to use
en
Feb 15, 2022, 8:05 PM
I've got mine set up like this and it works:
export default {
  type: 'document',
  name: '...',
  title: '...',
  i18n: {
    base: 'en_US',
    languages: ['en_US', 'nl_NL'],
    fieldNames: {
      lang: '__i18n_lang',
      references: '__i18n_refs',
      baseReference: '__i18n_base',
    },
  },
  fields: [...],
}
I wonder if setting an initial value for the language is what you need?

  initialValue: {
    __i18n_lang: 'en_US',
  },
That would only work for newly created documents, though. You'd have to mutate existing ones to add this value.
Feb 15, 2022, 8:12 PM
To clarify, everything works properly in Sanity, it's just querying things in graphql from the front end if I pass in the language (
en
) it doesn't return the things that have a
i18n_lang
value of null.
Feb 15, 2022, 8:14 PM
I think I will need to mutate the existing documents to make this work though, I agree
Feb 15, 2022, 8:16 PM
Right, it seems like that field isn't being set in your current setup so it's returning
null
when you're querying it. The suggestion above should help get around that πŸ™‚
Feb 15, 2022, 8:18 PM
Thanks for the help Racheal πŸ™‚
Feb 15, 2022, 8:18 PM
For sure! Let us know if you need help with the mutation as well.
Feb 15, 2022, 8:20 PM
Actually I would love help with understanding the best way to mutate all unset
i18n_lang
values to be
en
. I assume that would be done through the CLI?
Feb 15, 2022, 8:21 PM
Let me put together an example for you! I'm stepping out for lunch now, but I'll crank it out when I get back.
Feb 15, 2022, 8:27 PM
Perfect, thank you!
Feb 15, 2022, 8:28 PM
I like to use the JS client to write a script that I can then execute in the terminal. This is how I'd handle it:
import client from "part:@sanity/base/client"
import cq from 'concurrent-queue'

// Create a queue to limit the rate at which you write changes to Sanity
let queue = cq().limit({ concurrency: 2 }).process(task => {
  return new Promise(function (resolve, reject) {
      setTimeout(resolve.bind(undefined, task), 1000)
  })
})

const studioClient = client.withConfig({apiVersion: '2021-03-25'})

const mutateDocs = async () => {
  //Fetch the documents you need to mutate
  const query = `*[_type == '<your-type>' && !defined('i18n_lang')]`
  const docs = await studioClient.fetch(query)
  // Loop through all of the docs returned from our query
  for (const doc of docs){
    queue(doc).then(async () => {
    // Add a message to help us know the upload is happening
      console.log(`Mutating ${doc._id}`)
      // Tell the client to patch the current document
      studioClient.patch(doc._id)
        // Set the field
        .set({
          i18n_lang: 'en'
        })
        // Commit the changes
        .commit()
        .then(updatedDoc => console.log(`Hurray, the doc is updated! New document:${updatedDoc._id}`))
        .catch(err => console.error('Oh no, the update failed: ', err.message))
    })
  }
}

mutateDocs()

// execute this script by running 
// $ sanity exec ./path/to/mutateDocs.js --withUserToken
Feb 15, 2022, 9:08 PM
You'll have to install concurrent-queue and make sure you're within your Studio's directory for this to work.
Feb 15, 2022, 9:10 PM
Amazing, thank you for all your help Racheal
Feb 15, 2022, 9:51 PM
You're welcome!
Feb 15, 2022, 10:09 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?