I was just doing this. Have a look at Jason Chow's p28n article if you
haven't already. It's not perfectly complete (it wasn't for me) but
it's a great start. Here's some of what I've recently done with it.

http://bakery.cakephp.org/articles/view/p28n-the-top-to-bottom-persistent-internationalization-tutorial


class P28nController extends AppController {
    var $name = 'P28n';
    var $uses = array();
    var $components = array('P28n');

    function change($lang = null) {
        $this->P28n->change($lang);

        $this->redirect($this->referer(null, true));
    }

    function shuntRequest() {
        $this->P28n->change($this->params['lang']);

        $args = func_get_args();
        $this->redirect('/' . implode('/', $args));
    }
}


My routes.php includes the following:

Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));

Router::connect('/en/*', array(
    'controller' => 'p28n',
    'action' => 'shuntRequest',
    'lang' => 'en'
));
Router::connect('/fr/*', array(
    'controller' => 'p28n',
    'action' => 'shuntRequest',
    'lang' => 'fr'
));
Router::connect('/es/*', array(
    'controller' => 'p28n',
    'action' => 'shuntRequest',
    'lang' => 'es'
));

I could also collapse the last 3 into:

Router::connect('/:lang/*', array(
    'controller' => 'p28n',
    'action' => 'shuntRequest',
    'lang' => '[a-z]{2}'
));

So, any two-character leader to a URL would be treated as a language code.

The P28NController passes the $lang to the P28NComponent, which tells
the rest of your app what language to be using, then redirects to the
rest of the URL.

Or, if the URL is like "/lang/en" it will just change the lang in the
session and redirect back to the current page.

class P28nComponent extends Object
{
    var $components = array('Session', 'Cookie');
        var $controller;

    function startup(&$controller)
        {
                $this->controller = $controller;
                
        if (!$this->Session->check('Config.language'))
                {
            $this->change(($this->Cookie->read('lang') ?
$this->Cookie->read('lang') : DEFAULT_LANGUAGE));
        }
    }

        function init(&$controller)
        {
                $this->controller = $controller;
        }
        
    function change($lang = null)
        {
        if (!empty($lang))
                {
            $this->Session->write('Config.language', $lang);
            $this->Cookie->write('lang', $lang, false, '+350 day');
        }
    }
}

Make sure to specify a default language in bootstrap.php:

define('DEFAULT_LANGUAGE', 'en');


My app requires access to the two-letter code in various places
because I have database tables like:

CREATE TABLE regions (
  id serial NOT NULL PRIMARY KEY,
  name_en VARCHAR(24) NOT NULL,
  name_fr VARCHAR(24) NOT NULL,
  name_es VARCHAR(24) NOT NULL
);

I'm also using the i18n tables but the above format is all legacy
stuff that works well enough for me (and I don't forsee adding any
other languages to the site).

Some of my AppController:

class AppController extends Controller
{
        var $components = array('p28n', 'Session');

        var $lang;
        
        function __construct()
        {
                parent::__construct();
                $this->setLang(DEFAULT_LANGUAGE);
        }

        function beforeFilter()
        {
                $this->setLang($this->Session->read('Config.language'));
        }

        /**
         * make language code available to all controllers and views
         */
        function setLang($lang)
        {
                $this->lang = $lang;
                $this->set('lang', $lang);
        }

        function appendLang($str)
        {
                return $str . $this->lang;
        }
}

So, now I can do things like:

$this->Member->Region->displayField = $this->appendLang('name_');
$this->set('regions', $this->Member->Region->find('list'));

And my regions are ready for a select list in the correct language.


Now, if you want to have URLs in different laguages, like:

/artists/...
/artistes/...
/artistas/...

I'm very interested in what you come up with. My current plan is to
create routes for all of them and I can't say I'm pleased at the
prospect. I think I'll need to do something like:

Router::connect('/:artists/:display/:artist_slug', array('controller'
=> 'artists', 'action' => 'display'),
        array(
                'artists' => '[artists|artistes|artistas]',
                'display' => '[display|montrez|mostrar]',
                'artist_slug' => '[-_a-zA-Z]+'
        )
);

But I only just got all of the other stuff figured out last night and
so haven't tried this route yet. If there's a better way, I'm all
ears.


On Tue, Mar 25, 2008 at 4:47 AM, Jacob <[EMAIL PROTECTED]> wrote:
>
>  Hello all,
>
>  I need a multi language site with cakephp. Ok, this topic is discuss
>  commonly. But I need a friendly-url solution, something's like:
>    http://mywebsite.com/lang/controller/view
>
>  This is my solution:
>  file /app/webroot/index.php:
>  [code]
>  require CORE_PATH . 'cake' . DS . 'bootstrap.php';
>
>  // some code to parse extract language from $url
>  // in case $url = "/en/products/view/1"
>  // -->
>  // $lang = "en"
>  // $url1 = "/products/view/1";
>
>  $Dispatcher = new Dispatcher();
>  $Dispatcher->dispatch($url1);
>  // end of /app/webroot/index.php
>  [/code]
>
>  file /app/app_controller.php
>  [code]
>  function beforeFilter() {
>     global $lang;
>     $this->lang = $lang;
>  }
>
>  function beforeRender() {
>     $this->set('lang', $this->lang);
>  }
>  [/code]
>
>  It works correctly on ver 1.1.xxx, but I think it's not a standard
>  way. There is a comment in /app/webroot/index.php. So my solution may
>  down with a newer version.
>  [code]
>  ///////////////////////////////
>  //DO NOT EDIT BELOW THIS LINE//
>  ///////////////////////////////
>  [/code]
>
>  Anyone has a more standard solution?
>
>  Thanks,
>  Jacob
>
>  >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to