Cursorcursorintermediate

Add Internationalization to Next.js (Cursor Prompt)

Cursor prompt to add next-intl internationalization with middleware routing and language switching.

What you'll get

Internationalized Next.js app with locale routing, language switcher, and translation files using next-intl.

The Prompt

Add internationalization (i18n) to this Next.js app using next-intl.

FILES TO CREATE:
- src/i18n/request.ts — next-intl request configuration
- src/i18n/routing.ts — Locale routing configuration
- messages/en.json — English translations
- messages/es.json — Spanish translations (or your target language)
- src/middleware.ts — Update middleware to handle locale routing
- src/app/[locale]/layout.tsx — Locale-aware root layout
- src/app/[locale]/page.tsx — Move existing pages under [locale] segment
- src/components/LocaleSwitcher.tsx — Language switcher component

IMPLEMENTATION:
1. Install next-intl.
2. In routing.ts, define supported locales: ['en', 'es'] and defaultLocale: 'en'.
3. In request.ts, configure getRequestConfig to load the correct message file based on locale.
4. Update middleware.ts to use createMiddleware from next-intl/middleware. Set localePrefix: 'as-needed' so the default locale doesn't show in the URL.
5. Move all pages under src/app/[locale]/ to enable locale-based routing.
6. Replace all hardcoded strings with useTranslations('namespace').t('key') in client components and getTranslations('namespace') in server components.
7. Create LocaleSwitcher component that uses useRouter and usePathname from next-intl/navigation to switch languages while preserving the current path.
8. Add hreflang link tags for SEO in the layout.

MESSAGE FILE STRUCTURE:
{ "HomePage": { "title": "Welcome", "description": "..." }, "Nav": { "home": "Home", "about": "About" } }

DO NOT:
- Use i18next or react-intl (use next-intl for App Router compatibility)
- Hardcode any user-facing strings
- Change the visual design or layout

Replace these variables

VariableReplace with
[LOCALES]Supported locale codes (e.g., en, es, fr, de)
[DEFAULT_LOCALE]Default locale code (e.g., en)

Tips for best results

Use 'as-needed' locale prefix so your default language URLs stay clean (no /en/ prefix).

Organize translations by page/component namespace — flat files become unmaintainable quickly.

Follow-up prompts

Add RTL support

Add RTL (right-to-left) layout support for Arabic. Dynamically set dir='rtl' on html element and use CSS logical properties.

Related prompts