This document provides comprehensive documentation for the internationalization implementation in the component library.
The component library now supports multiple languages through a comprehensive internationalization system. The implementation includes:
- Multi-language support for English, Spanish, and French
- Language switcher component
- Context-based translation management
- Automatic language detection
- Persistent language selection
- English (en) - Default language
- Spanish (es)
- French (fr)
Translation files are organized in the public/locales directory:
public/
└── locales/
├── en/
│ └── common.json
├── es/
│ └── common.json
└── fr/
└── common.json
Each language has a common.json file containing all translations for that language.
The I18nContext provides translation capabilities throughout the application:
import { useI18n } from '../context/I18nContext';
const { locale, locales, changeLanguage, t } = useI18n();The useTranslation hook simplifies translation usage in components:
import { useTranslation } from '../hooks/useTranslation';
const { t, locale } = useTranslation();
const translatedText = t('navigation.home');The LanguageSwitcher component provides a UI for changing languages:
import LanguageSwitcher from '../components/LanguageSwitcher';
<LanguageSwitcher />To add a new language:
- Create a new directory in
public/localeswith the language code (e.g.,defor German) - Create a
common.jsonfile in the new directory with translations - Add the language code to the
localesarray innext-i18next.config.js - Add the language name to the
languageNamesobject inLanguageSwitcher.jsx
Example for German:
// public/locales/de/common.json
{
"navigation": {
"home": "Startseite",
"about": "Über uns",
"contact": "Kontakt"
}
}import { useTranslation } from '../hooks/useTranslation';
export default function MyComponent() {
const { t } = useTranslation();
return (
<h1>{t('homepage.hero_title')}</h1>
);
}// In translation file:
// "welcome_message": "Welcome, {{name}}!"
const message = t('welcome_message', { name: 'John' });{
"homepage": {
"hero": {
"title": "Build beautiful apps",
"description": "Create stunning applications"
}
}
}const title = t('homepage.hero.title');
const description = t('homepage.hero.description');The src/app/utils/translations.js file provides utility functions for common translation patterns:
getTranslatedNavLinks(t)- Returns translated navigation linksgetTranslatedFeatures(t)- Returns translated feature itemsgetTranslatedUsageSteps(t)- Returns translated usage stepsgetTranslatedPricingFeatures(t)- Returns translated pricing features
The system automatically detects the user's preferred language based on:
- Saved preference in localStorage
- Browser language settings
- Default language (English) as fallback
Language preferences are saved to localStorage and persist across sessions.
To test translations:
- Use the language switcher in the navigation bar
- Verify all text elements update correctly
- Check that the selected language persists after page refresh
- Ensure all components display translated content
- Use descriptive translation keys: Use clear, hierarchical keys like
homepage.hero.title - Keep translations consistent: Maintain consistent terminology across languages
- Test all supported languages: Verify translations work correctly in all supported languages
- Handle pluralization: Use appropriate plural forms for different languages
- Consider text length: Account for varying text lengths in different languages
- Dynamic imports: Load translation files on demand
- Translation management tool: Integrate with a translation management platform
- Additional languages: Support more languages based on user demand
- RTL support: Add right-to-left language support
- Translation fallbacks: Implement better fallback mechanisms
If text appears as translation keys:
- Verify the key exists in all translation files
- Check for typos in the translation keys
- Ensure the translation files are properly formatted
If language switching doesn't work:
- Check browser console for errors
- Verify the language code is supported
- Ensure localStorage is accessible
To contribute translations:
- Fork the repository
- Add or update translation files in
public/locales - Test the translations in the application
- Submit a pull request with your changes
Please ensure translations are accurate and culturally appropriate for the target audience.