2

I am having a strange problem with translation in Symfony 2.

This is my config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration

framework:
    #esi:             ~
    translator: { fallbacks: ['%locale%'] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    #serializer:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    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

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        mapping_types:
          enum: string
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: AppBundle\Entity\User
    profile:
        form:
            type: app_user_profile

parameters.yml

...
locale: ru

And, of course, I have messages.en.yml and messages.ru.yml.

Even when locale is set to 'ru' it displays English text, and $request->getLocale() is always returning 'en'.

What could be the problem and where is it overset to 'en' if not in configs?

Eiko
  • 25,601
  • 15
  • 56
  • 71
Tigran
  • 633
  • 4
  • 17
  • 26
  • 1
    Possible duplicate of [Symfony 2.1 set locale](http://stackoverflow.com/questions/14276756/symfony-2-1-set-locale) – Shawn Jan 07 '16 at 17:13
  • do you clear the cache after the config.yml or parameter.yml be edited? – abdiel Jan 07 '16 at 17:14

4 Answers4

5

The locale parameter in the config.yml file is just a fallback in case a translation for the requested language is not available.

Calling $request->getLocale() will return the language configuration of the browser from which the HTTP-Request was sent.

If you want the translation to be forced into another language take a look at this answer: https://stackoverflow.com/a/14331838/1173391

Community
  • 1
  • 1
Nickolaus
  • 4,785
  • 4
  • 38
  • 60
1

You need to create a twig extension that allows you to replace ru with en in your path route (and vice versa) :

{# yourProject/app_dev.php/ru #}
<a href="{{ localize_route('ru') }}"></a>

Look at this tutorial

http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route

Houssem ZITOUN
  • 644
  • 1
  • 8
  • 23
1

put in your routing.yml a prefix with the locale like this:

routing.yml

app:    
   resource: "@AppBundle/Controller/"    
   type:     annotation    
   prefix:   /{_locale}    
   requirements:    
       _locale: "%app.locales%"

config.yml

parameters:
    locale: en
    app.locales: en|ru
Roger Guasch
  • 412
  • 5
  • 19
0

Did you add:

{% trans_default_domain "message" %}

on top of your twig template file where the translation keys exist and did you clear cache?

Lord Zed
  • 750
  • 7
  • 28