Don't use dynamic [lang] segment for your i18n Next.js routes
Tags: nextjs, javascript, programming, webdev
Published: 2023-03-29 11:53:25

First of all, check the [official guide](https://beta.nextjs.org/docs/guides/internationalization#routing-overview) explaining how to handle internationalized routing in Next.js 13 if you are not familiar with the dynamic `[lang]` segment.

---

### TL;DR

Avoid dynamic `[lang]` segment because:
- It makes URL translations and page links clunky. 
- It keeps your runtime busy checking tons of rewrites.
- It creates content duplication by making your pages available on multiple URLs.
- It knocks your SEO score down.

Use [next-roots](https://github.com/svobik7/next-roots), the ultimate library for i18n file-based routing:
- It generates translated file routes within explicitly stated locales. 
- It provides a type-safe API for creating page links
- It needs no rewrites

---


### The dynamic `[lang]` segment sucks!

Don't get me wrong it works well in some cases but when you need to take care of your SEO score, it sucks. While the content translation is a breeze the URL translations become clunky.

Config option called [rewrites](https://github.com/vercel/next.js/blob/canary/examples/rewrites/next.config.js) can be used to set all possible URL translations and point them to proper destinations. Unfortunately, that goes against the benefits of file-based routing. All those rewrites must be checked before every request finds its target. In runtime. It is true even for the URLs that can be routed directly with no rewrites.

Not to mention the hard time of managing those rewrites for a site with 30+ routes and 5 localizations. And creating page links? Uff, even more frustrating. And yet the most annoying drawback is that your site ends up with one page accessible on multiple URLs. 

![Content duplication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7h4uwiza4yo5s8xai4bj.png)

That is because dynamic `[lang]` segment does not care about translated URL paths by default. Even you can set rewrites rules you still end up with having one page available on two URLs.

Such content duplication simply knocks your SEO score down with no mercy. Isn't there a way to overcome all those pitfall? Well, there is. By utilizing static locales.

---

### Static locales segments rock!

Drop that dynamic `[lang]` parent segment and spread all your translated file routes among explicitly stated locales.


![Static locales](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aj4kpyaxd93207p3h1tq.png)

That keeps your runtime free of looping through the rewrites. No page is available on multiple URLs. And your SEO score is not damaged. 

However, managing those translations stays cumbersome and page links are far from being easy to create. For those to become convenient you need a generator. And a router.

---

### Meet next-roots

The ultimate library for handling file-based i18n routing in your app. It treats your file routes as they were meant to be - just as routes with no business logic. It generates all translated file routes based on the configuration schema. And it provides a type-safe API for creating page links. Neat!

Find more in [next-roots](https://github.com/svobik7/next-roots) repo or in [Step-by-step guide for SEO friendly i18n routes in Next.js 13](https://dev.to/svobik7/step-by-step-guide-for-seo-friendly-i18n-routes-in-nextjs-13-3j0f)