开发者

Symfony switch language

I use symfony 1.4.11 .User can switch language , in my layout I have

<?php foreach (sfConfig::get('app_cultures_enabled') as $key => $lng): ?>
    <?php echo link_to_if($sf_user->getCulture() != $lng,
    "<img src=\"/images/flags/$lng.png\" alt=\"$lng\">", 
    preg_replace('/sf_culture=[a-z][a-z]/', 'sf_culture='.$lng, $sf_context->getRouting()->getCurrentInternalUri(true)))?>
  <?php endforeach ?>

So in each page I generate link, that I can switch language. But I开发者_StackOverflow中文版 have page with next rout:

page:
  url:   /:sf_culture/page/:slug
  param: { module: page, action: show}
  requirements: { sf_culture: (?:se|no|en), sf_method: GET }

And when I go to this page I have error, that :

The "/:sf_culture/page/:slug" route has some missing mandatory parameters (:slug).

I think that problem in

<?php echo link_to_if($sf_user->getCulture() != $lng,
        "<img src=\"/images/flags/$lng.png\" alt=\"$lng\">", 
        preg_replace('/sf_culture=[a-z][a-z]/', 'sf_culture='.$lng, $sf_context->getRouting()->getCurrentInternalUri(true)))?>

it can not generate link with slug, or..


If I'm reading this right, it looks like you're trying to mimick the slug variable using $sf_context->getRouting()->getCurrentInternalUri(true).

I've never done a language switch link this way but basically if that works, you need to pass it as a variable called "slug" to the routing. Somewhere in that magical link_to_if() function you need to have:

'&slug=' . $slug // the slug!

... given that slug is a named parameter that the routing rule is expecting.

UPDATE:

This is how I'd normally change language:

I'd create a separate action (e.g. changeLanguage) without a template, and then create links to it with the language as a parameter:

mysite.com/change-language/fr/ (?lang=fr)
mysite.com/change-language/de/ (?lang=de)
mysite.com/change-language/es/ (?lang=es)

In that action, I use $this->getUser()->setCulture($lang) to change language and anything else I want to do, and then use $this->redirect($request->getReferer()) to redirect the user back to the page they came from. If your app needs to use multi-language URLs, I would ensure that those get rendered on their own pages from a URL parameter (like the "slug"). And if you're doing this with SEO in mind, things also get a little more complicated because you should link directly to different language pages instead of using re-directs etc (to pass link juice).

Hope that helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜