Tutorials
Internationalization and Localization in Next.js Applications

Internationalization and Localization in Next.js Applications

Internationalization and Localization in Next.js Applications

Internationalization (i18n) and Localization (l10n) are key concepts for developers who want to create applications that work for users around the world. It's a process of adapting software to different languages and cultures. In this article, we are going to look at how to use Next.js to localize and internationalize our applications.

Next.js is a framework for building React applications that enables developers to quickly create and deploy applications with minimal configuration. It has built-in support for internationalization and localization, which makes the process of adapting our applications to different languages and cultures much easier.

In order to get started with internationalization and localization in Next.js, we first need to create a next.config.js file in our project root. This file will contain all of the configuration options that we need to enable internationalization and localization.

The next.config.js file should contain the following configuration options:

module.exports = {
  // Enable internationalization and localization
  i18n: {
    locales: ["en", "de", "fr"],
    defaultLocale: "en"
  }
}

The locales option is an array of languages that will be supported by our application. In this example, we are supporting English (en), German (de), and French (fr). The defaultLocale option specifies the language that will be used if a user's browser does not send an Accept-Language header.

Once we have the configuration set up, we can start creating localized content in our application. In order to do this, we need to create a directory in our project root called public/locale. Inside this directory, we will create a folder for each language that we are supporting.

For example, if we are supporting English, German, and French, we will create three directories: public/locale/en, public/locale/de, and public/locale/fr.

Inside each of these directories, we will create files containing the localized content for our application. For example, if we wanted to localize the text on a button, we could create a file called button.json in each of the language directories. The contents of the files would look something like this:

public/locale/en/button.json

{
  "text": "Click Me!"
}

public/locale/de/button.json

{
  "text": "Klick mich!"
}

public/locale/fr/button.json

{
  "text": "Cliquez-moi!"
}

Once we have our localized content set up, we need to access it in our code. To do this, we can use the useLocale hook provided by Next.js. This hook will give us access to the current locale as well as a function for loading localized content.

For example, if we wanted to render the localized text for our button, we could do something like this:

import { useLocale } from "next/i18n"
 
const MyButton = () => {
  // Get the current locale
  const { locale } = useLocale()
 
  // Load the localized button text
  const buttonText = useLocale("button")
 
  return <button>{buttonText.text}</button>
}

In this example, the useLocale hook will look for a file called button.json in the public/locale directory for the current locale. If it finds the file, it will return the localized content so that we can use it in our application.

Internationalization and localization are important for creating applications that are accessible to users around the world. Next.js makes it easy to add support for multiple languages and cultures to our applications with minimal configuration.

For more information on internationalization and localization in Next.js, check out the official documentation (opens in a new tab).