How to Add i18n to Next.js: Simple Setup Guide
To add
i18n in Next.js, configure the i18n field in next.config.js with your supported locales and default locale. For easier translation management, use the next-i18next library which integrates with Next.js and React hooks for translations.Syntax
Next.js supports i18n by configuring the i18n object in next.config.js. This object includes:
- locales: an array of language codes your app supports.
- defaultLocale: the default language shown when no locale is specified.
Example configuration:
javascript
module.exports = { i18n: { locales: ['en', 'fr', 'es'], defaultLocale: 'en', }, };
Example
This example shows how to set up next-i18next for translations in a Next.js app. It includes configuration, translation files, and usage in a component.
javascript
// next.config.js const { i18n } = require('./next-i18next.config'); module.exports = { i18n, }; // next-i18next.config.js module.exports = { i18n: { locales: ['en', 'fr'], defaultLocale: 'en', }, }; // public/locales/en/common.json { "greeting": "Hello!" } // public/locales/fr/common.json { "greeting": "Bonjour!" } // pages/_app.js import { appWithTranslation } from 'next-i18next'; function MyApp({ Component, pageProps }) { return <Component {...pageProps} />; } export default appWithTranslation(MyApp); // pages/index.js import { useTranslation } from 'next-i18next'; import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; export default function Home() { const { t } = useTranslation('common'); return <h1>{t('greeting')}</h1>; } export async function getStaticProps({ locale }) { return { props: { ...(await serverSideTranslations(locale, ['common'])), }, }; }
Output
<h1>Hello!</h1> (when locale is 'en') or <h1>Bonjour!</h1> (when locale is 'fr')
Common Pitfalls
- Missing locale files: Forgetting to add translation JSON files for each locale causes errors or missing text.
- Not wrapping
_app.js: OmittingappWithTranslationwrapper prevents translations from loading. - Incorrect locale keys: Using wrong keys in
useTranslationor missing namespaces leads to empty strings. - Not configuring
next.config.jsproperly: Thei18nfield must be present and correct for Next.js routing to work with locales.
javascript
// Wrong: missing appWithTranslation wrapper // pages/_app.js function MyApp({ Component, pageProps }) { return <Component {...pageProps} />; } export default MyApp; // Right: with appWithTranslation import { appWithTranslation } from 'next-i18next'; function MyApp({ Component, pageProps }) { return <Component {...pageProps} />; } export default appWithTranslation(MyApp);
Quick Reference
- next.config.js: Add
i18nwithlocalesanddefaultLocale. - Translation files: Place JSON files under
public/locales/[locale]. - Use
next-i18next: Wrap_app.jswithappWithTranslationand useuseTranslationhook. - Load translations server-side: Use
serverSideTranslationsingetStaticPropsorgetServerSideProps.
Key Takeaways
Configure the i18n object in next.config.js with your locales and defaultLocale.
Use next-i18next for easy translation management and React hooks support.
Always wrap your _app.js with appWithTranslation to enable translations.
Place translation JSON files in public/locales/[locale] folders.
Load translations server-side using serverSideTranslations in data fetching methods.