Weird issue, translations works if logged in, if not then didn't work in Symfony 2.5.6

我的梦境 提交于 2019-12-24 02:49:05

问题


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"
}

回答1:


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


来源:https://stackoverflow.com/questions/26814240/weird-issue-translations-works-if-logged-in-if-not-then-didnt-work-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!