Internationalization (i18n)
KnowledgeCheckr uses next-international v1.3.1 for type-safe internationalization with support for English and German locales.Overview
Next-international provides:- Type-safe translations
- Server and client component support
- Automatic locale routing
- Scoped translations
- Dynamic locale switching
- Pluralization support
Configuration
i18n Config
Location:src/i18n/i18nConfig.ts
en- English (default)de- German (Deutsch)
rewriteDefault
- English URLs:
/checks(no locale prefix) - German URLs:
/de/checks(locale prefix)
Server-Side i18n
Location:src/i18n/server-localization.ts
Server Component Usage
Scoped Translations
Reduce repetition with scoped translations:Static Params Generation
Generate static paths for all locales:Client-Side i18n
Location:src/i18n/client-localization.ts
Client Component Usage
Scoped Client Translations
Locale Switching
Translation Files
Locale Structure
Location:src/i18n/locales/en.ts (auto-generated)
Pluralization
Support for plural forms:Variable Interpolation
Layout Integration
Locale Layout
Location:src/app/[locale]/layout.tsx
I18nProvider Component
Locale Management
Source Files
Translations are managed in JSON files:Auto-Generation Script
Location:src/i18n/scripts/prepare-i18n_ally-locales.ts
Converts JSON locale files to TypeScript with type safety:
package.json:9:
- Watches
*.jsonfiles insrc/i18n/locales/ - Generates corresponding
.tsfiles - Adds
as constfor TypeScript type inference - Auto-generated files include warning comment
Adding New Translations
- Edit
src/i18n/locales/en.json(source of truth) - Edit
src/i18n/locales/de.json(German translation) - TypeScript files regenerate automatically in dev mode
- Use translations with full type safety
Type Safety
Translation Path Autocomplete
TypeScript provides autocomplete for translation keys:Compile-Time Validation
Missing translations are caught at build time:Type-Safe Parameters
Variable interpolation is type-checked:Middleware Setup
Next-international requires middleware for locale detection: Location:src/middleware.ts
- Detects locale from URL path
- Redirects
/to/(en) or/debased on browser settings - Rewrites paths to include locale in route params
Locale Detection
Priority Order
- URL Path:
/de/checks→de - Cookie:
NEXT_LOCALE=de - Accept-Language Header: Browser preference
- Default:
en
Example Flow
Development Workflow
Starting Development
- Starts Next.js dev server with Turbopack
- Starts nodemon to watch JSON locale files
- Auto-regenerates TypeScript files on changes
Adding a New Locale
-
Update config (
src/i18n/i18nConfig.ts): -
Create locale file (
src/i18n/locales/fr.json): -
Update server localization (
src/i18n/server-localization.ts): -
Update client localization (
src/i18n/client-localization.ts): - Restart dev server to regenerate types
Best Practices
1. Use Scoped Translations
Reduce repetition in components:2. Organize by Feature
Group translations by feature area:3. Use Meaningful Keys
Prefer descriptive keys over generic ones:4. Keep Translations Flat When Possible
Avoid excessive nesting:5. Server vs Client Components
- Use server translations for static content
- Use client translations for interactive elements
- Prefer server components for better performance
Performance Considerations
Code Splitting
Locale files are automatically code-split:- English users only download
en.ts - German users only download
de.ts
Server-Side Rendering
Translations are rendered on the server:- No flash of untranslated content
- SEO-friendly translated content
- Faster initial page load
Client Hydration
Client components receive pre-translated content:Testing
Test with Specific Locale
Test Locale Switching
Troubleshooting
TypeScript Not Recognizing New Keys
- Ensure JSON files are saved
- Check nodemon is running (via
yarn dev) - Restart TypeScript server in your IDE
- Manually run:
yarn tsx src/i18n/scripts/prepare-i18n_ally-locales.ts
Translations Not Updating
- Clear
.nextcache:rm -rf .next - Restart dev server
- Hard refresh browser (Ctrl+Shift+R)
Missing Translation Fallback
Next-international returns the key if translation is missing:Next Steps
- Architecture - System overview
- Authentication - Multi-language auth flows