SEO-friendly language redirect via popup

It's not SEO-friendly if you automatically redirect your visitor to the user language, because you might confuse the Google crawler. Here's a script with a popup based on SweetAlert2.

SEO-friendly language redirect via popup

Google doesn't like automatic redirect. Therefore a popup that let's the user choose is a better idea, here's an example code.

First include SweetAlert2 by follwing the instructions or just adding the following version via CDN:

<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>

And here's the script, currently only as a German & English switch, will add generic ones in the future.

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name) {   
    document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
	
jQuery(function($){
	if (!getCookie('displayedLanguagePopup')) {
		userLanguage = (navigator.languages
			? navigator.languages[0]
			: (navigator.language || navigator.userLanguage)).slice(0,2);
			
		setCookie('displayedLanguagePopup', 'yes', 90);

		// use cookie wp-wpml_current_language instead? should be the same though.
		websiteLanguage = document.documentElement.lang.slice(0,2);

		if((userLanguage == 'en') && (websiteLanguage == 'de') ) {
			Swal.fire({
			  title: 'Switch to English?',
			  text: "We detected that your browser language is set to English.",
			  showCancelButton: true,
			  confirmButtonText: "Switch to English",
			  icon: 'question',
			  cancelButtonText: "Continue in German",
			  reverseButtons: true,
			}).then((result) => {
			  if (result.isConfirmed) {
				window.location.replace("https://www.disney.com/");
			  }
			});
		}
		else if((userLanguage == 'de') && (websiteLanguage == 'en') ) {
			Swal.fire({
			  title: 'Auf Deutsch wechseln?',
			  text: "Wir haben erkannt, dass Deine Browsersprache Deutsch ist.",
			  showCancelButton: true,
			  confirmButtonText: "Auf Deutsch wechseln",
			  icon: 'question',
			  cancelButtonText: "In Englisch fortsetzen",
			  reverseButtons: true,
			}).then((result) => {
			  if (result.isConfirmed) {
				window.location.replace("https://www.disney.de/");
			  }
			});
		}
	}
});