symfony链接以更改语言并停留在页面上 [英] symfony link to change language and stay on the page

查看:69
本文介绍了symfony链接以更改语言并停留在页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个链接,该链接将在布局中更改语言.因此它应该适用于许多路线.

I would like to make a link the will be in the layout to change the language. So it should work for many routes.

例如 我在/en/myModule

for example i'm on the page /en/myModule

,链接应指向 /de/myModule /fr/myModule

and the links should point to /de/myModule /fr/myModule

我在这里找到了解决方案:http://oldforum.symfony-project.org/index.php/m/70452/

I found a solution here :http://oldforum.symfony-project.org/index.php/m/70452/

<?php echo link_to(
  'Germany', 
  '@default?' . http_build_query(array(
    'sf_culture' => 'de', 
    'module' => $sf_request->getParameter('module'), 
    'action' => $sf_request->getParameter('action'))
  ), null, '&')) ?>

问题是我需要默认路由,但我不想拥有它.

Problem is that I need a default route, and I don't want to have it.

我需要什么解决方案吗?

Is there any solution for what I need ?

推荐答案

routing:
  user_switch_culture:
   url: /culture-change/:language
   param: { module: user, changeCulture }

在布局模板中:

<?php echo link_to(image_tag("flags/gb.gif"), "user_switch_culture", array("language"=>"en", "redirect"=>$sf_request->getUri())) ?>

链接将生成:

http://example.com/culture-change/fr?redirect=http//example.com/fr/control-panel

在您的行动中:

public function executeChangeCulture(sfWebRequest $request)
{
    $oldCulture = $this->getUser()->getCulture();
    $newCulture = $request->getParameter("language");
    $this->getUser()->setCulture($newCulture);
    return $this->redirect(str_replace('/'.$oldCulture.'/', '/'.$newCulture.'/', $request->getParameter("redirect")));
}

从头顶上移开.应该工作...

Just off the top off my head. should work...

不好:应该对重定向进行一些过滤,以确保它是正确的域名等.

Not great: Should do some filter on the redirect to make sure it's the correct domain name etc.

这篇关于symfony链接以更改语言并停留在页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆