开发者

CakePHP i18n/i10n Routing

开发者 https://www.devze.com 2023-02-07 03:29 出处:网络
Update: I\'m using a shared CakePHP lib across several apps (with all of them living in subdomains).They\'re not all nested the same; so, for example, on the filesystem I might have:

Update:

I'm using a shared CakePHP lib across several apps (with all of them living in subdomains). They're not all nested the same; so, for example, on the filesystem I might have:

/foo
  .htaccess
  /1.0 (app)
  /1.1 (app)
  /1.2 (app)
  ...
/bar
  .htaccess
  /1.0 (app)
  /1.1 (app)
  /1.2 (app)
  ...

Where the .htaccess in each just defines which app is the default that requests are routed to within that subfolder, while all of them are still available by a direct url (eg /foo/1.0/...).

It seems that if I'm using the direct URL (eg not the .htaccess-rewritten one) it recognizes the routes appropriately, but if the URL is rewritten by .htaccess, it doesn't; it will invoke AppError::error404() with the ouput at the end of the question. This doesn't seem right to me... if anyone has any insight, that would be awesome.


Hey guys,

I'm using CakePHP 1.3 and having a routing issue. The gist of the concept is this:

  • I want to use 'en_us' style i18n/i10n, not simply 'en'
  • I'm not using built-in i18n because of requirements to have some languages display pages with localized views (views/:i18n/...) and others to common views with calls to __()

So, I have routes set up like so:

$sI18nFormat = '/[a-z]{2}_[a-z]{2}/';
Router::connect('/:i18n/:controller/:action',
    array('controller' => ':controller', 'action' => ':action', 'i18n' => ':i18n'),
    array('i18n' => $sI18nFormat));
Router::connect('/:i18n/:controller',
    开发者_如何转开发array('controller' => ':controller', 'action' => 'index', 'i18n' => ':i18n'),
    array('i18n' => $sI18nFormat));
Router::connect('/:controller/:action',
    array('controller' => ':controller', 'action' => ':action', 'i18n' => 'en_us'),
    array('i18n' => $sI18nFormat));
Router::connect('/:controller',
    array('controller' => ':controller', 'action' => 'index', 'i18n' => 'en_us'),
    array('i18n' => $sI18nFormat)); 

where obviously the routes are mirrored except that one has a dynamic i18n parameter passed, and the other has a static one.

The problem comes with using certain values for :i18n - for example, en_us is okay, but fr_fr seems to have Cake looking for FrController (not FrFrController) - seemingly because it's trying to work some magic with a built-in i18n fr prefix.

As an example, here's what AppError::error404 is given:

Array
(
    [className] => FrController
    [webroot] => /path/to/webroot
    [url] => _fr
    [base] => /path/to/webroot
)

Is it possible to a) make Cake stop this so my routes work as expected, or b) tell Cake what format I want my i18n/i10n in so it doesn't try to do it its own way?

Any thoughts would be appreciated.


You should not (need to) delimit the Regex, and there should also be no need to populate the default parameter. Try this:

$sI18nFormat = '[a-z]{2}_[a-z]{2}';
Router::connect('/:i18n/:controller/:action', array(), array('i18n' => $sI18nFormat));

It's probably also failing because the URL does not match the full defined route. I.e., /fr_fr/foo will not match the above route, since it contains no :action. Try adding shorter variants as well:

Router::connect('/:i18n/:controller', array('action' => 'index'), array('i18n' => $sI18nFormat));
Router::connect('/:i18n', array('controller' => 'foo', 'action' => 'index'), array('i18n' => $sI18nFormat));


Have you looked at p28n? http://bakery.cakephp.org/articles/p0windah/2007/09/12/p28n-the-top-to-bottom-persistent-internationalization-tutorial

I found that it made the whole thing much less of a handful.

0

精彩评论

暂无评论...
验证码 换一张
取 消