Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

how to implement localization on blocks?

4 replies
Last updated: Oct 5, 2020
Hi!Does anyone have an idea on how to implement localization on blocks? I tried doing it with a similar approach to this
https://www.sanity.io/docs/localization but I’m having trouble with the queries in Gatsby, I could do a work around and get the data, but maybe someone knows a better way?
Thanks!
Sep 28, 2020, 8:21 PM
An approach I’ve considered for localization of a full site, is to just have a seperate dataset … I don’t know how that would compare, but think it could be the cleanest approach.
Sep 28, 2020, 9:03 PM
Since I don’t need the localization for the entire site, I’m not sure it’s the best approach for me. But thanks for responding, Ben.
Sep 28, 2020, 10:12 PM
Hi Ximena, what’s your current approach? Something like this?
import supportedLanguages from './supportedLanguages'

export default {
  name: 'localeBlockContent',
  type: 'object',
  fieldsets: [
    {
      title: 'Translations',
      name: 'translations',
      options: {collapsible: true}
    }
  ],
  fields: supportedLanguages.map(lang => ({
    title: lang.title,
    name: lang.id,
    type: 'blockContent',
    fieldset: lang.isDefault ? null : 'translations'
  }))
}
This would give you essentially the same structure as with the locale fields from the example, nesting the block content in the locale codes. If you’ve set up a utility to deeply localise entire documents (
https://www.sanity.io/docs/localization#deeply-localizing-an-entire-document-545b44e4b950 ), you won’t have to specify things further.
One direction would then be:

import BlockContent from '@sanity/block-content-to-react'
...
<BlockContent blocks={data.sanityPost._rawContent} serializers={serializers} />
...
export const query = graphql`
  query myPostQuery {
    sanityPost(_id: { eq: "myPostId" }) {
      _rawContent
    }
  }
`
Sep 29, 2020, 5:51 AM
Thank you Peter! I was having trouble because of weird nesting I think, but this helped a lot 😁
Oct 5, 2020, 3:56 AM

Sanity– build remarkable experiences at scale

Sanity is a modern 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?