home / skills / hoangnguyen0403 / agent-skills-standard / internationalization
This skill helps implement Next.js i18n with sub-path routing, server components, and language detection to improve localization and SEO.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill internationalizationReview the files below or copy the command above to add this skill to your agents.
---
name: Next.js Internationalization (i18n)
description: Best practices for multi-language handling, locale routing, and detection middleware.
metadata:
labels: [nextjs, i18n, middleware, routing]
triggers:
files: ['middleware.ts', 'app/[lang]/**', 'messages/*.json']
keywords: [i18n, locale, translation, next-intl, redirect]
---
# Internationalization (i18n)
## **Priority: P2 (MEDIUM)**
Use Sub-path Routing (`/en`, `/de`) and Server Components for translations.
## Principles
1. **Sub-path Routing**: Use URL segments (e.g., `app/[lang]/page.tsx`) to manage locales.
- _Why_: SEO friendly, sharable, and cacheable.
2. **Server-Side Translation**: Load dictionary files (`en.json`) in Server Components.
- _Why_: Reduces client bundle size. No huge JSON blobs sent to browser.
3. **Middleware Detection**: Use `middleware.ts` to detect `Accept-Language` headers and redirect users to their preferred locale.
4. **Type Safety**: Use robust typing for translation keys to prevent broken text UI.
## Implementation Pattern
See [references/i18n_setup.md](references/i18n_setup.md) for Directory Structure, Middleware, and Server Component examples.
## Redirect Handling Strategy
When handling redirects (Authentication, Legacy URLs) in an i18n app:
- **Always preserve locale**: Use `redirect(`/${lang}/login`)` instead of just `/login`.
- **Server Actions**: Return `redirect(...)` from actions.
- **Next Config**: Use `next.config.js` for legacy SEO 301s (e.g., old-site `/about-us` -> `/en/about`).
This skill captures best practices for implementing internationalization (i18n) in Next.js apps using sub-path routing, server components, and middleware detection. It focuses on SEO-friendly URL structures, server-side translation loading, redirect handling, and type-safe translation keys to avoid runtime UI breakage. Followable patterns and concrete guidance help keep client bundles small and locale behavior predictable.
Locale routing is implemented with sub-paths (for example, app/[lang]/page.tsx) so each language has its own URL segment for SEO and caching. Server Components load locale dictionaries (e.g., en.json) at render time to avoid shipping large JSON to the browser. A middleware.ts inspects Accept-Language headers and redirects users to the most appropriate /[lang] path. Redirects and server actions preserve the locale by always including the language segment in target paths.
Should I use sub-paths or domains for locales?
Use sub-paths for most cases: they are simpler to manage, SEO-friendly, and work with caching and sharing links. Domains are useful for geolocation/brand reasons but add operational complexity.
How do I avoid shipping large JSON translation files to the client?
Load dictionaries in Server Components so translations render server-side. Only send minimal runtime data to the client and avoid bundling full locale JSON blobs.