fix(i18n): pasang penangkap event klik dan pembuat cookie googtrans

This commit is contained in:
Mas Budi Kusuma
2026-09-18 08:02:16 +07:00
parent 157c63eaff
commit 12eda8a5a0
2 changed files with 86 additions and 4 deletions
+75 -4
View File
@@ -1,5 +1,76 @@
/**
* EduKarsa Theme Core JavaScript
* Native Zero Plugin Architecture
*/
// 1. GLOBAL INITIALIZER UNTUK GOOGLE TRANSLATE
window.googleTranslateElementInit = function() {
if (typeof google !== 'undefined' && google.translate) {
new google.translate.TranslateElement({
pageLanguage: 'id',
autoDisplay: false
}, 'google_translate_element');
}
};
document.addEventListener("DOMContentLoaded", function() {
// Menu Toggle
// 2. LOGIKA SWITCHER BAHASA & MANAJEMEN COOKIE GOOGTRANS
function setLanguageCookie(lang) {
const host = window.location.hostname;
const rootDomain = '.' + host.replace(/^www\./, '');
if (lang === 'id') {
// Reset ke Bahasa Asli (Indonesia)
document.cookie = "googtrans=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.cookie = "googtrans=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=" + host;
document.cookie = "googtrans=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=" + rootDomain;
} else {
// Set bahasa tujuan (misal /id/en, /id/ar, dll.)
const cookieVal = '/id/' + lang;
document.cookie = "googtrans=" + cookieVal + "; path=/;";
document.cookie = "googtrans=" + cookieVal + "; path=/; domain=" + host;
document.cookie = "googtrans=" + cookieVal + "; path=/; domain=" + rootDomain;
}
window.location.reload();
}
// Pasang Event Listener ke seluruh tombol bahasa (.lang-btn)
const langButtons = document.querySelectorAll('.lang-btn');
const matchCookie = document.cookie.match(/googtrans=\/id\/([a-zA-Z\-]+)/i);
const activeLang = matchCookie ? matchCookie[1].toLowerCase() : 'id';
langButtons.forEach(function(btn) {
const targetLang = (btn.getAttribute('data-lang') || '').toLowerCase();
// Tandai tombol yang sedang aktif
if (targetLang === activeLang) {
btn.classList.add('active');
} else {
btn.classList.remove('active');
}
// Tangkap klik pergantian bahasa
btn.addEventListener('click', function(e) {
e.preventDefault();
const selectedLang = this.getAttribute('data-lang');
if (selectedLang) {
setLanguageCookie(selectedLang);
}
});
});
// Sembunyikan paksa toolbar dan frame pop-up bawaan Google Translate
setInterval(function() {
if (document.body.style.top && document.body.style.top !== '0px') {
document.body.style.top = '0px';
}
const banner = document.querySelector('.goog-te-banner-frame, iframe.goog-te-banner-frame, .VIpgJd-ZVi9od-ORHb-OEVmcd');
if (banner) {
banner.style.display = 'none';
}
}, 150);
// 3. MOBILE MENU TOGGLE
const toggleBtn = document.getElementById("mobile-menu-toggle");
const navMenu = document.getElementById("site-navigation");
if (toggleBtn && navMenu) {
@@ -9,7 +80,7 @@ document.addEventListener("DOMContentLoaded", function() {
});
}
// Back to Top
// 4. BACK TO TOP BUTTON
const bttBtn = document.getElementById("back-to-top");
if (bttBtn) {
window.addEventListener("scroll", function() {
@@ -20,7 +91,7 @@ document.addEventListener("DOMContentLoaded", function() {
});
}
// Accessibility Panel
// 5. TOOLBAR AKSESIBILITAS WCAG 2.1
const a11yTrigger = document.getElementById("a11y-trigger");
const a11yPanel = document.getElementById("a11y-panel");
const a11yClose = document.getElementById("a11y-close");
@@ -55,7 +126,7 @@ document.addEventListener("DOMContentLoaded", function() {
});
}
// Homepage Hero Slider Scroll
// 6. HOMEPAGE SLIDER SCROLL OTOMATIS
const slider = document.getElementById('hero-slider-wrapper');
if (slider && slider.children.length > 1) {
setInterval(function() {
+11
View File
@@ -86,5 +86,16 @@ function edukarsa_enqueue_scripts() {
if ( file_exists( get_template_directory() . '/assets/edukarsa.js' ) ) {
wp_enqueue_script( 'edukarsa-global-js', get_template_directory_uri() . '/assets/edukarsa.js', array(), time(), true );
}
// Memuat API Google Translate dengan dependensi ke edukarsa-global-js
if ( get_option( 'edukarsa_tampil_bilingual', 1 ) == 1 ) {
wp_enqueue_script(
'google-translate-api',
'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit',
array( 'edukarsa-global-js' ),
null,
true
);
}
}
add_action( 'wp_enqueue_scripts', 'edukarsa_enqueue_scripts' );