duanci5913 2017-07-21 17:37
浏览 67
已采纳

Symfony - 如何切换和使用区域设置

what is the best practise how to work with locale in the twig templates:

  • how switch between languages (with language switcher in template) and how to keep an existing URL with just a change of language
  • how create URL with locale support

For example: I have 2 languages (ES and EN), default language is ES, for my home page I create 2 Route Anotations for / (for default language, in this case ES) and for /{_locale}/ (for other languages) in my Controller file.

And now I need to get locale parameter to my twig template to my URL, but only if I will not on my default language.

Manually rewrite URL works fine, but is there any easy way how add locale parameter to URL when I creating on my twig templates ?

The actual value of the variable locale can be passed to Controller, but is there any better way to got it in twig?

EDIT:

/**
 * @Route(
 *     "/{_locale}/branches", name="branches",
 *     requirements={"_locale":"%app_locales%"}
 *     )
 * 
 */
public function indexAction(Request $request) {
    return $this->render('branches/index.html.twig');
}

index.html.twig

<li class="{% if app.request.attributes.get('_route') starts with 'branches' %}active{% endif %}">
                    <a href="{{ path('branches') }}" class="">{{ 'header.menu.branches'|trans }}</a>
                </li>

I got

No route found for "GET /branches" i use this URL http://localhost:8080/en/branches (works OK) and http://localhost:8080/branches (ERROR) I must use something like this:

/**
 * @Route(
 *     "/branches/", name="branches_def",
 *     defaults={"_locale":"%locale%"},
 *     )
 * @Route(
 *     "/{_locale}/branches/", name="branches",
 *     requirements={"_locale":"%app_locales%"}
 *     )
 * 
 */
public function indexAction(Request $request) {
    return $this->render('branches/index.html.twig');
}

URL generating with path is OK, but if I delete locale parameter from my URL in my browser I got this error.

Thanks a lot for any usefull advice

  • 写回答

2条回答 默认 最新

  • dozr13344 2017-07-22 13:46
    关注
    1. uncomment row translator: { fallbacks: ['%locale%'] } in config.yml
    2. declare needed variables in the same file:

      parameters:
          locale: en
          app.locales: en|lv|ru
      
    3. declare your needed route in routing.yml like this:

      contact_us:
          path: /{_locale}/contact
          defaults:
              _controller: 'AppBundle:Default:contact'
              _locale: '%app.locales%'
      
    4. use {{ path('another_route') }} without parameters, in case another_route declared with _locale placeholder. Also for single text translation use {{ 'home.title'|trans }}.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀