


Next.js provides server-side rendering for React applications. In this post, we will learn how to translate the text in a Next.js app by using
1
next-translateBefore we proceed ensure you have the following installed on your system:
- node
- npm
- yarn
Project setup
First, we need to create a project. We will do this by using the
1
create-next-appIn terminal type :
1npx create-next-app
This will run an interactive prompt that will ask for the project name. You can name it whatever you like. For the sake of this post, we named ours
1
next-translate1
yarn dev1
localhost:3000
We don't need these components and pages from the starter template so now you'll need to delete the
1
styles/Home.module.css1
api1
pages1
pages/index.js1 2 3 4 5 6 7const Home = () => { return ( <h1>Home</h1> ) } export default Home
We will also have an
1
about1 2 3 4 5 6 7const About = () => { return ( <h1>About</h1> ) } export default About
After getting this done we need to install a library for translating our pages. Open up a terminal in your project directory and type
1
yarn add next-translateTranslations
First, we need to create
1
next.config.js1 2 3const nextTranslate = require('next-translate') module.exports = nextTranslate()
After that, we need to add the
1
i18n.json1
next-translate1 2 3 4 5 6 7 8{ "locales": ["en", "me"], "defaultLocale": "en", "pages": { "/": ["home"], "/about": ["about"] } }
- In the array we specify all the locales that we want to use in our project (uses ISO format)
1locales - is required so that
1defaultLocaleknows what is our default language (uses ISO format)1next-translate - In we specify namespaces used in each page. To add namespaces to all pages use
1pages{"*": ["common"]}1(eg:/locales` root directory.1). You can also use regex to specify what locales are used in pages. \ \ After creating configuration files we now need to specify namespaces. By default, they are specified inside the
1 2 3 4 5 6├── en │ ├── about.json │ └── home.json └── me ├── about.json └── home.json
In these
1
.json1
i18n.json1
en/home.json1 2 3 4 5{ "title": "Home Page", "description": "This is a home page description written in the English language.", "current-language": "Current locale is set to /{{language}}" }
You probably noticed
1
{{language}}1
useTranslation1
next-translate/useTranslation1
.json1
/locales1
index.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19import useTranslation from "next-translate/useTranslation" const Home = () => { const { t, lang } = useTranslation("home") const title = t("title") const description = t("description") const language = t("language", { language: lang }) return ( <> <h1>{title}</h1> <h4>{description}</h4> <p>{language}</p> </> ) } export default Home
Let's explain line by line what is happening here.
By using the
1
useTranslation1
t1
lang1
t1
langBy adding a parameter to the
1
useTranslation1
next-translate1
i18n.json1
root1
t("title")1
/locales/(en/me)/home.jsonBy using
1
t("language", { language: lang })1
languageRouting to different locales
From version
1
101
Internationalized RoutingLet's say that you have 2 different locales
1
en1
me1
Link1
next/linkLink component
In NextJS
1
Link1 2 3 4 5import Link from "next/link"; <Link href="/about"> <a>About</a> </Link>
but if we want to send the user to the
1
/about1
locale1
Link1 2 3<Link href="/about" locale="en"> <a>About</a> </Link>
This also means that
1
next-translateAs we can see, translation in NextJS is convenient, easy to set-up, and use. You can find the code by clicking here. It is a GitHub repository with the code from this article that you can use and modify. The project in this repository contains basic styling and routing.


