Structured Content 2022: Join our conference to explore fresh perspectives on content and digital experiences →

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.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.