Jump to content
overoon

Set Language by Browser Language (only once on startup)

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

Share this post


Link to post
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 ;) )

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By DV-JF
      Hey girls and guys,
      I'll want to open this thread in order to discuss a main problem I've run into with following setup:
       
      I'm maintaining a site where different URLs are directed to the same folder. In site/ready.php the $user->language is set based on the $config->httpHost
      <?php /* Set language based on the domain and user is not logged in */ /* Slovenian */ if( $config->httpHost == "www.domain.si" || $config->httpHost == "domain.si" || $config->httpHost == "domain.si.local") { if (!($user->isSuperuser())) $user->language = $languages->get('si'); } /* German */ elseif( $config->httpHost == "www.domain.at" || $config->httpHost == "domain.at" || $config->httpHost == "domain.at.local" || $config->httpHost == "domain.ch" || $config->httpHost == "www.domain.ch" || $config->httpHost == "domain.de" || $config->httpHost == "www.domain.de") { if (!($user->isSuperuser())) $user->language = $languages->get('default'); } /* Croatian */ elseif( $config->httpHost == "www.domain.hr" || $config->httpHost == "domain.hr" || $config->httpHost == "domain.hr.local") { if (!($user->isSuperuser())) $user->language = $languages->get('hr'); } /* English */ elseif( $config->httpHost == "www.domain.eu" || $config->httpHost == "domain.eu" || $config->httpHost == "domain.eu.local") { if (!($user->isSuperuser())) $user->language = $languages->get('en'); } /* Italian */ elseif( $config->httpHost == "www.domain.it" || $config->httpHost == "domain.it" || $config->httpHost == "domain.it.local") { if (!($user->isSuperuser())) $user->language = $languages->get('it'); } The homepage (id=1) has following settings:

      As you can see German (Deutsch) is set as default language. Everything is working nice and fine and I'm really happy with this kind of setup but now there are some new requirements, which causes me quite a headache :
      I've to add some pages only in one or two languages (they should not be present in German) I've to create some editor roles that are allowed to only edit (can be done with https://processwire.com/docs/user-access/permissions/#multi-language-page-edit-permissions) & add pages to their specific language. What I've found out so far:
      The default language can't be disabled and must always be present (though this would be in my eyes the easiest solution) Creating my own "language select field" - example here: won't work in this case because I've to rely on the native languages in order to setup the right permissions for editors. There seems to be some solutionsbut I think these won't match for me because I've to think about handling editors and permissions, too. After searching and searching, scratching my head and searching and searching again, the only possibility that comes to my mind is to add another language for German and assign this language to the specific URL's instead of the default language.
      The advantages with this solution for me:
      I could activate or deactivate any language on any page Editors which are allowed to add pages can get the permission page-edit-lang-default without affecting the German pages. The disadvantages:
      All multi-language-fields will have an empty tab for the default language - this may irritate editors a lot Seems to me like a lot of work to do because I've to copy the language field nearly for 1000 pages/repeaters (maybe I'll find an SQL query) My questions:
      How would you handle this task? Could my setup be optimized in a completely different way? If I go for my solution (adding another language for German) would it be possible to hide the language tab for default language in any way?  If some points aren't clear enough please don't hesitate to ask.
      Many greets...
    • By snobjorn
      Here's my Norwegian language pack for ProcessWire. I've been adding translations over some time, to suit my own projects users needs.
      Last updated:
      2019/06/19 (June 19, 2019)
      Status:
      Not yet complete. Translations that non-superusers will see is prioritized.
      Name:
      ProcessWire-Norwegian-Language-Pack-nb-NO
      Translated using:
      ProcessWire 3.0.133 dev
      Number of files:
      189 wire files and 95 site files – ranging from just started to completely translated. The site files includes translation some free and some pro modules, see the complete list in the README.md at GitHub.
      Available at GitHub:
      https://github.com/snobjorn/processwire-norwegian-language-pack-nb-no
    • By wish-fulfillment
      how to change the default language in processwire cms
      from Eng  to Ru


    • By louisstephens
      Hopefully this is the right place, if not, please move it to the proper sub. I installed the multi language module on my development (sandbox) set up not too long ago to test out a few things. From the start, I ran into some issues with dependencies not being met/installed, so I thought that I would simply just uninstall what had been. Unfortunately, that took me down a road of using some code in my home template to remove the modules (cant find it at the moment). After some struggling, I finally got it uninstalled, but it left behind 3 fields,
      language language_files language_files_site I thought that I could simply remove them from the languages template and then delete them, but I get the following error:

      I was wondering, is there a way to remove these using the api? I couldn't add any new users to the setup until I made "language" not required, but I was hoping to just remove all of the fields.
    • By bartelsmedia
      "Deepl" is an unbelievably good (paid) text translation service, that mops the floor with Google Translate.
      They have an API and it would be awesome, if there would be a module, that would integrate it for easy content translation.
      It could be a right-click context menu on the language tab:

      It should ask from which of available other language it shall take translate from.
      What do you think?
×
×
  • Create New...