2

I come here with this weird issue around Symfony2 translations since I don't know what else to do. As title says "translations works if a user is logged in, otherwise doesn't. Here is what I've done:

  1. Set default locale and translator at config.yml:

    framework:
        translator:      { fallback: "%locale%" }
        default_locale:  "%locale%"
    
  2. Set locale value at parameters.yml

    locale: es
    
  3. Set language at base template:

    <html lang="{{ app.request.locale }}"> 
    
  4. Check locale after page loads in Twig template where issue is happening:

    {{ app.request.locale }} // returns "es" it's right
    
  5. Clear the cache several/many/manyyyyyyyyyyy times:

    1st approach (didn't work):

    php app/console cache:clear
    php app/console cache:warmup
    

    2nd approach (didn't work):

    rm -rf /app/cache
    

    3th approach (didn't work): cd to app/cache and select each file one by one (using mc from Linux) and remove all them including directories by pressing F8

  6. Clear browser cache and test in Firefox/Chrome

I've checked the app/cache dir under translations directory and there is a file catalogue.es.php which contains all the translated strings so translation is working.

My translation are defined in a messages.es.yml file at AppBundle/Resources/translations and this is an example of the content:

registro:
    natural:
        panelTitulo: Datos del Usuario
    columnas:
        tipo_usuario: Tipo de Usuario
campos:
    tipoTramite: Tipo de Trámite

Then in my view this is how I access those translations:

{{'registro.natural.panelTitulo'|trans}}
{{'registro.columnas.tipo_usuario'|trans}}

But this, when user is not logged in, does not work, but, if I logged in and try this translation instead:

{{'campos.tipoTramite'|trans}}
{{ 'registro.columnas.tipo_usuario'|trans }}

and both works (I'm using FOSUserBundle for User management but translations doesn't belongs to any domain, just in the directory I mention earlier) so I'm complete lost since I don't know what else to do. Can any give me some advice around this? Translation only work if user has logged in in a application? I'm doing something wrong? How is that possible?

Extra information

This is the relevant part of my composer.json file those are the bundles I'm using on my application right now, any one problematic?

"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.5.*",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0",
    "sensio/framework-extra-bundle": "3.0.*@dev",
    "friendsofsymfony/user-bundle": "~2.0@dev",
    "friendsofsymfony/jsrouting-bundle": "2.0.*@dev",
    "friendsofsymfony/rest-bundle": "1.5.*@dev",
    "jms/serializer-bundle": "0.13.*@dev",
    "jms/di-extra-bundle": "1.4.*@dev",
    "jms/security-extra-bundle": "dev-master",
    "knplabs/knp-paginator-bundle": "2.4.*@dev",
    "knplabs/knp-menu": "2.0.*@dev",
    "knplabs/knp-menu-bundle": "2.0.*@dev",
    "stof/doctrine-extensions-bundle": "1.2.*@dev",
    "misd/phone-number-bundle": "~1.0",
    "raulfraile/ladybug-bundle": "~1.0",
    "h4cc/alice-fixtures-bundle": "dev-master",
    "oneup/uploader-bundle": "dev-master",
    "willdurand/js-translation-bundle": "2.1.*@dev",
    "vich/uploader-bundle": "1.0.*@dev"
}
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Can you show a little bit more of the context of the template file? – Wouter J Nov 08 '14 at 09:41
  • @WouterJ what you mean with "a little bit more of the context of that template"? Can you clear what you need in order to give you the info? – ReynierPM Nov 08 '14 at 13:31
  • @WouterJ take a look to [this issue](https://github.com/symfony/symfony/issues/12432) it's better explained – ReynierPM Nov 17 '14 at 21:45
  • Have you tried to disable FOSUserBundle? Maybe it's some kind of conflict... although I can't really imagine that. – Markus Kottländer Nov 17 '14 at 22:23
  • @MarkusKottländer how is supposed that my application will works with FOSUserBundle disabled? That's part of it and users should be logged in for do things on it so I don't think that's the problem could be something else – ReynierPM Nov 17 '14 at 22:25
  • Of course you need FOS... ^^ but i just remembered that they had a translation issue some time ago. And I can't really imagine that your problem has something to do with it, but just to make things clear. – Markus Kottländer Nov 17 '14 at 22:28
  • I went to the trouble of creating a fresh Symfony2 install, added your translation file, set the fallback locale to `es`, and threw your Twig example for both the "secured area" and the unsecured area of the Hello World AcmeDemoBundle apps. With both the user logged in and no user logged in (anonymous), the app correctly translated both samples. See [here](http://i.imgur.com/zpqyNAG.png) and [here](http://i.imgur.com/oPhbzOz.png). I'm not sure how else I can reproduce your problem except for adding in the rest of your bundles.. this will likely take more detective work on your part. – sjagr Nov 17 '14 at 22:29
  • Can you try `{{'registro.natural.panelTitulo'|trans({}, 'AppBundle') }}`? Obviously clear all your caches again when trying it. – sjagr Nov 17 '14 at 22:44
  • 1
    Pardon, that won't work - try `messages` instead of `AppBundle` and do a search for files named `messages.es.*` in your entire project folder. – sjagr Nov 17 '14 at 22:51
  • Rename your `messages.es.yml` file to something else and use this as the domain: `{{ 'foo.bar'|trans({}, 'messages_renamed') }}` – Markus Kottländer Nov 17 '14 at 22:52

1 Answers1

3

messages is a fairly common keyword for a translation domain and I suspect that conflicting bundles are overriding your translation file in certain scenarios. To fix this:

  1. Rename your messages.es.yml file to AppBundle.es.yml
  2. Use this format {{ 'registro.natural.panelTitulo'|trans({}, 'AppBundle') }} for all usage of your bundle translations, especially when working with Twig files outside of AppBundle
  3. Clear your caches
sjagr
  • 15,983
  • 5
  • 40
  • 67
  • Thanks @sjagr now it works seems like that was the problems, I can give you the awards right now but as soon as I can I would, thanks for your help – ReynierPM Nov 18 '14 at 01:37
  • @ReynierPM It was a long-shot! I'm glad it worked! Don't forget to close your issue on GitHub! – sjagr Nov 18 '14 at 01:39
  • I have a additional question related, perhaps, to this translation issue, since I'm working with FOSUserBundle I have this in the template where issue was: `{% trans_default_domain 'FOSUserBundle' %}` what this code does? Could be this which are causing the issue in first? – ReynierPM Nov 18 '14 at 02:18
  • 1
    @ReynierPM Potentially! Whatever the `trans_default_domain` is set to is what is called in the `trans` filter! So in essence, if that was set to `FOSUserBundle`, it was as if you were using `{{ 'registro.natural.panelTitulo'|trans({}, 'FOSUserBundle') }}`. By explicitly defining `AppBundle` in the `trans` filter, it solves this problem as well :) you can leave the `trans_default_domain` be or else you may see other FOSUserBundle translations break. – sjagr Nov 18 '14 at 02:20
  • any advice on [this](http://stackoverflow.com/questions/26987149/bazingajstranslationbundle-does-not-translate-strings) topic? – ReynierPM Nov 18 '14 at 05:22