From c7ea7a32865d1d27116b672ea46f712ac0837499 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 30 Jun 2025 10:12:25 +0200 Subject: [PATCH] Add robust translation handling and safe template string method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 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 --- html/kidsai/script-new.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/html/kidsai/script-new.js b/html/kidsai/script-new.js index 26fb698..c6d64ea 100644 --- a/html/kidsai/script-new.js +++ b/html/kidsai/script-new.js @@ -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;