Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Sanity

Translate your Sanity website built with Next.js with Weglot.

Make your Sanity + Next.js website multilingual with the Weglot React package. Text written in your code is translated when your website is built, and content coming from Sanity is translated server-side when the page renders. Languages and translations are managed in your Weglot 2.0 project.

1. Set up the package

Follow Getting started in the folder that contains your Next.js website (often frontend in Sanity starters). Point sourceRoot to the folder that contains all your pages and components, for example ./app.

Text written in your code is now translated. Content coming from Sanity needs one more step.

2. Translate Sanity content

Translate the fields you fetch from Sanity with the dynamic content functions. Portable Text is translated as HTML: convert it with @portabletext/to-html first. Optional fields can be empty, so pass ?? "" for them.

npm install @portabletext/to-html
// app/posts/[slug]/page.tsx
import { tDynamicAll, tDynamicHtml } from "@weglot/translate-react/dynamic";
import { toHTML } from "@portabletext/to-html";
import { client } from "@/sanity/client";

export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const post = await client.fetch(POST_QUERY, { slug });

  const [[title, excerpt], body] = await Promise.all([
    tDynamicAll([post.title ?? "", post.excerpt ?? ""]),
    tDynamicHtml(toHTML(post.body ?? [])),
  ]);

  return (
    <article>
      <h1>{title}</h1>
      <p>{excerpt}</p>
      <div dangerouslySetInnerHTML={{ __html: body }} />
    </article>
  );
}

Translate SEO fields in generateMetadata the same way:

3. Check the shared fields

Content that appears on every page is easy to miss. Make sure these fields go through tDynamic, tDynamicAll or tDynamicHtml:

  • navigation and footer;

  • buttons and calls to action;

  • page headers;

  • SEO fields and image alt texts.

4. Skip translation in draft mode

With Visual Editing, Sanity encodes invisible characters in the strings of draft content. Translating them would break click-to-edit and send your drafts to Weglot. Serve the source text in draft mode:

Next steps

Last updated