Publisher does not support the Fluid field type. Please do not contact asking when support will be available.

If you purchased an add-on from expressionengine.com, be sure to visit boldminded.com/claim to add the license to your account here on boldminded.com.

Ticket: Check whether user has selected language manually

Status Resolved
Add-on / Version Publisher 1.6.4
Severity
EE Version 2.10.1

Linus Design

Jul 16, 2015

Bit of a complicated one I’m afraid:

When users first visit the the site I’m building, some PHP will detect their country and, if they’re looking at the site in the wrong language, redirect them to the appropriate one. (I have to do this by location rather than with Publisher’s “Get language from browser” option, as the site has both USA and UK versions, and UK browsers often have language set as en-US.)

I then need to display a language switcher at the top of each page. So far that all seems straight forward, but: once the visitor uses the language switcher, or dismisses it using a “close” button, it should not appear any more.

How can I check whether the user has manually selected a language? I thought to build an extension using the publisher_set_language hook, which would save a cookie which I could then use to hide the switcher. But I presume that that hook will also be called by my first bit of code above (when someone first visits the site in the wrong language, and is redirected to the correct one).

Any thoughts on how best to achieve this?

Great module btw!

#1

BoldMinded (Brian)

If you’re already using PHP to redirect them to the right language (us or uk), create a custom cookie with that, then use the set_language hook to compare the two. This sounds like a solvable problem with some custom work.

#2

Linus Design

Hmm, I’m still not sure I get how this could work…

What I’m stuck on is: using the language switcher will trigger the set_language hook, and my extension will store a “language has been manually set” cookie. If that cookie is set, we know not to display the language switcher again. So that all works.

But when they first arrive, my location-detection code will, presumably also trigger the publisher_set_language hook (as I’ll need to use Publisher’s switch_language_url to get them to the right place). So my extension won’t know whether it’s been triggered by the user arriving on the wrong version of the site and being redirected, or by them later switching language. And if it’s the former, it wouldn’t know not to set that cookie, so those people would then not get the language switcher.

Does that make sense? Sorry if I’m missing an obvious solution!

#3

Linus Design

Ah I just found a much simpler – and rather obvious – solution:

I just set the cookie with JS when the user uses the language switcher. Then future pages know not to show it, and nothing else can cause that cookie to be set.

Sorry, should have spotted that sooner – was massively over-thinking it!

#4

BoldMinded (Brian)

Ah, no worries. Glad you found a solution!

Login to reply