Ein Baum und seine Abenteuer



Contentful import to a different locale

After my recent undertaking to migrate a blog from Wordpress to Contentful I was also faced with the challenge to import content to a Space that had a different locale than the Space it was exported from. This came about after we setup a "base" space for our Product that is supposed to be rolled out in multiple countries.

Once again I wanted to use the Contentful-import tool to get this job done. However soon I kept seeing:

✖ Checking if destination space already has any content and retrieving it
   →       Default locale for destination space: es-ES

And while Contentful-bootstrap supports a --locale flag to do this it is a now unsupported Ruby based tool. As a webdeveloper I obviously want to use a Node based tool like Contentful-import.

It the solution is quite easy, we simply need to replace each occurrence of the tarting locale with the desired one. Using a regular expression on the JSON representation of our exported space makes this a breeze.

const desiredLocale = 'es-ES'
const replacesLocaleCodes = space.replace(/en-US/g, desiredLocale)

Super simple really. After running our exported Space through this we can run our import with it and succeed 🎉

Gotchas

However we will now find that our Space has a default locale called "English" that is the Spanish language 😕

To change that we need to replace the locale object in the .locale attribute of our exported space with the correct one for our target space. Assuming we have an array of locales exported from Contentful we can achieve this quite easily.

const targetLocale = 'es-ES'

// Assuming array locales with locale objects exported from Contentful.
if (!locales.find((locale) => locale.code === targetLocale)) {
  console.log(`Sorry, but we don't support ${targetLocale}`)
}

// Again assuming you have a way to get your space.
const space = await getExportedSpace()
const replacesLocaleCodes = space.replace(/en-US/g, targetLocale)
const parsedSpace = JSON.parse(replacesLocaleCodes)
parsedSpace.locales = [locales.find((locale) => locale.code === targetLocale)]
console.log(JSON.stringify(parsedSpace, null, 2))

Creating a util

Because this is bothersome and I am probably not the only one facing this problem you can find my solution on Github and install it via npm 🧙‍

npm install -g @hoverbaum/contentful-locale

contentful-locale exportedSpace.json target-locale -s output.json

You can also find the original code as a Gist.

Author

Portrait picture of Hendrik

I am a JavaScript and GenAI Enthusiast; developer for the fun of it!
Here I write about webdev, technology, personal thoughts and anything I finds interesting.

More about me

Read next

How I fell in love with an API-first CMS

Falling in love - Image by Contentful
Falling in love - Image by Contentful

The CMS (Content Management System) was one of the first building blocks of the content driven web. The CMS marked the move away from hardcoded HTML pages, and towards our modern web in which everyone has become a content creator. They are great for businesses because the competence of building websites and managing content could not only be split in theory, but also in practice. With a CMS, we can update our website on the fly — so there is really no justification for not using a CMS.

Wordpress to Contentful migration

Lets go on a journey.
Lets go on a journey.

Contentful is one of the most prominent headless CMSs at the moment. As such it provides you all the content management capabilities of traditional tools like WordPress while decoupling the delivery of this content via an API that can be used to retrieve content.

Headless CMS are quickly turning from the new kid on the block intro everyone's favourite. And I totally understand why: it makes thinking about building a frontend so much easier, for once you actually know what is happening and we have great separation of concerns. For me as a webdev this is great stuff!

Holiday greetings with GenAI

Festive Greetings - ChatGPT and Midjourney
Festive Greetings - ChatGPT and Midjourney

Happy Holidays and festive greetings, powered by ChatGPT, Midjourney and a little bit of Photoshop.

Utilizing my Custom GPT for Midjourney prompts (open source on GitHub), I generated the image and some subtle variations in three rounds. Finally touching it up with a tagline in Photopea.