tractatus/public/js/i18n-simple.js
TheFlow 06c3631ec4 feat: add multilingual support for 5 key pages (researcher, leader, implementer, about, faq)
Translation Infrastructure:
- Created 15 new translation files (en/de/fr) for 5 pages
- Enhanced i18n-simple.js to auto-detect page names
- Added page detection logic mapping URLs to translation files
- Supports researcher, leader, implementer, about, faq pages

Translation Files Created:
English (en/):
  - researcher.json (research foundations, empirical observations)
  - leader.json (governance gap, architectural approach, EU AI Act)
  - implementer.json (integration approaches, quick start, deployment)
  - about.json (mission, values, origin story, license)
  - faq.json (search modal, browse by audience, tips)

German (de/):
  - researcher.json (Forschungsgrundlagen, Empirische Beobachtungen)
  - leader.json (Governance-Lücke, Architektonischer Ansatz)
  - implementer.json (Integrationsansätze, Schnellstart)
  - about.json (Mission, Werte, Ursprungsgeschichte)
  - faq.json (Häufig gestellte Fragen)

French (fr/):
  - researcher.json (Fondements de Recherche, Observations Empiriques)
  - leader.json (Lacune de Gouvernance, Approche Architecturale)
  - implementer.json (Approches d'Intégration, Démarrage Rapide)
  - about.json (Mission, Valeurs, Histoire d'Origine)
  - faq.json (Questions Fréquemment Posées)

Technical Changes:
- i18n-simple.js: Added detectPageName() method
- Maps URL paths to translation file names
- Loads page-specific translations automatically
- researcher.html: Added data-i18n attributes to header section

Language Selector:
- Already deployed on all 6 pages (mobile icon-based design)
- Now backed by full translation infrastructure
- Switching languages loads correct page-specific translations

Implementation Status:
 Translation files: Complete (15 files, ~350 translation keys)
 i18n system: Enhanced with page detection
 Proof of concept: Working on researcher.html
 Full implementation: data-i18n attributes needed on remaining pages

Next Steps for Full i18n:
- Add data-i18n attributes to leader.html (~60 elements)
- Add data-i18n attributes to implementer.html (~70 elements)
- Add data-i18n attributes to about.html (~40 elements)
- Add data-i18n attributes to faq.html (~30 elements)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-16 23:11:12 +13:00

182 lines
4.6 KiB
JavaScript

/**
* Simple i18n system for Tractatus
* Supports: en (English), de (German), fr (French), mi (Te Reo Māori - coming soon)
*/
const I18n = {
currentLang: 'en',
translations: {},
supportedLanguages: ['en', 'de', 'fr'],
async init() {
// 1. Detect language preference
this.currentLang = this.detectLanguage();
// 2. Load translations
await this.loadTranslations(this.currentLang);
// 3. Apply to page
this.applyTranslations();
// 4. Update language selector if present
this.updateLanguageSelector();
console.log(`[i18n] Initialized with language: ${this.currentLang}`);
},
detectLanguage() {
// Check localStorage first
const saved = localStorage.getItem('tractatus-lang');
if (saved && this.supportedLanguages.includes(saved)) {
return saved;
}
// Check browser language
const browserLang = (navigator.language || navigator.userLanguage).split('-')[0];
if (this.supportedLanguages.includes(browserLang)) {
return browserLang;
}
// Default to English
return 'en';
},
detectPageName() {
// Try to get page name from data attribute first
const pageAttr = document.documentElement.getAttribute('data-page');
if (pageAttr) {
return pageAttr;
}
// Detect from URL path
const path = window.location.pathname;
// Map paths to translation file names
const pageMap = {
'/': 'homepage',
'/index.html': 'homepage',
'/researcher.html': 'researcher',
'/leader.html': 'leader',
'/implementer.html': 'implementer',
'/about.html': 'about',
'/faq.html': 'faq'
};
return pageMap[path] || 'homepage';
},
async loadTranslations(lang) {
try {
const pageName = this.detectPageName();
const response = await fetch(`/locales/${lang}/${pageName}.json`);
if (!response.ok) {
throw new Error(`Failed to load translations for ${lang}/${pageName}`);
}
this.translations = await response.json();
console.log(`[i18n] Loaded translations for page: ${pageName}`);
} catch (error) {
console.error(`[i18n] Error loading translations:`, error);
// Fallback to English if loading fails
if (lang !== 'en') {
this.currentLang = 'en';
await this.loadTranslations('en');
}
}
},
t(key) {
const keys = key.split('.');
let value = this.translations;
for (const k of keys) {
if (value && typeof value === 'object') {
value = value[k];
} else {
return key; // Return key if translation not found
}
}
return value || key;
},
applyTranslations() {
// Find all elements with data-i18n attribute
document.querySelectorAll('[data-i18n]').forEach(el => {
const key = el.dataset.i18n;
const translation = this.t(key);
if (typeof translation === 'string') {
el.textContent = translation;
}
});
// Handle data-i18n-html for HTML content
document.querySelectorAll('[data-i18n-html]').forEach(el => {
const key = el.dataset.i18nHtml;
const translation = this.t(key);
if (typeof translation === 'string') {
el.innerHTML = translation;
}
});
},
async setLanguage(lang) {
if (!this.supportedLanguages.includes(lang)) {
console.error(`[i18n] Unsupported language: ${lang}`);
return;
}
// Save preference
localStorage.setItem('tractatus-lang', lang);
// Update current language
this.currentLang = lang;
// Reload translations
await this.loadTranslations(lang);
// Reapply to page
this.applyTranslations();
// Update selector
this.updateLanguageSelector();
// Update HTML lang attribute
document.documentElement.lang = lang;
// Dispatch event for language change
window.dispatchEvent(new CustomEvent('languageChanged', {
detail: { language: lang }
}));
console.log(`[i18n] Language changed to: ${lang}`);
},
updateLanguageSelector() {
const selector = document.getElementById('language-selector');
if (selector) {
selector.value = this.currentLang;
}
},
getLanguageName(code) {
const names = {
'en': 'English',
'de': 'Deutsch',
'fr': 'Français',
'mi': 'Te Reo Māori'
};
return names[code] || code;
}
};
// Auto-initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => I18n.init());
} else {
I18n.init();
}
// Make available globally
window.I18n = I18n;