Common Issues
1. Translations Not Displaying
Problem: Calling t() function returns the key instead of translated text
Causes:
- Translation resources not loaded correctly
- Namespace configuration error
- Language code mismatch
Solutions:
javascript// check if translation resources are loaded console.log(i18next.store.data); // ensure language codes match i18next.init({ lng: 'en', // ensure it matches the key in resources resources: { en: { translation: { welcome: 'Welcome' } } } }); // check namespace i18next.init({ ns: ['translation', 'common'], defaultNS: 'translation' });
2. Language Switching Not Working
Problem: Translations don't update after calling changeLanguage()
Causes:
- Component not listening to language changes
- Translation resources not loaded
- React component not re-rendering
Solutions:
javascript// use useTranslation in React components function MyComponent() { const { t, i18n } = useTranslation(); useEffect(() => { const handleLanguageChange = () => { // re-render when language changes }; i18n.on('languageChanged', handleLanguageChange); return () => { i18n.off('languageChanged', handleLanguageChange); }; }, [i18n]); return <h1>{t('welcome')}</h1>; } // ensure translation resources are loaded await i18next.loadLanguages(['zh', 'fr']);
3. Interpolation Not Working
Problem: Variables not replaced when using {{variable}} interpolation
Causes:
- Interpolation configuration error
- Variable name typo
- Escape setting issue
Solutions:
javascript// check interpolation configuration i18next.init({ interpolation: { escapeValue: false, // usually false in React prefix: '{{', suffix: '}}' } }); // ensure variable names are correct t('greeting', { name: 'John' }); // translation resources { "greeting": "Hello {{name}}" }
4. Pluralization Not Working Correctly
Problem: Plural forms displayed incorrectly
Causes:
- Plural rules configuration error
- Language doesn't support default plural rules
Solutions:
javascript// configure plural rules for specific languages i18next.init({ lng: 'ru', resources: { ru: { translation: { "item_one": "один предмет", "item_few": "{{count}} предмета", "item_many": "{{count}} предметов", "item_other": "{{count}} предмет" } } } }); // use correct plural keys t('item', { count: 1 }); // один предмет t('item', { count: 2 }); // 2 предмета t('item', { count: 5 }); // 5 предметов
5. Performance Issues
Problem: Translation loading is slow, affecting application performance
Causes:
- Loading too many translation resources
- Not using cache
- Too many network requests
Solutions:
javascript// use lazy loading i18next.init({ ns: ['translation'], // only load necessary namespaces defaultNS: 'translation' }); // load other namespaces when needed i18next.loadNamespaces(['admin', 'settings']); // use cache import LocalStorageBackend from 'i18next-localstorage-backend'; i18next .use(LocalStorageBackend) .init({ backend: { expirationTime: 7 * 24 * 60 * 60 * 1000 // 7 days } }); // preload critical resources i18next.init({ preload: ['en', 'zh'] });
6. Missing Translation Handling
Problem: Missing translations show key instead of fallback text
Solutions:
javascript// configure missing translation handling i18next.init({ fallbackLng: 'en', saveMissing: true, missingKeyHandler: (lng, ns, key) => { console.warn(`Missing translation: ${lng}.${ns}.${key}`); // send to translation management system reportMissingTranslation(lng, ns, key); }, parseMissingKeyHandler: (key) => { return `Translation missing: ${key}`; } });
7. SSR Issues
Problem: Translations not displaying during server-side rendering
Solutions:
javascript// Next.js example import { appWithTranslation } from 'next-i18next'; export default appWithTranslation(MyApp); // get translations on server-side export async function getServerSideProps({ locale }) { return { props: { ...(await serverSideTranslations(locale, ['common', 'home'])) } }; }
8. Memory Leaks
Problem: Event listeners not cleaned up after component unmount
Solutions:
javascriptfunction MyComponent() { const { i18n } = useTranslation(); useEffect(() => { const handleLanguageChange = () => { console.log('Language changed'); }; i18n.on('languageChanged', handleLanguageChange); // clean up event listeners return () => { i18n.off('languageChanged', handleLanguageChange); }; }, [i18n]); return <div>Content</div>; }
Debugging Tips
Enable Debug Mode
javascripti18next.init({ debug: true, // enable debug logging initImmediate: false });
Check Translation Resources
javascriptconsole.log('Current language:', i18next.language); console.log('Loaded languages:', i18next.languages); console.log('Translation data:', i18next.store.data); console.log('Namespaces:', i18next.store.data[i18next.language]);
Listen to Events
javascripti18next.on('loaded', (loaded) => { console.log('Resources loaded:', loaded); }); i18next.on('failedLoading', (lng, ns, msg) => { console.error('Failed to load:', { lng, ns, msg }); }); i18next.on('languageChanged', (lng) => { console.log('Language changed to:', lng); });
Best Practices
- Error Handling: Always handle initialization and loading errors
- Debug Mode: Enable debug mode in development environment
- Logging: Log key events and errors
- Performance Monitoring: Monitor translation loading performance
- Unit Testing: Write tests to cover common issues
- Documentation: Document configuration and usage
- Version Control: Use version numbers to manage translation resources