Magd Kudama
Magd Kudama

Reputation: 3469

Error pages Symfony2 with locale

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!

Upvotes: 3

Views: 4215

Answers (1)

jandro_es
jandro_es

Reputation: 44

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.

Upvotes: 2

Related Questions