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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user