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!

Magd Kudama
  • 3,229
  • 2
  • 21
  • 25

1 Answers1

2

If the localization configuration is properly working, you should be able to print the locale on Twig. With something like this:

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

If your Symfony version is less than 2.1 you must use app.session.locale instead of app.request.locale

Nevertheless is a good practice to always create custom 404 and any other error page you need:

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

The most powerfull way is using this method:

Replace the default exception controller twig.controller.exception:showAction with your own controller and handle it however you want (see exception_controller in the Twig reference). The default exception controller is registered as a service - the actual class is Symfony\Bundle\TwigBundle\Controller\ExceptionController.

This way you can even have different templates for each country.

Kind regards.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
jandro_es
  • 44
  • 2
  • Thanks for the answer! I'll try creating my own controller! – Magd Kudama Aug 11 '13 at 16:37
  • Yes! Now it works (customizing the controller that handles the exception). Nevertheless it's weird that the controller inside TwigBundle does not handle locales correctly. I'll take a look, probably it's my fault. – Magd Kudama Aug 11 '13 at 16:43
  • 1
    Do you have an example of your own controller? – Mondane Jul 28 '15 at 19:40