简体   繁体   中英

Symfony2 locale detection and translation

Here is what I'm stuck on. I'm working on Symfony2 and trying to make the translations working. But somehow, I can't make it work as it should. Here's what I got. In the config.yml

translator:      { fallback: de }
session:
        default_locale: en

In messages.en.yml I have this structure:

Please log in: Bitte melden Sie sich an

I also have similar files for messages.en_US.yml , messages.ru.yml , etc.

In the code I use $this->get('session')->setLocale('ru_RU'); to change the locale.

But the translation works only when the locale is set to "en_US" or "de_DE". In any other case it returns the key, not the value. I try the translation with this code

return new Response($this->get('translator')->trans('Please log in').' '.$this->get('session')->getLocale()); and it returns the locale I have set.

So what can cause this problem?

you should clear the cache by using the symfony comand

app/console cache:clear --no-debug

then eventually restore write permission on cache/ and log/ folders as described here

I found the answer myself. Just needed to clean up the cache for Symfony. Used console to do so. Changed directory to myProject/app and used this commands to clean the cache and logs:

sudo rm -R cache/
sudo rm -R logs/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM