1

I have created a website in two languages with CodeIgniter: English (en) and German (de).

The default page is: www.mysite.com

The url for en is: www.mysite.com/en/ The url for de is www.mysite.com/de/

Now I would like to redirect the user according to his/her device language. I know, the script below it's not the right way.

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch($lang){
case 'en':
return redirect(site_url('en/'));
break;
case 'de';
return redirect(site_url('de/'));
break;
default:
return redirect(site_url('en/'));
}

How can I exactly manage it?

user4269288
  • 109
  • 2
  • 3
  • 10
  • Why don't you just redirect(site_url($lang)) ? EDIT: Nevermind, you need a default of english if it is not one of the two. You would need to swtich Lang both ways – Bryan Zwicker May 15 '15 at 13:09
  • possible duplicate of [Detect Browser Language in PHP](http://stackoverflow.com/questions/3770513/detect-browser-language-in-php) – machineaddict May 15 '15 at 13:14
  • 1
    remove return from redirect and try – Saty May 15 '15 at 13:16

1 Answers1

0

I found there is syntax error in your code at case 'de';

It would be case 'de':

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch($lang){
case 'en':
redirect(site_url('en/'));
break;
case 'de':
redirect(site_url('de/'));
break;
default:
redirect(site_url('en/'));
}

And make sure you have load url helper

$this->load->helper('url');

Also remove return from redirect

Saty
  • 22,443
  • 7
  • 33
  • 51