Building a Multi-Language Website with Next.js and i18n
Shubham Prakash

Introduction
In today’s interconnected world, reaching a global audience often requires creating a multi-language website. Next.js, with its built-in internationalization (i18n) support, makes it easier than ever to build scalable and SEO-friendly multilingual applications. In this blog, we’ll explore how to set up i18n in Next.js, discuss best practices, and ensure your website delivers a seamless experience to users worldwide.
Why Internationalization (i18n) Matters
Internationalization allows your website to adapt to different languages and cultural norms, ensuring inclusivity and accessibility for diverse user bases. A multi-language website can:
- Increase reach by catering to non-English-speaking audiences.
- Improve user experience by providing content in users' preferred languages.
- Boost SEO by ranking better in search engines for different languages.
With Next.js, implementing i18n is not only straightforward but also highly efficient.
Setting Up i18n in Next.js
Next.js simplifies i18n with built-in support for language routing and locale detection. Here’s how to get started:
Step 1: Configure next.config.js
Add the i18n
property to your next.config.js
file to define supported locales and default language.
module.exports = {
i18n: {
locales: ['en', 'fr', 'de', 'es'], // Supported languages
defaultLocale: 'en', // Default language
},
};
Step 2: Create Locale Files
Store your translations in JSON files organized by language. For example:
/locales/en/common.json
{
"welcome": "Welcome",
"about": "About Us"
}
/locales/fr/common.json
{
"welcome": "Bienvenue",
"about": "À propos de nous"
}
Step 3: Use a Translation Library
Integrate a library like next-i18next or react-intl to manage translations dynamically. For example, using next-i18next:
- Install the library:
npm install next-i18next
- Set up the i18n instance:
Create a next-i18next.config.js file.
const path = require('path');
module.exports = {
i18n: {
locales: ['en', 'fr', 'de', 'es'],
defaultLocale: 'en',
localePath: path.resolve('./locales'),
},
};
- Wrap your app with the I18nextProvider in _app.js:
import { appWithTranslation } from 'next-i18next';
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default appWithTranslation(MyApp);
- Use the useTranslation hook to access translations in components:
import { useTranslation } from 'next-i18next';
const Home = () => {
const { t } = useTranslation('common');
return <h1>{t('welcome')}</h1>;
};
export default Home;
Step 4: Add Language Switcher
Provide users with an intuitive way to switch languages. Here’s an example of a language switcher component:
import Link from 'next/link';
import { useRouter } from 'next/router';
const LanguageSwitcher = () => {
const { locales, locale, asPath } = useRouter();
return (
<div>
{locales.map((lang) => (
<Link key={lang} href={asPath} locale={lang}>
<button disabled={locale === lang}>{lang}</button>
</Link>
))}
</div>
);
};
export default LanguageSwitcher;
Best Practices for Multi-Language Websites
1. Optimize for SEO:
• Use localized URLs (e.g., /fr, /de).
• Generate sitemap.xml with all language routes.
• Add <link rel="alternate" hreflang="...">
tags for language versions.
-
Fallback Content: Ensure that your app gracefully handles missing translations by showing the default language content.
-
Cultural Sensitivity: Translate not just text but also date formats, currency, and imagery to align with local norms.
-
Performance Optimization: Use lazy loading for language files to reduce the initial load time.
Conclusion
Building a multi-language website with Next.js and i18n enables you to reach a wider audience, improve user experience, and boost your site’s SEO performance. With built-in internationalization support and powerful libraries like next-i18next, you can create a seamless and scalable multilingual experience with minimal effort.
By following best practices and leveraging Next.js features, you can ensure your web application is both inclusive and efficient. Whether you’re targeting a global audience or expanding to new markets, Next.js is your trusted framework for delivering exceptional results.