3

I'm trying to translate 404 pages on Symfony2, but it's not working. With 500 error pages it works great.

For example, when i try to access the page: /es/not-found it shows the 404 page, but when i access /en/not-found, it gives me the SAME page, although i have translated the error message on my messages-en.yml.

It seems it's always accessing the messages-es.yml file, because i've configured Symfony with this default locale (es).

If i print {{ app.request.locale }} on my Twig template, it doesn't give me anything.

I cleared the cache several times, yes :).

Thanks in advance!

4

1 回答 1

2

如果本地化配置正常工作,您应该能够在 Twig 上打印语言环境。像这样:

{% if app.request.locale == 'en' %}
    English 404 message
{% else %}
    Other 404 message
{% endif %}

如果你的 Symfony 版本低于 2.1,你必须使用 app.session.locale 而不是 app.request.locale

尽管如此,始终创建自定义 404和您需要的任何其他错误页面是一个好习惯:

http://symfony.com/doc/current/cookbook/controller/error_pages.html

最强大的方法是使用这种方法:

用您自己的控制器替换默认的异常控制器 twig.controller.exception:showAction 并根据需要处理它(请参阅 Twig 参考中的 exception_controller )。默认的异常控制器注册为服务——实际的类是 Symfony\Bundle\TwigBundle\Controller\ExceptionController。

这样,您甚至可以为每个国家/地区使用不同的模板。

亲切的问候。

于 2013-08-11T15:21:43.723 回答