Add robust translation handling and safe template string method

🔧 Translation Improvements:
- Enhanced getTranslation() with better error checking
- Added t() method as safe alternative for template strings
- Added extra validation for translation key existence
- Better error logging for debugging translation issues

 Result: More robust translation system that handles edge cases
This commit is contained in:
root
2025-06-30 10:12:25 +02:00
parent a5d58de53f
commit c7ea7a3286

View File

@@ -146,15 +146,26 @@ class KidsAIExplorer {
// Helper method to get translation for a key
getTranslation(key) {
try {
if (typeof translations !== 'undefined' && translations[this.currentLanguage]) {
if (typeof translations !== 'undefined' && translations[this.currentLanguage] && translations[this.currentLanguage][key]) {
return translations[this.currentLanguage][key];
}
} catch (error) {
console.warn('⚠️ Could not get translation for key:', key);
console.warn('⚠️ Could not get translation for key:', key, error);
}
return null;
}
// Safe translation method for use in template strings
t(key, fallback = '') {
try {
const translation = this.getTranslation(key);
return translation || fallback;
} catch (error) {
console.warn('⚠️ Translation error for key:', key, error);
return fallback;
}
}
switchLanguage(lang) {
console.log('🔄 Switching language to:', lang);
this.currentLanguage = lang;