Troubleshooting Internationalization
This guide covers common issues you might encounter when using the internationalization package and provides solutions to resolve them.Common Issues
Missing Translations
Symptoms
- Text appears in the default language (English) instead of the selected language
- Translation keys appear in the UI instead of translated text
- Console warnings about missing translations
Solutions
-
Check if the translation key exists
Verify that the translation key exists in the dictionary file for the current locale:
-
Add missing translations
Add the missing translation to the appropriate dictionary file:
-
Use fallback for optional translations
For translations that might not exist in all locales, use optional chaining and fallbacks:
-
Run the translation script
Use the built-in translation script to generate missing translations:
Middleware Not Working
Symptoms
- Locale is not detected correctly
- URL-based locale switching doesn’t work
- Always redirects to the default locale
Solutions
-
Check middleware configuration
Ensure your middleware.ts file is correctly configured:
-
Verify matcher pattern
Make sure the matcher pattern is correct and doesn’t exclude your routes:
-
Check for conflicting middleware
If you have other middleware, make sure they don’t conflict with the internationalization middleware:
Client Components Not Translating
Symptoms
- Server components show correct translations, but client components don’t
- Client components show translation keys instead of translated text
- Errors related to the
useTranslation
hook
Solutions
-
Check TranslationProvider setup
Make sure your client components are wrapped with the TranslationProvider:
-
Verify namespace parameter
Check that you’re using the correct namespace in the
useTranslation
hook: -
Check for React context issues
If you’re using multiple providers, make sure they’re not interfering with each other:
Language Switching Issues
Symptoms
- Clicking language switcher links doesn’t change the language
- URL changes but content doesn’t update
- Language switcher links go to 404 pages
Solutions
-
Check language switcher implementation
Make sure your language switcher correctly builds the URLs:
-
Verify route structure
Ensure your routes are properly structured with the locale parameter:
-
Check for caching issues
If the URL changes but the content doesn’t update, it might be a caching issue:
Date and Number Formatting Issues
Symptoms
- Dates and numbers are not formatted according to the locale
- Formatting is inconsistent across the application
Solutions
-
Use the Intl API consistently
Make sure you’re using the Intl API for formatting:
-
Create helper functions
Create reusable helper functions for formatting:
-
Handle locale-specific formats
Be aware of locale-specific formats and handle them appropriately:
Debugging Techniques
Console Logging
Add console logs to track the flow of translations:Browser Developer Tools
Use browser developer tools to inspect the React component tree and context values:- Open your browser’s developer tools (F12 or right-click > Inspect)
- Go to the Components tab (in React DevTools)
- Find the TranslationProvider component
- Check its props (locale and messages)
- Look for any errors in the console
React DevTools
Install React DevTools for more detailed debugging:Advanced Troubleshooting
Missing or Incorrect TypeScript Types
Symptoms
- TypeScript errors related to translation types
- No autocomplete for translation keys
Solutions
-
Define proper types for your translations
Create a type definition for your translations:
-
Use type assertions with the useTranslation hook
Performance Issues
Symptoms
- Slow page loads when switching languages
- High memory usage
Solutions
-
Optimize dictionary size
Split large dictionaries into smaller chunks:
-
Use code splitting
Load translations only when needed:
-
Memoize translation functions
Use memoization to prevent unnecessary re-renders:
Common Error Messages and Solutions
Error: Cannot find module '@repo/internationalization'
Error: Cannot find module '@repo/internationalization'
Solution:Make sure the package is correctly installed and included in your dependencies:Then run:
Error: Translation key 'x' does not exist
Error: Translation key 'x' does not exist
Solution:Add the missing translation key to all locale files:Or use optional chaining and fallbacks:
Error: useTranslation must be used within a TranslationProvider
Error: useTranslation must be used within a TranslationProvider
Solution:Make sure your client component is wrapped with the TranslationProvider:Check that you’re not using the hook in a Server Component:
Error: Failed to load dictionary for locale: [locale]
Error: Failed to load dictionary for locale: [locale]
Solution:Check that the dictionary file exists for the specified locale:If it doesn’t exist, create it:Then translate the content or run the translation script:
Getting Help
If you’re still experiencing issues after trying the solutions in this guide, you can:- Check the documentation Review the other internationalization documentation pages for more information.
- Search for similar issues Look for similar issues in the project’s issue tracker or discussion forums.
-
Ask for help
Reach out to the team for assistance, providing:
- A clear description of the issue
- Steps to reproduce the problem
- Error messages (if any)
- Code samples demonstrating the issue