> nextjs-i18n
Best practices for multi-language handling, locale routing, and detection strategies across App and Pages Router. Use when adding i18n, locale routing, or language detection in Next.js. (triggers: middleware.ts, app/[lang]/**, pages/[locale]/**, messages/*.json, next.config.js, i18n, locale, translation, next-intl, react-intl, next-translate)
curl "https://skillshub.wtf/HoangNguyen0403/agent-skills-standard/nextjs-i18n?format=md"Internationalization (i18n)
Priority: P2 (MEDIUM)
Maintain a single source of truth for locales and ensure SEO-friendly sub-path routing.
Principles
- Uniform Locale Routing: Always use URL segments (e.g.,
/en/dashboard) rather than cookies or localStorage for the primary locale state.- Why: Required for SEO, link sharing, and consistent SSR/RSC behavior.
- Server-Side First: Prefer loading translation dictionaries on the server.
- App Router: Use
getMessages()in RSCs. - Pages Router: Use
getStaticPropsorgetServerSidePropsto pass translations to the page.
- App Router: Use
- Middleware detection: Use
middleware.ts(Modern) ornext.config.js(Legacy) to handle automatic locale detection and redirection.
Implementation Strategies
1. App Router (Modern)
- Use dynamic segments:
app/[lang]/layout.tsx. - Implement
middleware.tsfor language detection based onAccept-Languageheaders.
2. Pages Router (Legacy Next.js 13)
- Configure
next.config.jswithi18nfield:module.exports = { i18n: { locales: ['en', 'fr', 'vi'], defaultLocale: 'en', }, };
3. Library Specifics
For detailed setup with common libraries, refer to:
Anti-Patterns
- Hardcoded Strings: Never commit raw text in JSX. Use translation keys.
- Client-Only Translation: Avoid loading multi-megabyte JSON translation files in the client bundle.
- Locale Squatting: Don't use a different URL structure for different languages (e.g.
domain.com/envsdomain.fr). Stick to sub-paths or domains consistently.
> related_skills --same-repo
> typescript-tooling
Development tools, linting, and build config for TypeScript. Use when configuring ESLint, Prettier, Jest, Vitest, tsconfig, or any TS build tooling. (triggers: tsconfig.json, .eslintrc.*, jest.config.*, package.json, eslint, prettier, jest, vitest, build, compile, lint)
> typescript-security
Secure coding practices for TypeScript. Use when validating input, handling auth tokens, sanitizing data, or managing secrets and sensitive configuration. (triggers: **/*.ts, **/*.tsx, validate, sanitize, xss, injection, auth, password, secret, token)
> typescript-language
Modern TypeScript standards for type safety and maintainability. Use when working with types, interfaces, generics, enums, unions, or tsconfig settings. (triggers: **/*.ts, **/*.tsx, tsconfig.json, type, interface, generic, enum, union, intersection, readonly, const, namespace)
> typescript-best-practices
Idiomatic TypeScript patterns for clean, maintainable code. Use when writing or refactoring TypeScript classes, functions, modules, or async logic. (triggers: **/*.ts, **/*.tsx, class, function, module, import, export, async, promise)