Skip to content

Conversation

@oilmonegov
Copy link

@oilmonegov oilmonegov commented Nov 16, 2025

Add Internationalization (i18n) Support with Comprehensive Test Suite

Overview

This PR adds full internationalization (i18n) support to the MNIST MLP Inference Visualization application, enabling users to switch between English and German languages. The implementation includes a robust i18n module, translation files, UI integration, and a comprehensive test suite with 65+ tests.

Features

🌍 Internationalization Module (assets/i18n.js)

  • Translation System: Complete i18n module with support for nested translation keys using dot notation
  • Language Switching: Seamless switching between English (en) and German (de)
  • Parameter Substitution: Support for dynamic parameters in translations (e.g., t("aria.loadRandom", { digit: 5 }))
  • Multiple Attribute Types: Support for:
    • data-i18n - Text content translation
    • data-i18n-html - HTML content translation
    • data-i18n-aria-label - ARIA label translation
  • Persistence: Language preference saved to localStorage
  • Accessibility: Automatic HTML lang attribute updates and translatable ARIA labels
  • Event System: Custom languageChanged event for components that need to react to language changes
  • Error Handling: Graceful fallback to English if translation files fail to load

📝 Translation Files

  • English (assets/i18n/en.json): Complete English translations for all UI elements
  • German (assets/i18n/de.json): Complete German translations for all UI elements
  • Coverage: Translations for:
    • Page title and metadata
    • Drawing canvas instructions
    • 3D controls and interactions
    • Mobile/touch controls
    • Predictions display
    • Network overview
    • Neuron detail panel
    • Timeline controls
    • Info modal
    • Advanced settings
    • Error messages
    • ARIA labels and accessibility text
    • FPS display

🎨 UI Integration

  • Language Toggle Button: Added language toggle button in the UI header
  • HTML Integration: All translatable text uses data-i18n attributes
  • JavaScript Integration: Dynamic translations using t() function throughout main.js
  • Real-time Updates: UI updates immediately when language is switched

Testing

Comprehensive Test Suite (test-i18n.js)

A robust Node.js-based test suite with 65 tests across 8 test suites:

  1. Translation File Structure and Completeness (7 tests)

    • Validates JSON structure and syntax
    • Ensures both languages have matching keys
    • Verifies all values are strings
    • Checks parameter placeholder consistency
  2. i18n.js Module Code Analysis (18 tests)

    • Verifies all exported functions exist
    • Checks error handling and fallback behavior
    • Validates localStorage usage
    • Ensures event dispatching
  3. HTML Integration (14 tests)

    • Verifies data-i18n attributes are used correctly
    • Checks language toggle button implementation
    • Validates all translation keys exist in translation files
    • Ensures proper HTML structure
  4. JavaScript Integration (9 tests)

    • Verifies t() function usage in main.js
    • Checks translation key references
    • Validates parameter substitution
    • Ensures language change listeners
  5. Translation Key Coverage (3 tests)

    • Verifies error keys are used
    • Checks UI key references
    • Validates parameter placeholders
  6. Code Quality and Best Practices (5 tests)

    • Checks for hardcoded German text
    • Validates translation file formatting
    • Ensures no empty strings
    • Verifies naming conventions
  7. Edge Cases and Error Handling (5 tests)

    • Tests missing key handling
    • Validates fallback behavior
    • Checks special character handling
    • Tests parameter substitution edge cases
  8. Accessibility and Internationalization (4 tests)

    • Verifies HTML lang attribute updates
    • Checks translatable aria-labels
    • Validates accessibility attributes
    • Ensures proper ARIA roles

Browser Test Suite (test-i18n-browser.html)

A browser-based test suite that tests runtime behavior:

  • Basic i18n module functionality
  • Language switching behavior
  • DOM element translation
  • Error handling
  • LocalStorage persistence
  • Translation completeness

Unified Test Runner (test.js)

A convenient unified test runner that:

  • Executes all Node.js-based tests
  • Provides colored output and clear summaries
  • Shows overall pass/fail status
  • Reminds about browser tests

Test Documentation (TEST-README.md)

Comprehensive documentation covering:

  • How to run all tests
  • Test file descriptions
  • Test coverage details
  • Troubleshooting guide
  • CI/CD integration examples

Usage

Running Tests

# Run all tests (recommended)
node test.js

# Run comprehensive tests directly
node test-i18n.js

# Open browser tests
open test-i18n-browser.html

Using Translations in Code

// Simple translation
const text = t("drawing.title");

// Translation with parameters
const ariaLabel = t("aria.loadRandom", { digit: 5 });

// Listen for language changes
window.addEventListener('languageChanged', (event) => {
  console.log('Language changed to:', event.detail.language);
  // Update UI accordingly
});

Adding New Translations

  1. Add translation keys to both assets/i18n/en.json and assets/i18n/de.json
  2. Use data-i18n="key.path" in HTML or t("key.path") in JavaScript
  3. Run tests to verify: node test.js

Files Changed

New Files

  • assets/i18n.js - i18n module implementation
  • assets/i18n/en.json - English translations
  • assets/i18n/de.json - German translations
  • test-i18n.js - Comprehensive test suite (65 tests)
  • test-i18n-browser.html - Browser-based test suite
  • test.js - Unified test runner
  • TEST-README.md - Test documentation

Modified Files

  • index.html - Added i18n script, language toggle button, and data-i18n attributes
  • assets/main.js - Integrated t() function for dynamic translations
  • assets/main.css - Styling updates (if any)

Testing Results

All tests pass:

  • 65 tests passed, 0 failed
  • ✅ Translation files validated
  • ✅ Code integration verified
  • ✅ Edge cases handled
  • ✅ Accessibility requirements met

Benefits

  1. User Experience: Users can now use the application in their preferred language
  2. Maintainability: Centralized translation system makes it easy to add new languages
  3. Quality Assurance: Comprehensive test suite ensures translations work correctly
  4. Accessibility: Proper ARIA labels and HTML lang attributes improve accessibility
  5. Developer Experience: Clear API and documentation make it easy to add new translations

Future Enhancements

  • Add support for additional languages (e.g., French, Spanish)
  • Implement RTL (right-to-left) language support
  • Add translation key validation in CI/CD pipeline
  • Consider using a translation management system for larger scale

Checklist

  • i18n module implemented
  • English translations complete
  • German translations complete
  • HTML integration with data-i18n attributes
  • JavaScript integration with t() function
  • Language toggle button added
  • LocalStorage persistence implemented
  • Comprehensive test suite (65 tests)
  • Browser test suite
  • Test documentation
  • All tests passing
  • No hardcoded text remaining
  • Accessibility requirements met

…y instructional text for clarity and accessibility.
…language switching and updating UI elements in main.js and index.html. Enhance accessibility with aria-labels and ensure consistent English translations across the application.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant