1

I'm building a multilingual site under Codeigniter 3.0 and I'd like to have this behavior:

The default language of the site is 'en'. When user visits site (/), it gets browser's Accept-language and stores it in session var. Then I check if language is 'en' or not. If not, it redirects to mysite.com/lang. Case language is not 'en', do nothing so it keeps mysite.com/

The problem is that CI takes /lang as a controller.

I edited routes.php as following:

$route['es'] = '/';
$route['en'] = '/';
$route['de'] = '/';

But now I'm on a "too many redirects" issue as routes.php redirects to / when coming from /language and in my controller redirects to /language

My controller:

class Checklanguage  {

        public $CI;

        /**
         * Constructor.
         */
        public function __construct()
        {
                if (!isset($this->CI))
                {
                        $this->CI =& get_instance();
                }
                $this->CI->load->library('session');
        }

        public function redirect_if_not_default()

        {
                /* LANGUAGES SECTION */
                $browserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
                // if browser has no language, we use 'en'
                if (strlen($browserLang) != 2) $browserLang = 'en';

                // retrieve session lang (maybe NULL)
                $sessionLang = $this->CI->session->userdata('lang');

                // determine which language use
                // if user has not set any language, we use browser lang
                if(!isset($sessionLang)) $preferredLang = $browserLang;
                // if user has changed language, we use session language
                if (strlen($sessionLang) == 2) {
                        $preferredLang = $sessionLang;
                }

                // redirect if language is not 'en'
                if ($preferredLang != 'en') {
                        header('Location: /'.$preferredLang);
                }

        }
}

How can I solve this? Thanks

F.Matas
  • 33
  • 4

1 Answers1

0

Try:

if ($preferredLang != 'en') {
  redirect(base_url()."/$preferredLang");
}

Addition:

if you are using CI language class for that, all you need to setup a language file inside application/language and then load that language in controller.

CI documentation

Imran Abdur Rahim
  • 407
  • 1
  • 10
  • 21
  • It causes the same problem – F.Matas Sep 18 '17 at 08:52
  • Are you using Apache rewrite rule (example: to remove index.php from url ) Secondly,are these language directories in different setup as you mentioned CI takes these as a controller? – Imran Abdur Rahim Sep 18 '17 at 10:15
  • Yes, I remove index.php from .htaccess and I don't have language folders. I don't have folders for different languages. I have only one CI installation and it uses session vars to deal with languages. All works fine except this "url issue". If I could do something to disable "/en" calling that controller (en, that obv doesn't exist) would be fine... – F.Matas Sep 18 '17 at 10:30
  • Sorry if i have not understood your problem properly. according to `CI` Documentation `https://www.codeigniter.com/userguide3/libraries/language.html` You dont need to redirect, just load the language file. You may check this answer too: `https://stackoverflow.com/questions/1328420/the-best-way-to-make-codeigniter-website-multi-language-calling-from-lang-array` – Imran Abdur Rahim Sep 18 '17 at 11:09
  • If you are using only `CL` class for language, then all you need to setup language file in `application/language` and then load this language file. – Imran Abdur Rahim Sep 18 '17 at 11:11