Jump to content

Set Language by Browser Language (only once on startup)


overoon
 Share

Recommended Posts

hi there,

this is my first post in the forums, and i got to say that up to this point working with process wire was a really easy-going experience. now i'm not quite sure if i'm just over-thinking my whole problem and i hope someone can help me.

i have a setup of processwire running with german as default and english as additional language. i successfully implemented a language-switcher which works like a charm.

now i want processwire to automatically check the visitors browserlanguage (basically on starting the session) and set it as default (if not german -> english)

BUT

keep open the possibility to do a manual switch.

my first very, very simple piece of code (which terribly failed) looked like this (in my header template)

if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) != 'de') {
    $user->language = $languages->get('en');
}

now i know what the problem is (my header.inc gets included everytime and does this request even after a switch), but cant make my mind up about a proper solution...

i also didnt find something about this very specific problem by using the search, but am really grateful about any advice :)

best,

overoon

Link to comment
Share on other sites

ok so i did some tinkering and found a way... yet i'm not sure if this is a "good" workaround. just to share:

if(!$session->get('setLang')) {
    if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) != 'de') {
        $user->language = $languages->get('en');
    }
    $session->set('setLang','1');
}

this code sits directly ontop of my header.inc file :)

edited: the main goal is of course to "internationalize" the page... our main visitors are from german speaking countries but all the other languages shall be directed to an english version (just in case you were asking yourself about the if clause logic ;) )

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for posting the solution you came up with here. One thing I just wanted to mention is that you may be better off using some kind of language gateway or language switcher at the top of the page (something that can be understood by all your target languages) and ensuring that each language version of a page resolves to a different URL (ensuring no 1 URL is serving more than 1 language). My understanding is that Google advises against modifying the output of the request based on the accept_language header, and thus could be an seo concern. 

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for posting the solution you came up with here. One thing I just wanted to mention is that you may be better off using some kind of language gateway or language switcher at the top of the page (something that can be understood by all your target languages) and ensuring that each language version of a page resolves to a different URL (ensuring no 1 URL is serving more than 1 language). My understanding is that Google advises against modifying the output of the request based on the accept_language header, and thus could be an seo concern. 

Thanks for your input, i changed my code and this should be working fine now:

if(!$session->get('setLang')) {
    if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) != 'de') {
        $session->set('setLang','1');
        $url = $page->localUrl('en');
        $session->redirect($url);
    }
    $session->set('setLang','1');
}

if i'm correct, the problem was, that when my user clicks the link to /de/team/member-name and has an english browser, the domain wouldnt change, but all the contents were in the right language. now i'm using a _real_ redirect, causing even a crawler, calling upon /de/team/member-name to get to /en/team/member-name

i really need the automatic redirection for non-germans, is this a proper way?

Thanks

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...