Tips on fixing server/client content mismatch caused by browser auto translate

While constructing a website with next14, storyblok, and i18n, I encountered an issue in the browser related to the automatic Google translate feature for Georgian and English languages. Despite setting Georgian as the default language in my app, when switching to English, the server content attempts to render in English while the client remains stuck on Georgian. How can I resolve this?

Errors - Prop lang did not match.

Server: "en" Client: "ka"

The Next14 middleware (app router) has been configured according to the documentation provided.

import { NextResponse } from "next/server";
 
let locales = ["ka", "en"]
 
function getLocale(request) { 
  return "ka"
}
 
export function middleware(request) {
  // Check if there is any supported locale in the pathname
  const { pathname } = request.nextUrl
  const pathnameHasLocale = locales.some(
    (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
  )
 
  if (pathnameHasLocale) return
 
  // Redirect if there is no locale
  const locale = getLocale(request)
  request.nextUrl.pathname = `/${locale}${pathname}`
  // e.g. incoming request is /products
  // The new URL is now /en-US/products
  return NextResponse.redirect(request.nextUrl)
}
 
export const config = {
  matcher: [
    // Skip all internal paths (_next)
    '/((?!_next).*)',
    // Optional: only run on root (/) URL
    // '/'
  ],
}

The pages accept the lang parameter from the parameters provided.

export default async function Home({params: {lang} }) {

You can find the complete repository at https://github.com/BryanMF87/bokeria

I have attempted to modify the content of getLocale but am unsure of how to adjust it to default to using "ka."

I also tried modifying the root layout html lang="ka" to html lang={lang} so that it utilizes the value of {lang} from the parameters. My intention was for this to enable the client to adapt to the user's language preference.

Answer №1

While it may not be the perfect solution in my opinion, I consider it a satisfactory workaround. I simply included translate="no" in my html tag.

With this addition, the browser's auto translate function continues to activate, but is unable to automatically translate the content, thus avoiding any potential errors due to mismatched content between server and client.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search