CakePHP - how to translate current URL after changing language and reloading Routing table

I'm using different routing tables for every language, and I have wrote action which change language and redirects to the same page but in target language (and target url).

The main problem is that my action is way too complicated - how can I make it simple? It should change language and redirect to new url (in target language).

In short: We had random valid cake url in one language and we had to translate it to adequate url in another language.

My routing table:

if( 'en' == Configure::read('Config.language') ) {
    Router::connect('/help',   array('controller' => 'pages', 'action' => 'display', 'help') );
} else {
    Router::connect('/pomoc',   array('controller' => 'pages', 'action' => 'display', 'help') );

Action changing language:

function lang($lang) {

    // getting previous url table
    $url = $this->referer();
    $url = Router::parse($url);

    // changing language
    if( in_array($lang, Configure::read('Languages.valid') ) ) {
        $this->Session->write('Language', $lang);
        Configure::write('Config.language', $lang);

    // saving base params
    $requestInfo = array(Router::getParams(), Router::getPaths());
    // reload routing table
    // restore base params

    // fix for 'pass' params
    if(!empty($url['pass']) && is_array($url['pass'])) {
        $url = array_merge($url, $url['pass']);

About 'pass' key in url table:


after Router::parse(), parameter is extracted:

pass => array(
    0 => 'help'

and later return value from Router::url() look like that:


so I have to fix it by merging 'pass' value with whole array and removing key


I have my routes.php like this

Configure::write('Config.language', $_SESSION['lang']);
Router::connect(__('/help',true),   array('controller' => 'pages', 'action' => 'display', 'help') );

so I use the __() to translate the url. It seaches the translation in the po files. And in your lang() function, after changing the Session to the current lang, all you need to do is:


Hope this helps

I know it is too late to answer that, but for those who experience the similar needs:

In routes.php p1 identifies the static page (same for all languages), could be any string that uniquely identifies the page.

Router::connect('/:language/p1/:translation',   array(
    'controller' => 'pages',
    'action' => 'display',
    'options' => array('language' => '[a-zA-Z]{2}')
) );

In your view

$this->Html->link('Click me', array(
    'language'    => Configure::read('Config.language'),
    'controller'  => 'pages',
    'action'      => 'display',
    'translation' => __('help') // could be any string in fact

will generate link to /en/p1/help in English, else to /xx/p1/pomoc.

Injection of language parameter into every link could be done in AppHelper::url() instead of providing it in every link occurence.

If you want to redirect in controller:

    'language'    => Configure::read('Config.language'),
    'controller'  => 'pages',
    'action'      => 'display',
    'translation' => __('help')

Need Your Help

Activating Bottom AppBar from Code behind in WinRT

c# xaml windows-8 windows-runtime winrt-xaml

Does anyone know of a way to activate the bottom AppBar from code-behind when we select a tile? I found this, but I could not find a C# equivalent. I've browsed around a bunch of samples but no luck

How do I validate these phone numbers with Regular Expressions?

java regex

Given the following patterns, how do I write a Regular Expression to validate these phone numbers?