@@ -12,6 +12,7 @@ export default class GtranslateDropdown {
1212
1313 constructor ( ) {
1414 this . isGoogleLoaded = false ;
15+ this . isGoogleInitialized = false ;
1516 this . eventsAttached = false ;
1617 this . gtranslateCheckRetries = 0 ;
1718 this . gtranslateCheckMaxRetries = 3 ;
@@ -99,6 +100,14 @@ export default class GtranslateDropdown {
99100 return ;
100101 }
101102
103+ // If Google Translate is already available, initialize it immediately.
104+ // This avoids relying on a full page reload after consent changes.
105+ // eslint-disable-next-line no-undef
106+ if ( typeof google !== 'undefined' && google . translate ) {
107+ this . initGoogleTranslate ( ) ;
108+ return ;
109+ }
110+
102111 this . loadGoogleTranslateAPI ( ) ;
103112 }
104113
@@ -180,20 +189,35 @@ export default class GtranslateDropdown {
180189 * @return {void }
181190 */
182191 loadGoogleTranslateAPI ( ) {
183- if ( this . isGoogleLoaded || document . querySelector ( 'script[src*="translate.google.com"]' ) ) {
192+ // Define the callback before injecting the script so the API can call it reliably.
193+ window . googleTranslateElementInit = this . initGoogleTranslate . bind ( this ) ;
194+
195+ // eslint-disable-next-line no-undef
196+ if ( typeof google !== 'undefined' && google . translate ) {
197+ this . initGoogleTranslate ( ) ;
198+ return ;
199+ }
200+
201+ if ( this . isGoogleLoaded ) {
202+ return ;
203+ }
204+
205+ const existingScript = document . querySelector ( 'script[src*="translate.google.com"]' ) ;
206+
207+ if ( existingScript ) {
208+ this . isGoogleLoaded = true ;
209+ setTimeout ( ( ) => {
210+ this . checkGoogleTranslateLoaded ( ) ;
211+ } , this . gtranslateCheckDelay ) ;
184212 return ;
185213 }
186214
187215 const script = document . createElement ( 'script' ) ;
188216 script . src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit' ;
189- script . setAttribute ( 'data-cookieconsent' , 'preferences' ) ;
190217 script . async = true ;
191218 document . head . appendChild ( script ) ;
192219 this . isGoogleLoaded = true ;
193220
194- // Define the callback function globally
195- window . googleTranslateElementInit = this . initGoogleTranslate . bind ( this ) ;
196-
197221 // Check if the script loaded successfully after a short delay
198222 setTimeout ( ( ) => {
199223 this . checkGoogleTranslateLoaded ( ) ;
@@ -213,6 +237,11 @@ export default class GtranslateDropdown {
213237 return ;
214238 }
215239
240+ if ( container . children . length > 0 || this . isGoogleInitialized ) {
241+ this . setDropdownVisibility ( true ) ;
242+ return ;
243+ }
244+
216245 // Get current language from data attribute or default to 'fi'
217246 const currentLang = container . dataset . lang || 'fi' ;
218247
@@ -221,6 +250,10 @@ export default class GtranslateDropdown {
221250 pageLanguage : currentLang ,
222251 autoDisplay : false ,
223252 } , 'google_translate_element_custom' ) ;
253+
254+ this . isGoogleInitialized = true ;
255+ this . gtranslateCheckRetries = 0 ;
256+ this . setDropdownVisibility ( true ) ;
224257 }
225258
226259 /**
0 commit comments