Last Updated: September 09, 2019
·
15.38K
· emgiezet

Symfony2 - The translation miracle

Many times when project is almost ready customer brings you a change request and say:

"We forget about language versions so there will be 4 of them for the entire site"

Budget is burned. It's late at night. You need a miracle.

The miracle tutorial begins here:

Install:
http://jmsyst.com/bundles/JMSI18nRoutingBundle

and

http://jmsyst.com/bundles/JMSTranslationBundle

Simply add those two to your composer.json file:

#composer.json
"require": {
    "jms/i18n-routing-bundle": "dev-master",
    "jms/translation-bundle": "1.1.*"
}

run the:

$php composer.phar update

Enable the bunldes in your AppKernel.php

$bundles = array (
    ...
    new JMS\I18nRoutingBundle\JMSI18nRoutingBundle(),
    new JMS\TranslationBundle\JMSTranslationBundle(),
);

Edit your config.yml:

jms_i18n_routing:
    default_locale: pl
    locales: [de, en, pl]
    strategy: prefix_except_default

Poof!

So right now you got 3 languages enabled on your site. Check it just by entiering your project with acmedemoapp.com/en prefix :)

look at the:

php app/console router:debug

Poof!

So many localized routes! YAY!

Ok but i still got only one messages translation.

Extract string to translations from your code!

ekhm... I can't gettext dude.

Poof!

Use the symfony command luke!
~ Master Yoda

Just like that:

$php app/console translation:extract pl --bundle=AcmeDemoBundle --enable-extractor=jms_i18n_routing --output-format=yml --exclude-dir=Tests

It don't go I'm using behat and throws an error.

add this:

--exclude-dir=Features

Repeat this for all your bundles.

Poof!

You can send the translations file to the customer. They will take care of the rest!

10 Responses
Add your response

but do not understand where I can then enter the translations

over 1 year ago ·

@ramondevesag You got all the translations exported per bundle to files in Resources/translations
in one of selected formats( xliff, gettext, yaml )

over 1 year ago ·

but if for example I introduce the data using a form to a database and then I collect the data from the database? For I understand that the file is not static?

over 1 year ago ·

Hello! Great tutorial, I've followed it and now I have the translation files of my routes, the problem is I'm only getting the language prefix in all the routes like:

  • en/hello
  • es/hello

What I want is:

  • en/hello
  • es/hola

I've changed the generated translation files, but doesn't seems to work, any help?

Thanks a lot!

over 1 year ago ·

@polsola did you cleared the cache?

over 1 year ago ·

Did you also manage the security component (login check route path) for logging in user ?

over 1 year ago ·

@ardianys: You can give to the security paths the route names. So for ex:

security:
    firewalls:
        main:
            switch_user:        false
            context:            user
            pattern:            .*
            form_login:
                provider:       fos_userbundle
                login_path:     fos_user_security_login
                use_forward:    false
                check_path:     fos_user_security_check
                failure_path:   null
            logout:             true
            anonymous:          true
over 1 year ago ·

Thank you Max, you saved my day!

over 1 year ago ·

@gpaton you're welcome!

over 1 year ago ·