Building Multilingual Websites with Next.js

Building multilingual websites can greatly enhance the user experience and expand your website's reach to a global audience. With Next.js, a powerful React framework, you can easily create and manage multilingual websites by leveraging its built-in features like internationalized routing, dynamic content rendering, and language detection. This blog post will guide you through the process of building a multilingual website using Next.js, providing you with practical tips and best practices along the way.

Building Multilingual Websites with Next.js

Building Multilingual Websites with Next.js

Introduction

In today's globalized world, it's becoming increasingly important for businesses and individuals to create websites that cater to a diverse audience. One way to achieve this is by building multilingual websites that can be easily understood by people from different linguistic backgrounds. In this blog post, we will explore how to build multilingual websites using Next.js, a popular React framework.

Why Build Multilingual Websites?

Before we dive into the technical aspects of building multilingual websites, let's first understand why it's essential to have a website that supports multiple languages.

  1. Reach a wider audience: By offering your website in multiple languages, you can reach a larger audience and potentially increase your user base. This is particularly important if you have a global customer base or if you want to expand your business to new markets.

  2. Improve user experience: When users can access your website in their preferred language, they are more likely to engage with your content and stay on your site for longer periods. This can lead to increased conversions and improved user satisfaction.

  3. Enhance SEO: Building a multilingual website can also have positive effects on your search engine optimization (SEO) efforts. By targeting specific keywords in different languages, you can improve your website's visibility in search engine results pages for users searching in those languages.

Now that we understand the benefits of building multilingual websites, let's explore how to achieve this using Next.js.

Getting Started with Next.js

Next.js is a powerful framework for building server-side rendered React applications. It provides an excellent foundation for creating multilingual websites due to its flexible routing system and support for server-side rendering.

To get started, make sure you have Node.js installed on your machine. You can then create a new Next.js project by running the following commands in your terminal:

npx create-next-app my-multilingual-website
cd my-multilingual-website

Once the project is set up, you can start building your multilingual website.

Internationalization in Next.js

Next.js provides built-in support for internationalization (i18n) through its next-i18next package. This package offers a comprehensive set of tools for managing translations and routing based on the user's language preference.

To add internationalization support to your Next.js project, follow these steps:

  1. Install the next-i18next package by running the following command:

    npm install next-i18next
    
  2. Create a new file called i18n.js in the root of your project. This file will contain the configuration for your internationalization setup. Here's an example of how the i18n.js file might look:

    const NextI18Next = require("next-i18next").default;
    
    const NextI18NextInstance = new NextI18Next({
      defaultLanguage: "en",
      otherLanguages: ["fr", "es", "de"],
    });
    
    module.exports = NextI18NextInstance;
    

    In this example, we set the default language to English (en) and define three additional languages: French (fr), Spanish (es), and German (de).

  3. Create a new folder called locales in the root of your project. Inside this folder, create a subfolder for each language you defined in the i18n.js file. For example, you might have the following folder structure:

    - locales
      - en
      - fr
      - es
      - de
    

    Each language folder should contain translation files in JSON format. For instance, the English translation file (en.json) might look like this:

    {
      "home": "Home",
      "about": "About",
      "contact": "Contact",
      "welcome": "Welcome to my website!",
      ...
    }
    

    Similarly, the French translation file (fr.json) might look like this:

    {
      "home": "Accueil",
      "about": "À propos",
      "contact": "Contact",
      "welcome": "Bienvenue sur mon site web !",
      ...
    }
    
  4. In your Next.js pages, you can now use the next-i18next package to handle translations and routing based on the user's language preference. Here's an example of how you can implement this:

    import { useTranslation } from "next-i18next";
    
    function HomePage() {
      const { t } = useTranslation("common");
    
      return (
        <div>
          <h1>{t("welcome")}</h1>
          <nav>
            <ul>
              <li>{t("home")}</li>
              <li>{t("about")}</li>
              <li>{t("contact")}</li>
            </ul>
          </nav>
        </div>
      );
    }
    
    export default HomePage;
    

    In this example, we import the useTranslation hook from next-i18next and use it to access the translated strings defined in the common namespace.

Dynamic Routing for Multilingual Websites

Next.js makes it easy to create dynamic routes for your multilingual website. You can use the getStaticPaths and getStaticProps functions to generate pages for each language based on the available translations.

Here's an example of how you can implement dynamic routing in Next.js:

// pages/[lang]/about.js
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";

function AboutPage() {
  const router = useRouter();
  const { t } = useTranslation("common");

  const { lang } = router.query;

  return (
    <div>
      <h1>{t("about")}</h1>
      <p>{t("about_description")}</p>
      <p>Current language: {lang}</p>
    </div>
  );
}

export default AboutPage;

export async function getStaticPaths() {
  return {
    paths: [
      { params: { lang: "en" } },
      { params: { lang: "fr" } },
      { params: { lang: "es" } },
      { params: { lang: "de" } },
    ],
    fallback: false,
  };
}

export async function getStaticProps({ params }) {
  return {
    props: {
      lang: params.lang,
    },
  };
}

In this example, we define the AboutPage component, which displays the translated "About" content and the current language based on the dynamic lang parameter in the URL. The getStaticPaths function specifies the available languages, and the getStaticProps function passes the selected language as a prop to the page.

Conclusion

Building multilingual websites is crucial for reaching a global audience and providing a seamless user experience. Next.js, with its support for internationalization and dynamic routing, offers an excellent framework for creating multilingual websites. By following the steps outlined in this blog post, you can start building your own multilingual website using Next.js and provide content in multiple languages to cater to a diverse audience. Happy coding!

Create a website that grows with you

Get Started