We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43864
    • 151 Posts
    Hey, can somebody point me (or explain here) to a good tutorial to remember the language-choice on a website. (Modx Revo)

    If a visitor picks for example French on a website, I want the next time he visit the website it's automatically French. (If it can from the first time then even better)

    I don't know if it's important, but I use Babel for the languages inside Modx.
      • 36818
      • 119 Posts
      I would solve that problem with JS.

      To remember the language you can set a cookie for the first time, and when the visitor comes back you can read it again.

      	function setCookie(lang) {
      		createCookie("language", lang);
      	}
      
      	function createCookie(name,value,days) {	
      	    if (days) {
      	        var date = new Date();
      	        date.setTime(date.getTime()+(days*24*60*60*1000));
      	        var expires = "; expires="+date.toGMTString();
      	    }
      	    else var expires = "";
      	    document.cookie = name+"="+value+expires+"; path=/";
      	}
      	
      	function readCookie(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) {
      	    createCookie(name,"",-1);
      	}
      	}


      On your Babel-Language links you can set something like
      <a href="babellink" onclick='setCookie("de")'>German</a>


      And then in your pages (at least the starting page) you must read the cookie and redirect the visitor:
      <script type="text/javascript">
      	if (readCookie("language") == "de") {
      		window.location.replace("[[~[[your-startid-de]]]]");                
      	} 
              else if (readCookie("language") == "fr") {
       		window.location.replace("[[~[[your-startid-fr]]]]");                  
              } 
              else if (readCookie("language") == "en") {
       		window.location.replace("[[~[[your-startid-en]]]]");   	                  
              } 
              else {
                window.location.replace("[[~[[your-standard-startid]]]]");         
              };
      
      </script>


      To find out the language even for a fresh visitor you could try to read the browser language.
      For example like such: http://stackoverflow.com/questions/3770513/detect-browser-language-in-php

      All in all, this is not a Revo specific solution and I didn't look for an Extra that would do this.
      But probabely someone else has a more elegant solution.

      Best wishes
      Fabian