Mavidart commited on
Commit
c2c4409
·
verified ·
1 Parent(s): b50fbf2

Manual changes saved

Browse files
Files changed (1) hide show
  1. script.js +57 -19
script.js CHANGED
@@ -50,30 +50,68 @@ feather.replace();
50
  });
51
 
52
  function updateLanguage(language) {
53
- // Update all elements with data-i18n attributes
54
- document.querySelectorAll('[data-i18n]').forEach(element => {
55
- const key = element.getAttribute('data-i18n');
56
- if (translations[language] && translations[language][key]) {
57
- element.textContent = translations[language][key];
58
- } else if (translations['es'][key]) {
59
- // Fallback to Spanish if translation missing
60
- element.textContent = translations['es'][key];
61
- }
62
- });
63
 
64
- // Update HTML lang attribute
65
- document.documentElement.lang = language;
 
 
 
 
 
 
 
 
 
66
 
67
- // Update active language in selector
68
- document.querySelectorAll('.language-option').forEach(option => {
69
- if (option.getAttribute('data-lang') === language) {
70
- option.classList.add('active');
71
- } else {
72
- option.classList.remove('active');
73
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  }
76
 
 
77
  function filterBlogPosts(category) {
78
  const blogPosts = document.querySelectorAll('.blog-post');
79
  blogPosts.forEach(post => {
 
50
  });
51
 
52
  function updateLanguage(language) {
53
+ // Safety: ensure translations object exists
54
+ if (typeof translations === 'undefined') return console.warn('translations no definido');
 
 
 
 
 
 
 
 
55
 
56
+ // 1) Light DOM: actualizar elementos regulares con data-i18n
57
+ document.querySelectorAll('[data-i18n]').forEach(element => {
58
+ // evita tocar elementos que estén dentro de un shadowRoot (los veremos después)
59
+ if (element.getRootNode && element.getRootNode() instanceof ShadowRoot) return;
60
+ const key = element.getAttribute('data-i18n');
61
+ if (translations[language] && translations[language][key]) {
62
+ element.textContent = translations[language][key];
63
+ } else if (translations['es'] && translations['es'][key]) {
64
+ element.textContent = translations['es'][key];
65
+ }
66
+ });
67
 
68
+ // 2) HTML lang attribute
69
+ document.documentElement.lang = language;
70
+
71
+ // 3) Actualizar opciones de idioma visibles en el Light DOM (buscamos por data-lang)
72
+ document.querySelectorAll('[data-lang]').forEach(option => {
73
+ if (option.getAttribute('data-lang') === language) {
74
+ option.classList.add('active');
75
+ } else {
76
+ option.classList.remove('active');
77
+ }
78
+ });
79
+
80
+ // 4) PASO IMPORTANTE: actualizar cualquier header / componente con Shadow DOM
81
+ // Busca componentes custom que podrían tener shadowRoot y [data-i18n] dentro
82
+ document.querySelectorAll('custom-header').forEach(comp => {
83
+ const sr = comp.shadowRoot;
84
+ if (!sr) return;
85
+ // actualizar textos dentro del shadow root
86
+ sr.querySelectorAll('[data-i18n]').forEach(el => {
87
+ const key = el.getAttribute('data-i18n');
88
+ if (translations[language] && translations[language][key]) {
89
+ el.textContent = translations[language][key];
90
+ } else if (translations['es'] && translations['es'][key]) {
91
+ el.textContent = translations['es'][key];
92
+ }
93
  });
94
+
95
+ // si tu header usa un indicador (bandera/código) con IDs 'currentFlag'/'currentCode', actualizarlos
96
+ const flag = sr.getElementById('currentFlag');
97
+ const code = sr.getElementById('currentCode');
98
+ if (flag && code) {
99
+ if (language === 'es') { flag.textContent = '🇪🇸'; code.textContent = 'ES'; }
100
+ else if (language === 'en') { flag.textContent = '🇬🇧'; code.textContent = 'EN'; }
101
+ else if (language === 'fr') { flag.textContent = '🇫🇷'; code.textContent = 'FR'; }
102
+ else { flag.textContent = '🌐'; code.textContent = language.toUpperCase(); }
103
+ }
104
+
105
+ // si el componente expone un método público applyLanguage, llámalo también (compatibilidad)
106
+ try {
107
+ if (typeof comp.applyLanguage === 'function') comp.applyLanguage(language);
108
+ } catch (err) {
109
+ console.warn('applyLanguage fallo en custom-header', err);
110
+ }
111
+ });
112
  }
113
 
114
+
115
  function filterBlogPosts(category) {
116
  const blogPosts = document.querySelectorAll('.blog-post');
117
  blogPosts.forEach(post => {