CakePHP 2.x i18n route

前端 未结 2 1852
余生分开走
余生分开走 2021-01-26 00:13

There is some solution for using CakePHP route with params only when are not empty?

Now I code below, but I would like some better:

if(Configure::read(\'         


        
相关标签:
2条回答
  • 2021-01-26 01:08

    You need to use 2 routes but add the 'persist' option for your language based routes. Adding 'persist' will avoid having to specify 'language' key each time when generating urls.

    // for all languages without default language. 
    Router::connect(
        '/:lang/'. __('register', true),
        array(
            'controller' => 'users', 
            'action' => 'register'
        ), 
        array(
            'persist' => array('lang')
        )
    );
    
    // only for default language (en)
    Router::connect(
        '/'. __('register', true),
        array(
            'controller' => 'users', 
            'action' => 'register'
        )
    ); 
    

    You might also want to checkout CakeDC's I18n plugin.

    0 讨论(0)
  • 2021-01-26 01:17

    Ok, these things work better, but I still other problem.

    I set default language by Configure::write('Config.language'); to en in bootstrap.php

    Next i wrote shema for url like this:

    Router::connect('/:language/'. __('register', true), array('controller' => 'users', 'action' => 'register'), array('persist' => array('lang')));
    Router::connect('/'. __('register', true), array('controller' => 'users', 'action' => 'register')); 
    

    And when users change language by beforeFilter in AppController (set new Config.language) will content from static .po and db worsk perfeclty, but links not translated. Parametr :language works but magic function __() in Router:connect() not works.

    Because first loaded is bootstrap.php, next is router.php and last is AppController.php

    My question is, how to force router.php to translate links (__())?

    Sorry, but still learn english...

    0 讨论(0)
提交回复
热议问题