i18n Routing
Using `gt-next`'s built-in middleware to create i18n routes for your app
i18n routing is a web development technique used to manage multiple languages and regional formats on a website, ensuring users can access content in their preferred language through language-specific URLs. It involves detecting the user's language and providing localized routes, like www.example.com/fr
for French. gt-next
's middleware allows you to easily add i18n routing to your Next.js App Router project.
In Next.js, create a file called middleware.js
(or .ts
if you are using TypeScript) inside the root directory. If you are using the src/
folder, place it in src/middleware.js
instead.
You should include defaultLocale
and locales
in both middleware.js
and next.config.js
. The reason you have to include them separately is that on certain hosts (e.g. Vercel), middleware runs in a different environment, meaning createNextMiddleware()
does not have access to your initGT()
configuration.
Then, create a dynamic route (e.g., [lang]
) in your app folder, and include all pages and layouts under it. From Vercel:
Ensure all special files inside
app/
are nested underapp/[lang]
. This enables the Next.js router to dynamically handle different locales in the route, and forward the lang parameter to every layout and page.
Your file structure should look like:
This will enable gt-next
-powered i18n routing on your Next.js project.