Files
reactShop/MULTILINGUAL_IMPLEMENTATION.md

5.1 KiB

Multilingual Implementation Guide

Overview

Your website now supports multiple languages using react-i18next. The implementation is designed to work seamlessly with your existing class components and provides:

  • German (default) and English support
  • Language persistence in localStorage
  • Dynamic language switching
  • SEO-friendly language attributes
  • Class component compatibility

Features Implemented

1. Language Switcher

  • Located in the header next to the login/profile button
  • Shows current language (DE/EN) with flag icon
  • Dropdown menu for language selection
  • Persists selection in browser storage

2. Translated Components

  • Header navigation: Categories, Home links
  • Authentication: Login/register forms, profile menu
  • Main pages: Home, Actions, Store pages
  • Cart: Shopping cart title and sync dialog
  • Product pages: Basic UI elements (more can be added)
  • Footer: Basic elements (can be expanded)

3. Architecture

  • src/i18n/index.js - Main i18n configuration
  • src/i18n/withTranslation.js - HOCs for class components
  • src/i18n/locales/de/translation.json - German translations
  • src/i18n/locales/en/translation.json - English translations
  • src/components/LanguageSwitcher.js - Language selection component

Usage for Developers

Using Translations in Class Components

import { withI18n } from '../i18n/withTranslation.js';

class MyComponent extends Component {
  render() {
    const { t } = this.props; // Translation function
    
    return (
      <Typography>
        {t('navigation.home')} // Translates to "Startseite" or "Home"
      </Typography>
    );
  }
}

export default withI18n()(MyComponent);

Using Translations in Function Components

import { useTranslation } from 'react-i18next';

const MyComponent = () => {
  const { t } = useTranslation();
  
  return (
    <Typography>
      {t('navigation.home')}
    </Typography>
  );
};

Language Context Access

import { withLanguage } from '../i18n/withTranslation.js';

class MyComponent extends Component {
  render() {
    const { languageContext } = this.props;
    
    return (
      <Button onClick={() => languageContext.changeLanguage('en')}>
        Switch to English
      </Button>
    );
  }
}

export default withLanguage(MyComponent);

Adding New Translations

1. Add to German (src/i18n/locales/de/translation.json)

{
  "newSection": {
    "title": "Neuer Titel",
    "description": "Neue Beschreibung"
  }
}

2. Add to English (src/i18n/locales/en/translation.json)

{
  "newSection": {
    "title": "New Title", 
    "description": "New Description"
  }
}

3. Use in Components

{t('newSection.title')}

Adding New Languages

1. Create Translation File

Create src/i18n/locales/fr/translation.json for French

2. Update i18n Configuration

// src/i18n/index.js
import translationFR from './locales/fr/translation.json';

const resources = {
  de: { translation: translationDE },
  en: { translation: translationEN },
  fr: { translation: translationFR } // Add new language
};

3. Update Language Provider

// src/i18n/withTranslation.js
availableLanguages: ['de', 'en', 'fr'] // Add to available languages

4. Update Language Switcher

// src/components/LanguageSwitcher.js
const names = {
  'de': 'Deutsch',
  'en': 'English',
  'fr': 'Français' // Add language name
};

Configuration Options

Language Detection Order

Currently set to: ['localStorage', 'navigator', 'htmlTag']

  • First checks localStorage for saved preference
  • Falls back to browser language
  • Finally checks HTML lang attribute

Fallback Language

Set to German (de) as your primary language

Debug Mode

Enabled in development mode for easier debugging

SEO Considerations

  • HTML lang attribute updates automatically
  • Config object provides language-specific metadata
  • Descriptions and keywords are language-aware
  • Can be extended for hreflang tags and URL localization

Best Practices

  1. Namespace your translations - Use nested objects for organization
  2. Provide fallbacks - Always have German as fallback since it's your primary market
  3. Use interpolation - For dynamic content: t('welcome', { name: 'John' })
  4. Keep translations consistent - Use same structure in all language files
  5. Test thoroughly - Verify all UI elements in both languages

Current Translation Coverage

  • Navigation and menus
  • Authentication flows
  • Basic product elements
  • Cart functionality
  • Main page content
  • Detailed product descriptions (can be added)
  • Legal pages content (can be added)
  • Form validation messages (can be added)
  • Error messages (can be added)

Performance

  • Translations are bundled and loaded immediately
  • No additional network requests
  • Lightweight implementation
  • Language switching is instant

Browser Support

Works with all modern browsers that support:

  • ES6 modules
  • localStorage
  • React 19

The implementation is production-ready and can be extended based on your specific needs!