Next.js Blog Starter - How to add a Localized `blockContent` in the `post.js` Schema

9 replies
Last updated: Aug 8, 2022
Hello 👋 I’m a total newbie to Sanity and am currently trying it out for a few hours with the Nextjs Blog starter .

I would like to add a localized
blockContent
in the
post.js
schema,
so that users can add body text in two languages.
I successfully localized the title, but can’t do so for the body. After reading about it, I thought duplicating the body field element would be a simple solution


    {
      name: "body",
      title: "English body",
      type: "blockContent",
    },
    {
      name: "body_de",
      title: "German body",
      type: "blockContent",
    },
and then in
[slug.js]
using a ternary to pass the respective version into the boilerplate component

<PostBody _content_={lang === "de" ? post.body : post.body_tr} />

However, the
body_de
content does not make it into the post payload.

What would be the easiest solution do the above? It doesn’t have to be fancy at all, the resulting site will be most simple.
Thanks a lot for any suggestions!
🙏
AI Update

Good news - your approach of duplicating the field is actually a perfectly valid solution for simple localization! The issue you're experiencing where body_de doesn't appear in your post payload is almost certainly a GROQ query problem, not a schema problem.

When you query documents in Sanity, you need to explicitly request the fields you want. The Next.js blog starter likely has a GROQ query that fetches post data, and it's probably only requesting the original body field.

Here's what you need to do:

1. Find your GROQ query

Look in your Next.js project for where posts are being fetched. In the blog starter, this is typically in a lib or sanity folder, often in files like queries.js, queries.ts, or directly in your [slug].js page. You're looking for something like:

*[_type == "post" && slug.current == $slug][0] {
  title,
  body,
  slug,
  // ... other fields
}

2. Add your new field to the projection

Simply add body_de to the list of fields being requested:

*[_type == "post" && slug.current == $slug][0] {
  title,
  body,
  body_de,  // Add this line
  slug,
  // ... other fields
}

3. Make sure you're using the correct field name in your component

I noticed in your example you wrote:

<PostBody content={lang === "de" ? post.body : post.body_tr} />

But you defined the field as body_de, not body_tr. Make sure these match:

<PostBody content={lang === "de" ? post.body_de : post.body} />

This field-level approach is a totally reasonable solution for simple sites, especially when you're just starting out. If you later need more sophisticated localization (many languages, translation workflows, etc.), you can explore document-level localization or the official internationalization plugins, but for a two-language site, your approach is perfectly fine!

I will answer on Monday with more if nobody else chimes in, but in your case I would check out these plugins (the first 2 are my favs) 🙂
Oh, I should have checked out the plugins page, nice! All looking very good, and so far the experience with Sanity is fantastic btw.
Thanks
user J
!
So, do these plugins answer your question? If yes, I would mark this thread as
They do, thanks 👍, even though setting up Document Internationalization has been a little challenge for me so far.
I’m not really sure how to customize Desk Structure (as
explained here ), but I guess I need to read through all the docs regarding it in order to really understand how this example can be applied to the Post schema of the Nextjs Blog Starter .
Desk Structure is one of my strengths now 😉 so I can help.
I have examples of custom desk structures here , which might help you along. But reading through the docs is still necessary, to get your stuff done correctly 🙂
Oh cool, lucky me 🙂
So I guess I’m struggling to understand if/how I should build a custom structure if I wanted to localize a Post type document (so that in Studio, the language select button shows my two languages - right now it’s there, but disabled).

Building on the starter kit, I thought that this would do the trick (which it isn’t, it breaks Studio). But like I said, I haven’t had the time to extensively read the docs on this part


import S from "@sanity/desk-tool/structure-builder";

export default () =>
  S.listItem()
    .title(`Post`)
    .child(
      S.documentList()
        .title(`Title`)
        .schemaType("string")
        .filter('_type == "string" && __i18n_lang == $baseLanguage')
        .params({ baseLanguage: `de_DE` })
        .canHandleIntent(S.documentTypeList("string").getCanHandleIntent())
    );
oh yeah, you need to look at the complete folder 😉You need a
.list()
to see a
.listItem()
!
So look at
…/deskStructure/index.js
and then you will understand the structure you need to build.
You will have to read some guide or the docs to understand what I am doing (which is more of a “look how clean SB code can look!“). A good start is this guide .

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?