In this article, you will learn how to translate your Sanity website with Weglot, step by step.
This guide is for Sanity websites built with Next.js, the most common setup for Sanity projects. It uses Weglot's npm package, @weglot/translate-react, which translates both the text written in your website's code and the content your team publishes in Sanity Studio.
The installation is done once, in your website's code. Your team then manages every translation from the Weglot dashboard.
Using an AI coding assistant? First, follow step 1 to create your Weglot project and copy your project key. Then your assistant (Cursor, Claude Code, GitHub Copilot or any other) can handle the rest: skip steps 2 to 5 and paste our ready-made prompt from the AI coding assistant section at the end of this article.
The@weglot/translate-reactpackage is currently in beta. If you notice anything unexpected, contact us at help@weglot.com.
Before you start
You'll need:
- A Weglot account (you can create one for free during step 1)
- A Sanity website with a Next.js front end using the App Router (Next.js 15 or 16, React 19)
- Access to your website's code
- Access to your hosting settings (for example, Vercel or Netlify) to add an environment variable
Step 1: Create your Weglot project
If you don't have a Weglot account yet, create one at auth.weglot.com, then create your workspace and a new project.
-
Enter your website URL. Use the live URL of your Sanity website.

-
Choose your project's technology. Weglot may not detect Sanity automatically. In the "Project's technology" section, select Other.
- Choose your languages. Select the original language of your website and the language(s) you want to translate it into.

- Define your Brand Voice. Weglot scans your website and suggests a Brand Voice, which the AI Translation Model uses to keep your tone and style in every language. A sample translation appears on the right: adjust your Brand Voice and check how the result changes.
-
Add your Translation Guidelines. Use the suggested Guidelines or write your own (tone of voice, words to keep untranslated, formal or informal style).

-
Copy your project key. You'll reach the "Install your snippet" page. You won't paste this snippet: the npm package replaces it. You only need the project key inside it, which starts with
pk_(for examplepk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx). Copy it and keep this page open, you'll come back to it in step 7.

Step 2: Install the package and add your project key
Open a terminal in your Next.js project folder. In most Sanity projects, the website and Sanity Studio live in two separate folders (often called frontend and studio): use the website one.
Install the package:
npm install @weglot/translate-react@next
Then add your project key to your local environment file, .env.local, at the root of the same folder:
WEGLOT_API_KEY=pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Keep this file private: never commit it to GitHub or share it publicly. Next.js projects ignore
.env.localfiles by default, but it's worth double-checking your.gitignore.
Step 3: Connect Weglot to Next.js
This step translates all the text written in your code (menus, buttons, labels, footer). It takes two small changes.
1. Wrap your Next.js configuration. Open next.config.ts and wrap your existing configuration with withWeglot:
import type { NextConfig } from "next";
import { withWeglot } from "@weglot/translate-react/next";
const nextConfig: NextConfig = {
// your existing configuration stays here
};
export default async function (): Promise<NextConfig> {
return await withWeglot(nextConfig, { sourceRoot: "./app" });
}
sourceRoot tells Weglot where your code lives. Point it to the folder that contains all your pages and components. If your components sit outside app (for example, in a components folder next to it), use the parent folder instead.
2. Wrap your root layout. Open app/layout.tsx and wrap the content of <body> with WeglotProvider:
import { WeglotProvider } from "@weglot/translate-react/react";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<WeglotProvider>{children}</WeglotProvider>
</body>
</html>
);
}
You don't need to list your languages anywhere in the code: Weglot reads them from your project settings.
Keeping a word untranslated. To keep your brand name or any element in its original language, add translate="no" to it, for example <span translate="no">Example</span>. You can also list words to keep untranslated in your Translation Guidelines.
Step 4: Translate your Sanity content
This is the Sanity-specific step. Content created in Sanity Studio (pages, blog posts, FAQs...) is loaded when the page is displayed, so Weglot can't detect it in your code. Instead, you pass each Sanity field through a translation function on the server side.
The package provides three functions:
| Function | Use it for |
tDynamic(text) |
A single text field: a title, a button label, an SEO description |
tDynamicAll([text1, text2]) |
Several fields at once, in a single request (faster) |
tDynamicHtml(html) |
Rich text converted to HTML, keeping its formatting (bold, links, lists) |
Here's an example for a blog post page:
import { tDynamic, tDynamicAll, tDynamicHtml } from "@weglot/translate-react/dynamic";
import { toHTML } from "@portabletext/to-html";
import { client } from "@/sanity/client";
export default async function PostPage({ params }) {
const post = await client.fetch(POST_QUERY, { slug: params.slug });
const [title, excerpt] = await tDynamicAll([post.title, post.excerpt]);
const body = await tDynamicHtml(toHTML(post.body));
return (
<article>
<h1>{title}</h1>
<p>{excerpt}</p>
<div dangerouslySetInnerHTML={{ __html: body }} />
</article>
);
}
Rich text (Portable Text). Sanity stores rich text as Portable Text. To translate it while keeping its formatting, convert it to HTML first (for example with @portabletext/to-html), then pass it to tDynamicHtml.
Don't forget the less visible fields. Make sure every Sanity field displayed on your website goes through one of these functions, including:
- Navigation and footer content stored in Sanity
- Buttons and links inside page sections
- Page headers and intro texts (blog listing, contact page...)
- SEO fields (meta title and description, in
generateMetadata) - Image alt texts
These functions run on the server only, so your project key is never exposed to visitors.
Step 5: Add a language switcher
Your visitors need a way to change language. The package includes a ready-made switcher. Create a small component and place it in your header:
"use client";
import { LocaleSwitcher } from "@weglot/translate-react/react";
export function LanguageSwitcher() {
return <LocaleSwitcher locales={["en", "fr", "es"]} />;
}
List the same languages as in your Weglot project.
Prefer your own design? Build a custom switcher with useWeglotContext, which gives you the current language (locale) and a function to change it (setLocale).
Step 6: Test locally, then deploy
Test on your computer. Start your website as usual:
npm run dev
Open your local website and switch languages. The first time, some texts may still show in the original language while Weglot generates their translations: reload the page after a few seconds.
Then open your Weglot dashboard: your translations appear in the Translation List, where you can review and edit them, or check them in context with the Visual Editor.
Deploy to production.
- In your hosting settings, add the environment variable
WEGLOT_API_KEYwith your project key. On Vercel: Settings > Environment Variables. - Push your changes to your repository. Your host rebuilds the website automatically.
During the build, Weglot generates any missing translations. The build may take a little longer than usual while Weglot generates new translations.
Step 7: Verify your installation
Once your website is live with the new version, go back to the "Install your snippet" page in your Weglot dashboard and click Verify my installation.

Make sure your deployment is finished before clicking: the verification checks your live website, not your local version. If there is an error, you'll see a button to relaunch the verification, and another one to contact our support team.
Optional: Use language URLs (/en, /fr) for SEO
By default, the package remembers each visitor's language in a cookie, so all languages share the same URLs. That works well for visitors, but search engines can then only index one version of each page.
To get each language indexed on Google, give each language its own URL, such as yoursite.com/en/pricing and yoursite.com/fr/pricing. This currently requires a small custom setup in your Next.js project:
-
Read the language from the URL. Add Next.js middleware (called proxy in Next.js 16) that reads the language prefix (
/en,/fr...) and sets the visitor's language accordingly. Redirect/to your default language. - Keep the language in every link. Make your language switcher and internal links keep the current language prefix.
-
Tell search engines about each version. Add
hreflangtags and a canonical URL on every page, and list all language versions in your sitemap.
Tip: an AI coding assistant can set this up for you. See the prompt in the next section.
Shortcut: Install with an AI coding assistant
If you use an AI coding assistant (Cursor, Claude Code, GitHub Copilot or any other), you can let it handle steps 2 to 5. Open your project in it, paste the prompt below and replace the placeholder with your project key. Review the proposed changes before accepting them.
Install the @weglot/translate-react package in this Sanity + Next.js project, following its README (npm install @weglot/translate-react@next), in the folder that contains the Next.js website.
1. Check that Next.js (15 or 16) and React (19) versions are compatible.
2. Add WEGLOT_API_KEY=pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx to the .env.local file and make sure it is never committed.
3. Wrap next.config.ts with withWeglot, with a sourceRoot covering all pages and components.
4. Wrap the root layout with WeglotProvider and add the LocaleSwitcher in the header, with the languages of my Weglot project.
5. Translate every field coming from Sanity server-side with tDynamic, tDynamicAll or tDynamicHtml from @weglot/translate-react/dynamic: page sections, navigation, footer, buttons, page headers, SEO metadata and image alt texts. Convert Portable Text to HTML before tDynamicHtml to keep its formatting.
6. If pages, layouts or data fetches are cached or statically generated, make the cache depend on the visitor's language.
7. Format dates in the visitor's current language.
8. Optional: add language URLs (/en, /fr...) with middleware, keep the prefix in all links, and add hreflang tags, canonical URLs and both languages in the sitemap.
Explain each change simply.Need help?
If you have any difficulty setting up Weglot on your Sanity website, contact us at help@weglot.com. Mention that you're using the @weglot/translate-react package with Sanity and Next.js, so we can help you faster.