You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a configuration file to define your supported locales, default locale, and helper functions for URL localization. This file serves as the single source of truth for your i18n setup and ensures type safety throughout your application.
127
127
128
-
**Why this step is important**: Centralizing your locale configuration prevents inconsistencies and makes it easier to add or remove locales in the future. The helper functions ensure consistent URL generation for SEO and routing.
128
+
Centralizing your locale configuration prevents inconsistencies and makes it easier to add or remove locales in the future. The helper functions ensure consistent URL generation for SEO and routing.
129
129
130
130
```ts fileName="i18n.config.ts"
131
131
// Define supported locales as a const array for type safety
@@ -210,7 +210,7 @@ export type AboutTranslator = TFunction<"about">;
210
210
211
211
Create a server-side initialization function that loads translations for server components. This function creates a separate i18next instance for server-side rendering, ensuring that translations are loaded before rendering.
212
212
213
-
**Why this step is important**: Server components need their own i18next instance because they run in a different context than client components. Pre-loading translations on the server prevents flash of untranslated content and improves SEO by ensuring search engines see translated content.
213
+
Server components need their own i18next instance because they run in a different context than client components. Pre-loading translations on the server prevents flash of untranslated content and improves SEO by ensuring search engines see translated content.
214
214
215
215
```ts fileName="src/app/i18n/server.ts"
216
216
import { createInstance } from"i18next";
@@ -265,7 +265,7 @@ export async function initI18next(
265
265
266
266
Create a client component provider that wraps your application with i18next context. This provider receives pre-loaded translations from the server to prevent flash of untranslated content (FOUC) and avoid duplicate fetching.
267
267
268
-
**Why this step is important**: Client components need their own i18next instance that runs in the browser. By accepting pre-loaded resources from the server, we ensure seamless hydration and prevent content flashing. The provider also manages locale changes and namespace loading dynamically.
268
+
Client components need their own i18next instance that runs in the browser. By accepting pre-loaded resources from the server, we ensure seamless hydration and prevent content flashing. The provider also manages locale changes and namespace loading dynamically.
269
269
270
270
```tsx fileName="src/components/I18nProvider.tsx"
271
271
"use client";
@@ -348,7 +348,7 @@ export default function I18nProvider({
348
348
349
349
Set up dynamic routing for locales by creating a `[locale]` directory in your app folder. This allows Next.js to handle locale-based routing where each locale becomes a URL segment (e.g., `/en/about`, `/fr/about`).
350
350
351
-
**Why this step is important**: Using dynamic routes enables Next.js to generate static pages for all locales at build time, improving performance and SEO. The layout component sets the HTML `lang` and `dir` attributes based on the locale, which is crucial for accessibility and search engine understanding.
351
+
Using dynamic routes enables Next.js to generate static pages for all locales at build time, improving performance and SEO. The layout component sets the HTML `lang` and `dir` attributes based on the locale, which is crucial for accessibility and search engine understanding.
352
352
353
353
```tsx fileName="src/app/[locale]/layout.tsx"
354
354
importtype { ReactNode } from"react";
@@ -400,7 +400,7 @@ export default function LocaleLayout({
400
400
401
401
Create JSON files for each locale and namespace. This structure allows you to organize translations logically and load only what you need for each page.
402
402
403
-
**Why this step is important**: Organizing translations by namespace (e.g., `common.json`, `about.json`) enables code splitting and reduces bundle size. You only load the translations needed for each page, improving performance.
403
+
Organizing translations by namespace (e.g., `common.json`, `about.json`) enables code splitting and reduces bundle size. You only load the translations needed for each page, improving performance.
404
404
405
405
```json fileName="src/locales/en/common.json"
406
406
{
@@ -442,7 +442,7 @@ Create JSON files for each locale and namespace. This structure allows you to or
442
442
443
443
Create a page component that initializes i18next on the server and passes translations to both server and client components. This ensures that translations are loaded before rendering and prevents content flashing.
444
444
445
-
**Why this step is important**: Server-side initialization loads translations before the page renders, improving SEO and preventing FOUC. By passing pre-loaded resources to the client provider, we avoid duplicate fetching and ensure smooth hydration.
445
+
Server-side initialization loads translations before the page renders, improving SEO and preventing FOUC. By passing pre-loaded resources to the client provider, we avoid duplicate fetching and ensure smooth hydration.
@@ -502,7 +502,7 @@ export default async function AboutPage({
502
502
503
503
Client components can use the `useTranslation` hook to access translations. This hook provides access to the translation function and the i18n instance, allowing you to translate content and access locale information.
504
504
505
-
**Why this step is important**: Client components need React hooks to access translations. The `useTranslation` hook integrates seamlessly with i18next and provides reactive updates when the locale changes.
505
+
Client components need React hooks to access translations. The `useTranslation` hook integrates seamlessly with i18next and provides reactive updates when the locale changes.
506
506
507
507
> Ensure the page/provider includes only the namespaces you need (e.g., `about`).
508
508
> If you use React < 19, memoize heavy formatters like `Intl.NumberFormat`.
Server components cannot use React hooks, so they receive translations via props from their parent components. This approach keeps server components synchronous and allows them to be nested inside client components.
551
551
552
-
**Why this step is important**: Server components that might be nested under client boundaries need to be synchronous. By passing translated strings and locale information as props, we avoid async operations and ensure proper rendering.
552
+
Server components that might be nested under client boundaries need to be synchronous. By passing translated strings and locale information as props, we avoid async operations and ensure proper rendering.
To change the language of your content in Next.js, the recommended way is to use locale-prefixed URLs and Next.js links. The example below reads the current locale from the route, strips it from the pathname, and renders one link per available locale.
@@ -656,7 +656,7 @@ export default function LocaleSwitcher() {
656
656
657
657
Reusing localized URLs across your app keeps navigation consistent and SEO-friendly. Wrap `next/link` in a small helper that prefixes internal routes with the active locale while leaving external URLs untouched.
@@ -707,7 +707,7 @@ export default function LocalizedLink({
707
707
708
708
Server Actions often need the current locale for emails, logging, or third-party integrations. Combine the locale cookie set by your proxy with the `Accept-Language` header as a fallback.
@@ -745,7 +745,7 @@ export async function stuffFromServer(formData: FormData) {
745
745
746
746
Translating content is important, but the main goal of internationalization is to make your website more visible to the world. I18n is an incredible lever to improve your website visibility through proper SEO.
747
747
748
-
**Why this step is important**: Properly internationalized metadata helps search engines understand what languages are available on your pages. This includes setting hreflang meta tags, translating titles and descriptions, and ensuring canonical URLs are correctly set for each locale.
748
+
Properly internationalized metadata helps search engines understand what languages are available on your pages. This includes setting hreflang meta tags, translating titles and descriptions, and ensuring canonical URLs are correctly set for each locale.
749
749
750
750
Here's a list of good practices regarding multilingual SEO:
751
751
@@ -812,7 +812,7 @@ export default async function AboutPage() {
812
812
813
813
Generate a sitemap that includes all locale versions of your pages. This helps search engines discover and index all language versions of your content.
814
814
815
-
**Why this step is important**: A properly internationalized sitemap ensures search engines can find and index all language versions of your pages. This improves visibility in international search results.
815
+
A properly internationalized sitemap ensures search engines can find and index all language versions of your pages. This improves visibility in international search results.
816
816
817
817
```ts fileName="src/app/sitemap.ts"
818
818
importtype { MetadataRoute } from"next";
@@ -866,7 +866,7 @@ export default function sitemap(): MetadataRoute.Sitemap {
866
866
867
867
Create a robots.txt file that properly handles all locale versions of your protected routes. This ensures that search engines don't index admin or dashboard pages in any language.
868
868
869
-
**Why this step is important**: Properly configuring robots.txt for all locales prevents search engines from indexing sensitive pages in any language. This is crucial for security and privacy.
869
+
Properly configuring robots.txt for all locales prevents search engines from indexing sensitive pages in any language. This is crucial for security and privacy.
870
870
871
871
```ts fileName="src/app/robots.ts"
872
872
importtype { MetadataRoute } from"next";
@@ -897,7 +897,7 @@ export default function robots(): MetadataRoute.Robots {
897
897
898
898
Create a proxy to automatically detect the user's preferred locale and redirect them to the appropriate locale-prefixed URL. This improves user experience by showing content in their preferred language.
899
899
900
-
**Why this step is important**: Middleware ensures that users are automatically redirected to their preferred language when they visit your site. It also saves the user's preference in a cookie for future visits.
900
+
Middleware ensures that users are automatically redirected to their preferred language when they visit your site. It also saves the user's preference in a cookie for future visits.
Intlayer is a **free** and **open-source** library designed to assist the localization process in your application. While i18next handles the translation loading and management, Intlayer helps automate the translation workflow.
988
988
989
-
**Why this step is important**: Managing translations manually can be time-consuming and error-prone. Intlayer automates translation testing, generation, and management, saving you time and ensuring consistency across your application.
989
+
Managing translations manually can be time-consuming and error-prone. Intlayer automates translation testing, generation, and management, saving you time and ensuring consistency across your application.
0 commit comments