codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
▲

Next.js

47 / 73 topics
44Internationalization (i18n)45Setting Up i18n46Dynamic Locale Routing47Using GetText for i18n
Tutorials/Next.js/Using GetText for i18n
▲Next.js

Using GetText for i18n

Updated 2026-04-20
3 min read

Using GetText for i18n in Next.js

Introduction

Internationalization (i18n) is a crucial aspect of building applications that cater to a global audience. One popular method for handling translations is using the GNU gettext library, which provides a robust framework for internationalizing software. In this tutorial, we will explore how to integrate GetText into a Next.js application to enable localization.

Prerequisites

Before diving into the implementation, ensure you have the following:

  • Basic knowledge of Next.js.
  • Node.js installed on your machine.
  • Familiarity with JavaScript and React.

Setting Up Your Next.js Project

First, create a new Next.js project if you haven't already:

npx create-next-app@latest my-i18n-app
cd my-i18n-app

Installing Required Packages

To use GetText in your Next.js application, you will need to install the following packages:

  • gettext-parser: A library for parsing and generating gettext files.
  • next-gettext: A simple i18n solution for Next.js using gettext.

Install these packages using npm or yarn:

npm install gettext-parser next-gettext

or

yarn add gettext-parser next-gettext

Configuring GetText

Next, set up the configuration for GetText in your project. Create a new file named i18n.js in the lib directory:

// lib/i18n.js
import { createGettext } from 'next-gettext';
import parser from 'gettext-parser';

const translations = {
  en: parser.po.parse(`
msgid ""
msgstr ""

msgid "Hello, world!"
msgstr "Hello, world!"

msgid "Welcome to Next.js"
msgstr "Welcome to Next.js"
`),
  es: parser.po.parse(`
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"

msgid "Hello, world!"
msgstr "¡Hola, mundo!"

msgid "Welcome to Next.js"
msgstr "Bienvenido a Next.js"
`)
};

const gettext = createGettext({
  translations,
  defaultLocale: 'en',
  locales: ['en', 'es']
});

export default gettext;

In this configuration, we define translations for English (en) and Spanish (es). The gettext-parser library is used to parse the PO files containing the translations.

Creating a Custom App Component

To integrate GetText into your application, you need to create a custom _app.js file in the pages directory:

// pages/_app.js
import '../styles/globals.css';
import { GettextProvider } from 'next-gettext';
import gettext from '../lib/i18n';

function MyApp({ Component, pageProps }) {
  return (
    <GettextProvider translations={gettext}>
      <Component {...pageProps} />
    </GettextProvider>
  );
}

export default MyApp;

This custom _app.js file wraps your application with the GettextProvider, making the translation functions available throughout your app.

Using Translations in Your Components

Now, you can use translations in your components. Here's an example of how to do this:

// pages/index.js
import { useTranslation } from 'next-gettext';

export default function Home() {
  const { t } = useTranslation();

  return (
    <div>
      <h1>{t('Welcome to Next.js')}</h1>
      <p>{t('Hello, world!')}</p>
    </div>
  );
}

In this example, the useTranslation hook is used to access the translation function t, which retrieves the translated strings based on the current locale.

Handling Locale Switching

To allow users to switch between different locales, you can create a simple component for locale selection:

// components/LocaleSwitcher.js
import { useRouter } from 'next/router';

export default function LocaleSwitcher() {
  const router = useRouter();
  const { locale } = router;

  const changeLocale = (newLocale) => {
    router.push(router.pathname, router.asPath, { locale: newLocale });
  };

  return (
    <div>
      <button onClick={() => changeLocale('en')}>English</button>
      <button onClick={() => changeLocale('es')}>Español</button>
    </div>
  );
}

You can use this component in your pages or layouts to provide users with the option to switch locales.

Best Practices

  1. Separate Translation Files: For larger projects, consider separating translations into different files for each locale and loading them dynamically.
  2. Use Environment Variables: Store default locale and supported locales in environment variables for easy configuration.
  3. Fallback Translations: Implement fallback translations to handle missing translations gracefully.
  4. Testing: Ensure thorough testing of your application with different locales to catch any translation issues.

Conclusion

Integrating GetText into a Next.js application provides a powerful way to manage internationalization. By following the steps outlined in this tutorial, you can easily add support for multiple languages and enhance the accessibility of your application. Remember to regularly update your translations and test your app with different locales to ensure a smooth user experience.


PreviousDynamic Locale RoutingNext SEO Optimization

Recommended Gear

Dynamic Locale RoutingSEO Optimization