Next.js Discord

Discord Forum

How to handle Internatinalization on Root Page.js

Unanswered
Donskoy posted this in #help-forum
Open in Discord
Avatar
DonskoyOP
Hello, I have app router app > page.js where my landing page stays. now as SSG site. I also want to enable the internationalization. but in the documentation it mentioned that we need to create [lang] directory. but that leads to URL changes. and I don't want to change URL for default language which is en-US. I have shared code snippet of app > [lang] > page.js which is shared by next js documentation site. but it is not ideal for app > page.js .

export async function generateStaticParams() {
return [{ lang: 'en-US' }, { lang: 'de' }]
}

export default function Root({ children, params }) {
return (
<html lang={params.lang}>
<body>{children}</body>
</html>
)
}

basically i want, abc.com not abc.com/en

20 Replies

Avatar
Have you tried to change structure to app >[locale] >page.js? By using or the next-intl either without it, with custom middleware which can handle the requests with the locale and rewrite with locale.
Avatar
DonskoyOP
no i didn't. can you share example or something
Avatar
check this structure
Image
and the middleware code with using next-intl

const handleI18nRouting = createIntlMiddleware({
locales: ['en', 'ru', 'de'],
defaultLocale: 'en',
localePrefix: 'as-needed',
localeDetection: false,
alternateLinks: false,
});

export default async function middleware(request: NextRequest) {
const response = handleI18nRouting(request);


return response;
}
Avatar
DonskoyOP
understood. let me check this
Avatar
DonskoyOP
Hello @HaykMkrtich , do you have any link for reference?
Avatar
DonskoyOP
just to update you, i am working on SSG project, where in next.config file i have "output":"export"
Avatar
i haven't tried with this configuration ("output":"export"), you will need to try it. I think it should work if you configure the intlMiddleware and the right way and also use the right structure app>[locale]> page.tsx/or what you want.
import createIntlMiddleware from 'next-intl/middleware';

// create intl middleware instance
const handleI18nRouting = createIntlMiddleware({
locales: ['en', 'ru', 'de'],
defaultLocale: 'en',
localePrefix: 'as-needed',
localeDetection: false,
alternateLinks: false,
});


export default async function middleware(request: NextRequest) {

//wrap request into middleware instance
const response = handleI18nRouting(request);

// by returning response the middleware will handle all requests with locale prefix and inside each page.tsx you'll get locale prop and depending on that you can render fetch and render localized versions of content.
return response;
Avatar
DonskoyOP
it is not working with output:export
⨯ Middleware cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export
Avatar
static exports are limited to their use. Some features are unsupported. That's why you cant use your middleware
Image
Avatar
DonskoyOP
Yeah
exactly what i want is.
if lang is en then url should https://abc.com/
for pl(polish) lang- https://abc.com/pl
Avatar
yea, then your middleware should handle the detection and redirection of this. However: you can't use the middleware with static export. So either find a new way on detecting and redirecting the client to the correct language (without the middleware) or run a server that handles the middleware (for example via vercel)
Avatar
Why not using dynamic force-static with time based revalidation configured once year? this way your pages will be updated once a year so you. Of course if you will not change any common component in page that can regenerate pages with new code. What's the reason for exporting pages reducing data cache usage on vercel?. Anyway if you continue to work on this project and add or change components then each time your page will be regenerated.
Avatar
@Donskoy solved?
Avatar
DonskoyOP
no still not solved