khromov / wp-wpml-geoip-browser-language-redirect

GeoIP Redirector for WPML (WordPress)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

browserLanguage returning null

jonheslop opened this issue · comments

Hey there,

The plugin isn’t on our dev site see — http://testeur.rapha.cc/service/returns
If you look in the console you’ll see I put in a few extra logs to browser-redirect-geoip.js

I see that the script looks up the country with an AJAX call is this what it calls — http://testeur.rapha.cc/?wpml_geoip=1 because that gives an error:

Fatal error: Cannot redeclare geoip_load_shared_mem() (previously declared in /apps/cms/rapha-wordpress/wp-content/plugins/wp-wpml-geoip-browser-language-redirect/lib/geoip-api-php/geoip.inc:241) in /apps/cms/rapha-wordpress/wp-content/plugins/wp-wpml-geoip-browser-language-redirect/lib/geoip-api-php/geoip.inc on line 257

I have declared my language mappings and default language, this is the file — http://jfh.cc/Udfx

Any help would be greatly appreciated

Can you try swapping include() for include_once() for the libraries, as per this commit:
a871554

Nice one that gives the proper json output — http://testeur.rapha.cc/?wpml_geoip=1

But still the redirect doesn’t happen for users in other locales.

http://testeur.rapha.cc/service/returns is translated into all our supported langs e.g. http://testeur.rapha.cc/de/service/returns but it doesn’t redirect automatically?

If I open http://testeur.rapha.cc/de/service/returns incognito in Chrome (no cookies) it redirects me to English, wonderful.

If my German colleague opens http://testeur.rapha.cc/service/returns incognito it sticks in English even though when they open http://testeur.rapha.cc/?wpml_geoip=1 incognito it returns
{"country_code":"de"}

Jon,

Sounds like some of the checks in the JavaScript are not triggering.

Can you check that cookie_enabled is returning true by doing console.log(cookie_enabled); just above it.
https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/js/browser-redirect-geoip.js#L11

Also can you kindly log out cookie_params.name below line 15 to see if it's defined.
https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/js/browser-redirect-geoip.js#L15

Your script is here:
http://testeur.rapha.cc/wp-content/plugins/wp-wpml-geoip-browser-language-redirect/js/browser-redirect-geoip.js?ver=3.8.1

I don't have a WPML setup with the latest version to test on right now but I'll set one up shortly. Just thought I'd get the ball rolling.

Hi Jon,

Everything looks right, so I'll have to experiment for a bit and get back to you.

Hi again Jon,

I have thoroughly reviewed this issue on a test site running WPML 3.1.4, and I have been unable to replicate your problem. Your test site is also currently down, so I am unable to check it.

Regardless, I have released a new version which adds a lot of debug messages in the JavaScript console. After updating to the new version, you may go into the file js/browser-redirect-geoip.js and on line 3 enable GEOIP_DEBUG (from false to true). After that, if you open a debug console in your browser you will see exactly how redirection logic is performed, similar to this:

wpml-redirect-logic

If you try that and get back to me, I can look into it further.

To better understand the way redirection works, check my comment in issue #3

Thanks! Will try it out

Hey there,

Thanks for all your help.

So I dug a little deeper to try and work out the problem and this is what I worked out.

I used a Browserstack instance to pretend to be the Netherlands, which we have translations for.

I added a load of debug messages and commented out the cookie check to see what happens my modified browser-redirect-geoip.js

I visited http://testeur.rapha.cc and was redirected to http://testeur.rapha.cc/nl Great!
Same for http://testeur.rapha.cc/service to http://testeur.rapha.cc/nl/service Wonderful!
But when I try http://testeur.rapha.cc/service/shipping it gets stuck in a redirect loop for like 20 reloads before finally getting it right?!

It goes like this:

(from Dutch IP) goto: http://testeur.rapha.cc/service/shipping
console: pageLanguage = en, browserLanguage = nl
Redirects to http://testeur.rapha.cc/nl/service/shipping
console: pageLanguage = en, browserLanguage = nl

And thats the strange bit! because with that second URL the pageLanguage should be nl This only happens on child pages. You should be able to see this for yourself if you try http://testeur.rapha.cc/service/shipping (our sysadmin locked it down by IP earlier, sorry about that)

I wondered if it was to do with line 63 here that somehow get_bloginfo('language') returns the wrong language.

Perhaps the problem is related to WPML? I could ask them I guess?

Jon,

When I check the pageLanguage on the page:
http://testeur.rapha.cc/nl/service/shipping

The page language comes back correctly:

"pageLanguage":"nl"

However, I had to check this through CURL, because you have modified the script to force redirect users on every page load. (By removing the if (!jQuery.cookie(cookie_name)) check)
That is the reason you are getting a redirect loop. You should uncomment the if check and try again, youll see the pageLanguage is actually correct.

The get_bloginfo() function you refer to is never called. It is a safeguard in case WPML is not enabled. ICL_LANGUAGE_CODE is defined in WPML and is always defined when the plugin is active.

Hope that helps.

Thanks for getting back so quick :D

Ok so I dropped in your JS but added in logs to print out pageLanguage and browserLanguage

I used the Chrome extension Steathly to proxy through France.
If you go to http://testeur.rapha.cc/service it redirected perfectly to http://testeur.rapha.cc/fr/service
But http://testeur.rapha.cc/service/shipping redirects to http://testeur.rapha.cc/service/shipping

So it persists to load the wrong language… the same thing happens with http://testeur.rapha.cc/service/returns (and any other child page)

Screenshot of log

Can you print out the value under the conditional 1? It should contain the french URL.

Add:

console.log(redirectUrl);

Just beneath console.log("Found redirection in conditional 1");

Done :)

Added that to the file… Same result It says its gonna redirect to fr but it just seems to load the same page...

Hey @jonheslop, sorry for the delayed response.

It looks like everything is correct. The only way this would fail is if you are somehow disallowing window.location from being changed (thus disabling the redirect), which can happen with some security extensions in web browsers. I would try this in a few other browsers and maybe a fresh Chrome install without extensions. Closing for now.