Language detection and appropriate redirects

The first two medium cake applications I wrote was immediately multilingual. To check whether there is a language in the url, cookie set and/or AJAX request is a matter of priorities. What will be the steps to set the current language?

Here is my app_controller.php language handler. Put this function call in the app_controllers beforeFilter() method.

// sets the app language based on session, cookie or params
function _languageHandling() {
    // 1. check if cookie lang exitst
    $cookieVal = $this->Cookie->read('MARKTck');
    if ($cookieVal) {
        $this->lang = $cookieVal;
    }

    // 2, url from params
    if (!empty($this->params['lang'])) {
        if (in_array($this->params['lang'], $this->available_lang)) {
            $this->lang = $this->params['lang'];
        }
    }        

    // 3. check user language from session
    if ($this->Session->check('Auth.User') && $this->Auth->user('lang')) {
        $this->lang = $this->Auth->user('lang');
    }

    // redirects to lang if not AJAX call and lang is in URL
    if (!$this->RequestHandler->isAjax()) {
        // if not the same lang in the url get the remaining part of the url
        if (empty($this->params['url']['url']) || substr($this->params['url']['url'],0,2) != $this->lang) {
            $request_url = (!empty($this->params['url']['url']) ? mb_substr($this->params['url']['url'], 2, mb_strlen($this->params['url']['url'])) : '');
            if (empty($request_url)) {
                $request_url = '/';
            }
            $this->redirect("/" . $this->lang . $request_url);
        }        

        // save lang to cookie
        if ($cookieVal) {
            if ($this->lang != $cookieVal) {
                $this->Cookie->write('MARKTck', $this->lang);
            }
        } else {
            $this->Cookie->write('MARKTck', $this->lang, true, Configure::read('Markt.loginCookieLife'), '/');
        }
    }
    // $this->param['lang'] = $this->lang;
    setlocale(LC_ALL, $this->lang.'_'.up($this->lang));

    return $this->lang;
}

So I’ve made 2 steps to detect the language. Firts it checks if there is a cookie set with the language. Maybe he hasn’t been back to the site for over a month and we want still detect what language he was reading the site in.

But if he is coming with another language in url e.g. /en/posts/view/1 instead of the cookies lang ‘nl’ then this will have more prio over the cookie.

And last but not least… If the user is logged in his prefered language is more important then anything else. I had some question here. F.e. if I as admin want to test the english language site but my prefered language is dutch then I would never see it…. Hmmm Maybe I will change this in the future.

What do you think? What do you have to detect language of the user?

2 Responses to “Language detection and appropriate redirects”

  1. Tajinder Singh Says:

    Hi!

    I am working on a multilingual site in CakePHP.
    Session is being used for storing the language iso2 code and translations are saved in the MySQL tables.

    Here the language is changing properly and content depicts it. Now the problem is that how the search engines will know about the language and how can I specify the code in url.

    Please guide with your experience.

    Thanks!
    Take Care!

  2. Tajinder Singh Says:

    Hi!

    Sorry1 Forgot to mention that the languages are dynamic in the sense that administrator can add remove languages from the admin panel

    Thanks!