Internationalization (i18n)
Next.js i18n internationalization App Router mein, [locale] segment, proxy.ts se language detection, dictionaries aur next-intl, Roman Urdu mein.
On this page
1. Internationalization (i18n) kya hai?
Is ke do hisse hain:
- Routing:
/en/aboutaur/ur/aboutjaise URLs, jo App Router meinapp/[locale]folder se bante hain. - Localization: har locale ke liye alag text (dictionaries) aur dates, numbers ki formatting.
App Router mein Pages Router wala i18n config nahi hota. Routing aap [locale] segment aur proxy.ts se khud set karte hain, ya next-intl jaisi library use karte hain.
2. Kyun zaroori hai?
Agar text components mein hardcode ho to doosri zuban add karna poora code dobara likhne jaisa hai. URL mein locale na ho to Google har zuban ka page alag index nahi kar sakta.
| Hardcoded text | i18n setup |
|---|---|
| Text har component mein likha hua | Text JSON dictionaries mein |
| Ek hi URL, zuban cookie se | Har zuban ka apna URL, SEO friendly |
| Nayi zuban matlab code copy | Nayi zuban matlab nayi JSON file |
| User ki browser language ignore | Accept-Language se sahi locale |
3. Kahan use hota hai?
| Project | i18n ki zaroorat |
|---|---|
| E-commerce | /en aur /ur product pages, currency formatting |
| SaaS dashboard | User settings se zuban chunna |
| Blog ya docs | Har article ka translated version |
| Government ya NGO site | English, Urdu aur regional zubanein |
4. Kaise kaam karta hai?
Dictionaries Server Components mein load hoti hain, is liye translation files client bundle mein nahi jati, sirf rendered HTML jata hai.
5. Code example
proxy.ts: locale detect kar ke redirect
import { NextResponse, type NextRequest } from "next/server";
import { match } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
const locales = ["en", "ur"];
const defaultLocale = "en";
function getLocale(request: NextRequest) {
const headers = { "accept-language": request.headers.get("accept-language") ?? "" };
const languages = new Negotiator({ headers }).languages();
return match(languages, locales, defaultLocale);
}
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const hasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
if (hasLocale) return;
request.nextUrl.pathname = `/${getLocale(request)}${pathname}`;
return NextResponse.redirect(request.nextUrl);
}
export const config = {
matcher: ["/((?!_next|api|.*\\..*).*)"],
};Dictionaries
import "server-only";
const dictionaries = {
en: () => import("./dictionaries/en.json").then((m) => m.default),
ur: () => import("./dictionaries/ur.json").then((m) => m.default),
};
export type Locale = keyof typeof dictionaries;
export const hasLocale = (locale: string): locale is Locale => locale in dictionaries;
export const getDictionary = async (locale: Locale) => dictionaries[locale]();app/[locale]/page.tsx
import { notFound } from "next/navigation";
import { getDictionary, hasLocale } from "./dictionaries";
export function generateStaticParams() {
return [{ locale: "en" }, { locale: "ur" }];
}
export default async function Page({ params }: { params: Promise<{ locale: string }> }) {
const { locale } = await params;
if (!hasLocale(locale)) notFound();
const dict = await getDictionary(locale);
return <h1>{dict.home.title}</h1>;
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
Negotiator | Accept-Language header se user ki pasandeeda zubanein |
match() | Supported locales mein se best match, warna default |
matcher | _next, api aur files (jin mein dot ho) par proxy nahi chalta |
"server-only" | Dictionary loader galti se client mein import na ho |
hasLocale | Type narrowing, aur ghalat locale par 404 |
generateStaticParams | Har locale ka page build time par static |
Urdu jaisi right-to-left zuban ke liye root layout mein html par dir="rtl" aur lang={locale} set karna na bhoolein.
6. Common mistakes
Galati: Proxy matcher mein static files aur _next ko shamil rehne dena
Why? Images, CSS aur JS requests bhi locale redirect ho jati hain aur page toot jata hai.
Behtar: Matcher se _next, api aur file extension wale paths exclude karo.
Galati: Locale param ko bina check kiye use karna
Why? Koi /xyz/page khole to dictionary undefined milti hai aur runtime error aata hai.
Behtar: hasLocale se check karo aur ghalat locale par notFound() call karo.
Galati: Translations Client Component mein poori JSON import karna
Why? Har zuban ki poori file browser bundle mein chali jati hai.
Behtar: Dictionary Server Component mein load karo aur client ko sirf zaroori strings props mein do.
Galati: html lang aur dir set na karna
Why? Screen readers ghalat pronunciation karte hain aur Urdu text ulta align hota hai.
Behtar: Root layout mein lang aur RTL zubanon ke liye dir rtl locale ke hisaab se set karo.
Interview tip
"App Router mein i18n config kyun nahi?" Kyunke routing ab file system aur proxy.ts ke zariye flexible hai. Aap [locale] dynamic segment banate hain, aur detection ya redirect logic proxy mein likhte hain.
7. Pehle aur baad mein kya seekhein?
| Stage | Chapters |
|---|---|
| Pehle | Dynamic Routes, Proxy (formerly Middleware), Cookies & Headers |
| Saath mein | Metadata & SEO, Layouts & Templates |
| Baad mein | Testing Next.js Apps, Deployment |
Final quick revision
| Point | Detail |
|---|---|
| Routing | app/[locale]/... dynamic segment |
| Detection | proxy.ts mein Accept-Language aur redirect |
| Text | JSON dictionaries, Server Component mein load |
| Static | generateStaticParams har locale ke liye |
| Library | next-intl bare projects ke liye |
| RTL | html par lang aur dir |