0
votes

I'm translating my Symfony2 website, and it seems to work fine except for the layout... My template extends layout.html.twig, which contains a translated menu. When I'm in french, everything works fine (this is my default locale), but when I switch to english, the menu stays in french and the rest of the page (contained in my template) translates in english. Does someone have an idea of what's going on ?

Template (services.html.twig)

{% extends "GslsAppBundle::layout.html.twig" %}

Layout (layout.html.twig)

<div id="menu">
        <a href="{{ path('gsls_app_homepage') }}" class="accueil">{{ "menu.accueil"|trans|raw }}</a> |
        <a href="{{ path('gsls_app_services') }}" class="services">{{ "menu.services"|trans|raw }}</a> |
        <a href="#" class="reservation">{{ "menu.reservation"|trans }}</a> |
        <a href="#" class="contact">{{ "menu.contact"|trans }}</a>
    </div>

messages.fr.yml

menu:
    accueil: Accueil
    services: Services
    reservation: Réservation
    contact: Contactez-nous

messages.en.yml

menu:
    accueil: Home
    services: Services
    reservation: "Book online"
    contact: "Contact us"

Thank you !

1
How do you define the users locale? Try {{ dump(app.request.locale) }} in your template to see which locale is present. - SirDerpington
I already did that, and even in my layout it dump "en" ... But it translate in french :/ I define my locales in my routes - Julie Laporte
If you've just created messages.en.yml file, you have to clear cache in order for Symfony to discover it. - Igor Pantović
Can't the error simply be the quotation marks around Book onlineand Contact us? I know this is maybe trivial but yeah... - SirDerpington
No, i put the quotation marks thinking that it was actually the problem ;) They were not there before and I had the same error :( - Julie Laporte

1 Answers

0
votes

I could use one of the translations, but not the other and didn't know why. If you have troubles with translations also, read this.

First, standard checklist:

  • Make sure you enabled and configured translator.
  • Make sure translation is in proper place and follows proper naming convenction ( domain(messages by default).lang_code.file_format ).
  • Clear cache using php app/console cache:clear command.
  • Try to manually call $this->getRequest()->setLocale('en'); in Controller, also you may try to use $this->get('translator')->trans('Some message'); directly in your Controller.
  • If it still doesn't work, make sure BOM isn't in your translated file. That was my case.

Watch out for BOM in the translated file. The translator who translates the yml file used UTF8 which is OK, but editor he used leaved BOM at the beginning of the file. This is dangerous probably because of PHP's UTF8 BOM bug as it adds few invisible characters to first section of your file.

Btw, debugging your translations may be very helpful, too.

Btw2, sorry for sending this answer twice, but I cannot comment and link to original question yet :)