向所有路由添加语言环境和要求 - Symfony2 [英] Add locale and requirements to all routes - Symfony2

查看:24
本文介绍了向所有路由添加语言环境和要求 - Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个不打算进行翻译的应用程序,但现在我决定添加此功能.问题是我所有的路线都是这样的:

I created an application which was not intended to have translations, but now I decided to add this feature. The problem is that all my routes look like this:

goodbye:
    pattern: /goodbye
    defaults: { _controller: AcmeBudgetTrackerBundle:Goodbye:goodbye }

我希望他们现在是这样的:

and I want them now to be like this:

goodbye:
    pattern: /goodbye/{_locale}
    defaults: { _controller: AcmeBudgetTrackerBundle:Goodbye:goodbye, _locale: en }
    requirements:
        _locale: en|bg

我真的必须这样做吗,有没有办法做更多的全局或自动操作,或者至少只添加一次要求,因为它们对所有网址都相同?非常感谢提前!

Do I really have to do this and is there a way to do more global or automatic, or at least to add requriements only once, because they are the same for all urls? Thank very much in advance!

推荐答案

使用 JMS18nRoutingBundle (文档)用于此目的.没有自定义加载器,没有编码......

Use JMS18nRoutingBundle (documentation) for this purpose. No custom loader, no coding ...

除了包的一些配置外,捆绑包能够为所有路由添加区域设置的前缀,而无需更改任何内容.这是让您入门的最快(也是我推荐的)解决方案.

The bundle is able to prefix all your routes with the locale without changing anything except some configuration for the bundle. It's the quickest ( and my recommended ) solution to get you started.

您甚至可以翻译不同地区的现有路线.

You can even translate existing routes for different locales.

可以在这篇 coderwall 帖子中找到简要介绍.

A quick introduction can be found in this coderwall post.

这篇关于向所有路由添加语言环境和要求 - Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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