Multi-language the Next.js site

In this article, I will summarize the key points to consider when developing a multilingual site on a Next.js-based site.

Prerequisite.

The site we will be building will be in two languages, Japanese and English.For this reason, the prerequisites are listed below.

Create a new project

The first step is to create a new project.

sh

The following settings were made for each project when it was created.

Image

After a few moments, a new Next.js project will be created.As for the main files, they are created as follows

text

Implementation of multilingual support

Now we will do some work to actually make it multilingual.

Dynamic Route

Next.js has a mechanism called Dynamic Route that allows URL paths to be used as values.

For example, if you create a folder called [lang] and place the page.tsx file in it, the page can be displayed using the value set for [lang].This time, we will use this mechanism to create a page that can display /en and /en.

I will move some files toward this.

Now you have a basic file move.We would like to move the style sheet files as well.

Change the import code in src/app/[lang]/layout.tsx because the style sheet location has changed.

src/app/[lang]/layout.tsx

After implementing the above changes, the following behavior will occur

First, make sure the Dynamic Router is working properly before proceeding.

Add middleware.ts

Next.js allows the use of middleware to implement code before the request is executed.So this time, create the file src/middleware.ts and add the following code.

middleware.ts

The process includes the following

After adding the above file, when accessing http://localhost:3000, we were able to redirect to ja if the browser is set to Japanese, and to en otherwise.

Specify language

In order to make the site Japanese and English, an additional implementation will be made to display a 404 error when a [lang] other than ja and en is specified.

For the language to be used, create the file src/constants/build.ts and add the following definitions, assuming future expansion.

src/constants/build.ts

Using this value, rewrite the file src/app/[lang]/page.tsx as follows.

src/app/[lang]/page.tsx

This code implements the following behavior

After the change, /en and /ja will be displayed, but if other languages are specified, a 404 error will be displayed.

Image

Update Layout.tsx

The HTML html initially contains lang="en", but this can also be implemented to change the value using the [lang] value.

Update the RootLayout in the file layout.tsx as follows

src/app/[lang]/layout.tsx

The changes are as follows

This will set lang="ja" for /en.

URL Control

You can access the ja and en pages at this stage.We will also set up URL controls as follows.

For this, use the Next.js functionality.

This can be handled by simply adding a setting to the file next.config.ts.

next.config.ts.

This allows you to add consistent rules for URLs.

Summary

The standard Next.js project could be improved by adding the language paths to the URLs and also adding the associated processing to see what the base process would be for multilingual use.

The final changed file, and the new path, will look like this

text

Sample code is available in the following repositories