Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: default locale not applied in translation

I want to translate my website with Symfony. I activated the translator in my configuration.

# app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

framework:
    #esi:             ~
    translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  %locale%
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
    fragments:       ~
    http_method_override: true


# app/config/parameters.yml
# This file is auto-generated during the composer install
parameters:
    ...
    locale: fr
    secret: MySecretToken

My translation files are located in app/config/Resources/messages.en.yml and app/config/Resources/messages.fr.yml .

But when I do this in my controller:

$translator = $this->get('translator');
var_dump($translator->trans('hello'));
exit();

It still displays "hello" instead of "Bonjour". I checked my PHP locale with \Locale::getDefault(), it's "en", and my Symfony locale is "fr" (obtained by a getLocale() on the translator object).

It works when I set manually the locale in the beginning of my action, but I've a lot of actions, and I would like to set the default locale for the whole website. I thought the config.yml could do that for me, but it seems that it's not the case... Do you see where my problem come from ? Thx !

like image 741
maxime Avatar asked Mar 20 '23 21:03

maxime


1 Answers

Try to clear app cache in app/cache dir. It's a very important for first time.

Also you must to add locale in your router

like image 193
Victor Bocharsky Avatar answered Apr 02 '23 22:04

Victor Bocharsky