Skip to content

Commit 86b7146

Browse files
committed
Merge branch 'TMS-1237-fix' into stage
2 parents 8fc89fc + f98e7b1 commit 86b7146

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

CHANGELOG.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- Update modaal string null-guards
1212
- TMS-1141: Add if-statement for block-subpages title container
1313
- Update Node, webpack & dependency versions
14+
- TMS-1237: Fix script load on cookie consent without page refresh
15+
16+
## [1.73.0] - 2026-05-05
17+
1418
- TMS-1237: Add a custom Google Translate functionality to navigation
1519
- Remove delete site-link from wp-admin
1620

assets/scripts/gtranslate-dropdown.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Theme Name: TMS Theme Base
33
* Description: Tampere Multisite Base Theme
44
* Domain Path: /lang
5-
* Version: 1.72.0
5+
* Version: 1.73.0
66
* Author: Geniem
77
* Author URI: https://geniem.fi
88
* Text Domain: tms-theme-base

0 commit comments

Comments
 (0)