Issues with `i18n_lang` field in Sanity document internationalization plugin
14 replies
Last updated: Feb 15, 2022
S
I am implementing the
sanity-io/document-internationalizationplugin for the first time and am have issues where any english pages have a
i18n_langvalue 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_langfield to be the defaultLanguage value?
Feb 15, 2022, 6:50 PM
S
{ "idStructure": "subpath", "referenceBehavior": "weak", "base": "en", "languages": ["en", "fr"], "fieldNames": { "lang": "i18n_lang", "references": "i18n_refs" } }
initialValueof my base language and manually update all the existing documents to use
en
Feb 15, 2022, 8:05 PM
R
I've got mine set up like this and it works:
I wonder if setting an initial value for the language is what you need?
That would only work for newly created documents, though. You'd have to mutate existing ones to add this value.
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: [...], }
initialValue: { __i18n_lang: 'en_US', },
Feb 15, 2022, 8:12 PM
S
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_langvalue of null.
Feb 15, 2022, 8:14 PM
S
I think I will need to mutate the existing documents to make this work though, I agree
Feb 15, 2022, 8:16 PM
R
Right, it seems like that field isn't being set in your current setup so it's returning
nullwhen you're querying it. The suggestion above should help get around that π
Feb 15, 2022, 8:18 PM
S
Thanks for the help Racheal π
Feb 15, 2022, 8:18 PM
R
For sure! Let us know if you need help with the mutation as well.
Feb 15, 2022, 8:20 PM
S
Actually I would love help with understanding the best way to mutate all unset
i18n_langvalues to be
en. I assume that would be done through the CLI?
Feb 15, 2022, 8:21 PM
R
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
S
Perfect, thank you!
Feb 15, 2022, 8:28 PM
R
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
R
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
S
Amazing, thank you for all your help Racheal
Feb 15, 2022, 9:51 PM
R
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.