4

How would you redirect users with an "english browser" automatically to the english site? I have no idea if this can be made with craft or if this is an apache webserver thing. Any idea?

'domain.de' => array(
    'siteUrl' => array(
        'de' => 'http://domain.de/',
        'en' => 'http://domain.de/en/',
    ),
), 
Patte
  • 83
  • 5
  • 1
    Hey Patte, you should probably find an answer to your question here: Locale change based on geolocation. – carlcs Apr 23 '15 at 13:39
  • Hey carls, meanwhile I have successfully tested this solution: 302 redirects to locales in main index.php http://craftcms.stackexchange.com/questions/2108/302-redirects-to-locales-in-main-index-php

    I have no chance to install an apache module so I hope this solves my client needs.

    – Patte Apr 23 '15 at 13:58
  • Hi Patte... If that solution works for you, feel free to add it as an official answer (it's ok to answer your own question), and even mark the answer as "accepted". – Lindsey D Apr 23 '15 at 18:15

2 Answers2

5

Here is what i have done. Not sure if it helps you. I can't add comments. So here you go. This is based on geo plugin

Plugin serviceclass:


//to get local ele
private function getLocalElement()
    {
        $source=craft()->request->getRequestUri();
         if($source==''){
             $source = 'home';
         }
        return  craft()->elements->getElementByUri($source);
    }

//finds locale and redirect
private function redirectToLang()
   {
       $value=$this->detectBrowserLang()."_".strtolower($this->detectCountryCode());
       if(in_array($value, craft()->i18n->siteLocaleIds)==1){
            $element=$this->getLocalElement();
            if(!is_null($element)){
                $localElementUri = craft()->elements->getElementUriForLocale($element->id, $value);
                header('Location: '.$this->configBaseUrl().'/'.$value.'/'.$localElementUri, true, 302); 
            }            
        }      

   }

//browser lang
private function detectBrowserLang()
   {
        $browserLang=craft()->request->getBrowserLanguages();
        if(isset($browserLang)){
            $lang=substr(strtolower($browserLang[0]),0,2); 
            return $lang;
        }
   }

//detect country code by geo
private function detectCountryCode()
    {
        $info=$this->getInfoGeo();
        $countryCode=strtoupper($info["country_code"]);      
        return $countryCode;          
    }


Basically checking browser lang and country code(geo) with craft locales.

lokesh
  • 83
  • 6
2

I recently released Country Redirect, a plugin that handles this smoothly: https://superbig.co/plugins/country-redirect

It lets you map which countries get redirected to which locales, let visitors override the country, and easily let you create a locale nav based on the country mapping.

Fred Carlsen
  • 2,997
  • 13
  • 23