问题
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:
Set default locale and translator at
config.yml
:framework: translator: { fallback: "%locale%" } default_locale: "%locale%"
Set locale value at
parameters.yml
locale: es
Set language at base template:
<html lang="{{ app.request.locale }}">
Check locale after page loads in Twig template where issue is happening:
{{ app.request.locale }} // returns "es" it's right
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 F8Clear 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:
- Rename your
messages.es.yml
file toAppBundle.es.yml
- Use this format
{{ 'registro.natural.panelTitulo'|trans({}, 'AppBundle') }}
for all usage of your bundle translations, especially when working with Twig files outside ofAppBundle
- Clear your caches
来源:https://stackoverflow.com/questions/26814240/weird-issue-translations-works-if-logged-in-if-not-then-didnt-work-in-symfony