0
votes

I followed this tutorial to create multilingual site. http://nuts-and-bolts-of-cakephp.com/2008/11/28/cakephp-url-based-language-switching-for-i18n-and-l10n-internationalization-and-localization/

However although I can get to the login page at example.com/en/users/login when i try to login, the login wouldn't go through. I get thrown at example.com/users/login without the /en/ and I don't know if this matters, but the password does not contain 4 letters/stars/dots like the password i tried, but about 40.

Also, I noticed that the example.com/en/users/logout function does work to log me out, but takes me to example.com/users/login instead of example.com/en/users/logout

I managed to get to the fact that the function that breaks it is the:

class AppHelper extends Helper {

    function url($url = null, $full = false) {
       if(!isset($url['language']) && isset($this->params['language'])) {
           $url['language'] = $this->params['language'];
       }

       return parent::url($url, $full);
    }

}

However, without it, no urls work at all in terms of the /en/ addition in the URL.

Can anyone help? I will provide any code needed.

4

4 Answers

1
votes

This was quite a journey to make it work. Some of the things I still don't get, but at least I know they work. If you make an improvement on this let me know, I'd like to be aware of it.

This tutorial in the question only gets you half way.

In addition, here are a few things that need to be moded to get this working:

Routes.php:

 /**
* LANGUAGES
*/
Router::connect( '/:language/:plugin/:controller/:action/*', array(), array('language' => '[a-z]{3}')); // PLUGIN FIX
Router::connect('/:language/:controller/:action/*', array('plugin' => null), array('language' => '[a-z]{3}'));
Router::connect('/:language', array('controller'=>'static', 'plugin'=>null), array('language' => '[a-z]{3}')); 

App_controller.php:

function beforeFilter(){
    $this->_setLanguage();
    $this->Auth->logoutRedirect = array( 'controller' => 'static', 'action' => 'index', 'language'=>$this->Session->read('Config.language'));
    $this->Auth->loginRedirect = array( 'controller' => 'static', 'action' => 'dashboard', 'language'=>$this->Session->read('Config.language'));
    $this->Auth->loginAction = array( 'controller'=>'users', 'action'=>'login', 'language'=>$this->Session->read('Config.language'));
}

function _setLanguage() {
    if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
        // If ... don't really get this. 
        $this->Session->write('Config.language', $this->Cookie->read('lang'));
    }
    else if (isset($this->params['language']) && ($this->params['language']
             !=  $this->Session->read('Config.language'))) {
            // Had a language set, but different from the one in the URL, set to URL

        $this->Session->write('Config.language', $this->params['language']);
        $this->Cookie->write('lang',$this->params['language'], false, '360 days');
    }else{
        // First time comer
        $this->Session->write('Config.language', Configure::read('Config.language' ));
        $this->Cookie->write('lang', Configure::read('Config.language' ), false, '360 days');
    }
}

function redirect( $url, $status = NULL, $exit = true ) {
    if (!isset($url['language']) && $this->Session->check('Config.language')) {
        $url['language'] = $this->Session->read('Config.language');
    }
    parent::redirect($url,$status,$exit);
}

app_helper.php:

   function url($url = null, $full = false) {
    if(!isset($url['language']) && isset($this->params['language'])) {
      $url['language'] = $this->params['language'];
    }else if (!isset($url['language']) &&  !isset($this->params['language'])){
        if($_COOKIE['lang']){
            $url['language'] = $_COOKIE['lang'];
        }else{
            $url['language']  =  Configure::read('Config.language');
        }
     }

    return parent::url($url, $full);
}

And this should be it. Hope that helps all of you lost souls with multilingual cakephp stuff pp

1
votes

I used the same resource to make cakephp multilingual, but today got stuck with that problem that by default login action loses language. When I found this post, the only thing that I need in addition to the original manual is the following code in controller_app.php beforeFilter:

$this->Auth->logoutRedirect = array( 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginRedirect = array( 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginAction = array( 'controller'=>'users', 'action'=>'login', 'language'=>$this->Session->read('Config.language'));
0
votes

I saw your other question about multilingual setting. I was going to say that just make the default language to be 'en'. But I'm uncertain how that would affect SEO, so I didn't say anything. But yeah, the way you are doing right now might interfere with many Cake automagic, like the Auth problem you are having. Here what I'd suggest: In app_controller:

function beforeFilter(){
    $this->Auth->loginAction = array(
       'controller' => 'users', 
       'action' => 'login' // insert the language option that you use here
     );
    $this->Auth->logoutRedirect = array(
       'controller' => 'users', 
       'action' => 'index'
    );// you can set where to redirect after logout here
}

I would advise not to redirect to /users/logout, unless you want to do something there. And you would need to change the logout function in users_controller too.

0
votes

One thing that helped me, (not perfect but it's a start)

is i created exceptions for the 'loginPage' (the login action for the auto component)

so the language of the url doesn't get populated when logging in.

this helped me as i wasn't able to login until i did this

i added this to the app_helper.php

>     //exceptions to allow to login
>     if (isset($url['action'])) {
>          if ($url['action'] == 'loginPage') $url['language'] = false;
>     }