Changing language and saving it in cookie
-
Hi,
I’m using this script, which is a modified version of your code from GitHub
jQuery(document).ready(function($) { // This condition prevents a redirect loop: // Redirect only if the home page is called. Change this condition to the specific page or URL you need. // You can remove it if the Javascript is not included in the page where the redirect below points to. console.log(window.location.pathname); //if (window.location.pathname != '/' || window.location.pathname != '/en/') return; geoip_detect.get_info().then(function(record) { if (record.error()) { console.error('WARNING Geodata Error:' + record.error() ); } var country = record.get('country.iso_code'); console.log('Country "' + country + '" detected'); if (country == 'PL' && window.location.pathname == '/en/') { window.location.pathname = '/'; //console.log(window.location.pathname); } else if (country !== 'PL' && window.location.pathname == '/'){ window.location.pathname = '/en/'; } }); });
It basically checks if you are on the polish page (‘/’) and your IP is from outside of Poland, then it redirects to the English page (‘/en/’) and vice versa. It’s working, but my problem is that I have also Polish customers that live abroad and when they want to change the language by hand it automatically redirects them to the English site. I was thinking about making a popup that asks if somebody wants to change the language. But it would be a bit stupid to ask it every time. So I was thinking about maybe storing someone’s choice in the cookie. So if it detects that someone’s IP is outside of Poland and he is currently on the Polish version of the website the popup would pop up and only after clicking some buttons like ‘Change language’ or ‘Keep it’ it would change the language or not. The cookie would store that choice and prevent automatically changing the language of the website and popping popup.
What do you think about it? Have you got a better idea?
Best regards ??
- The topic ‘Changing language and saving it in cookie’ is closed to new replies.