1

I am using NextJS 14. When I installed everything for multi language from next-intl and I check from offical website but system is giving couple hydration errors!

  1. created i18n.ts
  2. updated next.config.mjs
  3. created middleware.ts
  4. created [locale] folder and created layout.tsx and page.tsx inside the [locale] folder

all codes are below:

i18n.ts

import {notFound} from 'next/navigation';
import {getRequestConfig} from 'next-intl/server';

// Can be imported from a shared config
const locales = ['en', 'nl', 'de', 'tr'];

export default getRequestConfig(async ({locale}) => {
    // Validate that the incoming `locale` parameter is valid
    if (!locales.includes(locale as any)) notFound();

    return {
        messages: (await import(`./languages/${locale}.json`)).default
    };
});

next.config.mjs

import createNextIntlPlugin from 'next-intl/plugin';

const withNextIntl = createNextIntlPlugin();

/** @type {import('next').NextConfig} */
const nextConfig = {
    images: {
        domains: [
            "avatars.githubusercontent.com",
            "lh3.googleusercontent.com",
            "res.cloudinary.com",
            "images.unsplash.com"
        ]
    }
};

export default withNextIntl(nextConfig);

middleware.ts

import createMiddleware from 'next-intl/middleware';

export default createMiddleware({
    // A list of all locales that are supported
    locales: ['en', 'nl', 'de', 'tr'],

    // Used when no locale matches
    defaultLocale: 'en'
});

export const config = {
    // Match only internationalized pathnames
    matcher: ['/', '/(en|nl|de|tr)/:path*']
};

layout.tsx

import {NextIntlClientProvider} from 'next-intl';
import {getMessages} from 'next-intl/server';

export default async function LocaleLayout({children, params: {locale}}: {
    children: React.ReactNode;
    params: {locale: string};
}) {
    // Providing all messages to the client
    // side is the easiest way to get started
    const messages = await getMessages();

    return (
        <html lang={locale}>
        <body>
        <NextIntlClientProvider messages={messages}>
            {children}
        </NextIntlClientProvider>
        </body>
        </html>
    );
}

page.tsx

import {useTranslations} from 'next-intl';

export default function Index() {
    const t = useTranslations('home_page');
    return (
        <h1>{t('slider_title')}</h1>
    );
}

error

I checked a lot of different website and questions and answers but I couldn't find any correct answer. But I don't now what I need to do?

4
  • Can you update next to 14.2 to see better hydration diff and replace the last screen shot Commented May 12, 2024 at 3:41
  • Upgrade to latest version, but still continue :( ``` "next": "^14.2.3", "next-intl": "^3.13.0", "react": "^18.3.1", "react-dom": "^18.3.1", ``` imgur.com/a/KqRAtRx Commented May 12, 2024 at 16:02
  • Please, share a Minimal, Reproducible repository Commented May 12, 2024 at 16:08
  • Hi Ahmed, thank you first of all. I fixed the problem. Solution was, upgrade nextjs to lastest version but didn't change anything. Delete the project and install nextjs latest version, after that install next-intl and check the all installation steps. Now working. Commented May 12, 2024 at 18:54

1 Answer 1

1

I had the same problem, I had created the [locale] folder and moved everything inside. I thought the problem was in the use of the middleware or with next-i18n-router, the problem was so simple that it took me 4 hours to find it, I had left loading.js outside of [locale]. It seems that everything outside of [locale] causes problems when using the middleware

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.