Even though I have ‘Browser language redirect’ activated at WMPL’s setup screen, users aren’t being correctly redirected according to browser language.
I currently have three languages selected: en (default), es and pt-br.
Only es users are automatically redirected to spanish page. pt-br users aren’t, no matter what browser I use.
I’ve found what was going on, so I’m going to answer it for future reference, as it’s a common issue without clear solution.
The Problem
The problem arises at the redirection javascript provided by WPML (browser-redirect.js):
As you can see at line 4, browserLanguage is trimmed, in a way that pt-br becomes pt, en-us becomes en, and so on. That’s probably there so users from different locations but with same language would both be redirected.
The actual problem is at line 10. languageUrls is an array which contains all languages codes with their respective urls, but the codes aren’t trimmed!
So, if browserLanguage was trimmed, the if condition at line 10 returns false, and then you won’t get redirected:
Also, even if browserLanguage wasn’t trimmed at all, it still wouldn’t work completely, because some browsers pass browserLanguage with capital letters, like pt_BR, and it just wouldn’t match.
My fix is to use two browserLanguage variables, one trimmed and one in lowercase. Then you compare pageLanguage against both of them, so the behaviour for ‘different location, same language’ users will likely stay the same and you fix the bug.
The Solution
Replace the content of /wp-content/plugins/sitepress-multilingual-cms/res/js/browser-redirect.js with this:
Bonus Solution
Users will get redirected only once. This is proposital (and horrible) behaviour. If you’re not satisfied with that, you can try another alternative. Just keep in mind this is a hack that will redirect all users based on their browser language no matter what language they choose, so you MUST have all pages translated and avoid showing a language switcher, as it wont work.