|
| 1 | +# Internationalization (i18n) Implementation |
| 2 | + |
| 3 | +This document provides comprehensive documentation for the internationalization implementation in the component library. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The component library now supports multiple languages through a comprehensive internationalization system. The implementation includes: |
| 8 | + |
| 9 | +- Multi-language support for English, Spanish, and French |
| 10 | +- Language switcher component |
| 11 | +- Context-based translation management |
| 12 | +- Automatic language detection |
| 13 | +- Persistent language selection |
| 14 | + |
| 15 | +## Supported Languages |
| 16 | + |
| 17 | +- English (en) - Default language |
| 18 | +- Spanish (es) |
| 19 | +- French (fr) |
| 20 | + |
| 21 | +## Implementation Details |
| 22 | + |
| 23 | +### 1. Translation Files Structure |
| 24 | + |
| 25 | +Translation files are organized in the `public/locales` directory: |
| 26 | + |
| 27 | +``` |
| 28 | +public/ |
| 29 | +└── locales/ |
| 30 | + ├── en/ |
| 31 | + │ └── common.json |
| 32 | + ├── es/ |
| 33 | + │ └── common.json |
| 34 | + └── fr/ |
| 35 | + └── common.json |
| 36 | +``` |
| 37 | + |
| 38 | +Each language has a `common.json` file containing all translations for that language. |
| 39 | + |
| 40 | +### 2. Translation Context |
| 41 | + |
| 42 | +The `I18nContext` provides translation capabilities throughout the application: |
| 43 | + |
| 44 | +```jsx |
| 45 | +import { useI18n } from '../context/I18nContext'; |
| 46 | + |
| 47 | +const { locale, locales, changeLanguage, t } = useI18n(); |
| 48 | +``` |
| 49 | + |
| 50 | +### 3. Translation Hook |
| 51 | + |
| 52 | +The `useTranslation` hook simplifies translation usage in components: |
| 53 | + |
| 54 | +```jsx |
| 55 | +import { useTranslation } from '../hooks/useTranslation'; |
| 56 | + |
| 57 | +const { t, locale } = useTranslation(); |
| 58 | +const translatedText = t('navigation.home'); |
| 59 | +``` |
| 60 | + |
| 61 | +### 4. Language Switcher Component |
| 62 | + |
| 63 | +The `LanguageSwitcher` component provides a UI for changing languages: |
| 64 | + |
| 65 | +```jsx |
| 66 | +import LanguageSwitcher from '../components/LanguageSwitcher'; |
| 67 | + |
| 68 | +<LanguageSwitcher /> |
| 69 | +``` |
| 70 | + |
| 71 | +## Adding New Languages |
| 72 | + |
| 73 | +To add a new language: |
| 74 | + |
| 75 | +1. Create a new directory in `public/locales` with the language code (e.g., `de` for German) |
| 76 | +2. Create a `common.json` file in the new directory with translations |
| 77 | +3. Add the language code to the `locales` array in `next-i18next.config.js` |
| 78 | +4. Add the language name to the `languageNames` object in `LanguageSwitcher.jsx` |
| 79 | + |
| 80 | +Example for German: |
| 81 | + |
| 82 | +```json |
| 83 | +// public/locales/de/common.json |
| 84 | +{ |
| 85 | + "navigation": { |
| 86 | + "home": "Startseite", |
| 87 | + "about": "Über uns", |
| 88 | + "contact": "Kontakt" |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Using Translations in Components |
| 94 | + |
| 95 | +### Basic Usage |
| 96 | + |
| 97 | +```jsx |
| 98 | +import { useTranslation } from '../hooks/useTranslation'; |
| 99 | + |
| 100 | +export default function MyComponent() { |
| 101 | + const { t } = useTranslation(); |
| 102 | + |
| 103 | + return ( |
| 104 | + <h1>{t('homepage.hero_title')}</h1> |
| 105 | + ); |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### With Parameters |
| 110 | + |
| 111 | +```jsx |
| 112 | +// In translation file: |
| 113 | +// "welcome_message": "Welcome, {{name}}!" |
| 114 | + |
| 115 | +const message = t('welcome_message', { name: 'John' }); |
| 116 | +``` |
| 117 | + |
| 118 | +### Nested Translations |
| 119 | + |
| 120 | +```json |
| 121 | +{ |
| 122 | + "homepage": { |
| 123 | + "hero": { |
| 124 | + "title": "Build beautiful apps", |
| 125 | + "description": "Create stunning applications" |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +```jsx |
| 132 | +const title = t('homepage.hero.title'); |
| 133 | +const description = t('homepage.hero.description'); |
| 134 | +``` |
| 135 | + |
| 136 | +## Translation Utilities |
| 137 | + |
| 138 | +The `src/app/utils/translations.js` file provides utility functions for common translation patterns: |
| 139 | + |
| 140 | +- `getTranslatedNavLinks(t)` - Returns translated navigation links |
| 141 | +- `getTranslatedFeatures(t)` - Returns translated feature items |
| 142 | +- `getTranslatedUsageSteps(t)` - Returns translated usage steps |
| 143 | +- `getTranslatedPricingFeatures(t)` - Returns translated pricing features |
| 144 | + |
| 145 | +## Automatic Language Detection |
| 146 | + |
| 147 | +The system automatically detects the user's preferred language based on: |
| 148 | + |
| 149 | +1. Saved preference in localStorage |
| 150 | +2. Browser language settings |
| 151 | +3. Default language (English) as fallback |
| 152 | + |
| 153 | +## Persistent Language Selection |
| 154 | + |
| 155 | +Language preferences are saved to localStorage and persist across sessions. |
| 156 | + |
| 157 | +## Testing Translations |
| 158 | + |
| 159 | +To test translations: |
| 160 | + |
| 161 | +1. Use the language switcher in the navigation bar |
| 162 | +2. Verify all text elements update correctly |
| 163 | +3. Check that the selected language persists after page refresh |
| 164 | +4. Ensure all components display translated content |
| 165 | + |
| 166 | +## Best Practices |
| 167 | + |
| 168 | +1. **Use descriptive translation keys**: Use clear, hierarchical keys like `homepage.hero.title` |
| 169 | +2. **Keep translations consistent**: Maintain consistent terminology across languages |
| 170 | +3. **Test all supported languages**: Verify translations work correctly in all supported languages |
| 171 | +4. **Handle pluralization**: Use appropriate plural forms for different languages |
| 172 | +5. **Consider text length**: Account for varying text lengths in different languages |
| 173 | + |
| 174 | +## Future Improvements |
| 175 | + |
| 176 | +1. **Dynamic imports**: Load translation files on demand |
| 177 | +2. **Translation management tool**: Integrate with a translation management platform |
| 178 | +3. **Additional languages**: Support more languages based on user demand |
| 179 | +4. **RTL support**: Add right-to-left language support |
| 180 | +5. **Translation fallbacks**: Implement better fallback mechanisms |
| 181 | + |
| 182 | +## Troubleshooting |
| 183 | + |
| 184 | +### Missing Translations |
| 185 | + |
| 186 | +If text appears as translation keys: |
| 187 | +1. Verify the key exists in all translation files |
| 188 | +2. Check for typos in the translation keys |
| 189 | +3. Ensure the translation files are properly formatted |
| 190 | + |
| 191 | +### Language Switching Issues |
| 192 | + |
| 193 | +If language switching doesn't work: |
| 194 | +1. Check browser console for errors |
| 195 | +2. Verify the language code is supported |
| 196 | +3. Ensure localStorage is accessible |
| 197 | + |
| 198 | +## Contributing Translations |
| 199 | + |
| 200 | +To contribute translations: |
| 201 | + |
| 202 | +1. Fork the repository |
| 203 | +2. Add or update translation files in `public/locales` |
| 204 | +3. Test the translations in the application |
| 205 | +4. Submit a pull request with your changes |
| 206 | + |
| 207 | +Please ensure translations are accurate and culturally appropriate for the target audience. |
0 commit comments