Configure Symfony to use gettext translation files

Please let our ADS show!

This sites offers only FREE software and it's supported by a few advertisement boxes (no intrusive popups).
Please:

  • disable your AdBlocker by adding CoolSoft website to whitelist
  • give the proper cookie consent
  • enable JavaScript for this website

This seconds wait is to let you update your browser configuration...

Ok, I've done the required changes... now show me your content!
!!! Please enable JavaScript !!!

Once finished my Symfony application, I'm now facing with the translation task.

Symfony comes with XLIFF as default translation provider; XLIFF is a good format, which manages correctly UTF-8 charsets, but its editor support is poor.
I tried some editors, most of them in Java, but none satisfied me (all of them are joung and primitives).

gettext() is a stable and well-known choice and, most of all, it has a great Eclipse integrated plugin.
Ok, let's try to configure Symfony to use gettext as translation provider.

Documentation on this argument is a bit confused and does not target version 1.2 of the framework, so this is my contribute.

Enable I18N framework in settings.yml:

#apps/<appName>/config/settings.yml
all:
  .settings:
    charset:                utf-8
    i18n:                   on
    standard_helpers:       [ ...your helpers..., I18N]

Now in documentation you'll read to create a i18n.yml file, and you'll also read that this is deprecated (with no further details on how to proceed).
The right way is to add this config params to your factories.yml file:

#apps/<appName>/config/factories.yml
all:
  i18n:
    class: sfI18N
    param:
      default_culture:  en_US
      source:           gettext

Then create language folders under apps/<appName>/:

 apps/<appName>/i18n/<lang>

Now all the i18n related Symfony tasks should use gettext() as translation provider, including i18n:extract. 

You're done, now choose your favorite .po file editor and start translate your application.

My suggestions are: Poedit as standalone editor and gted as Eclipse plugin.

Category: 

Comments

Hi!

Thanks for this information, it may be usefull to specify that the i18n config should be added in factories.yml

Thanks
Mick

You're right, I'll add filenames to code snippets right now!

Thanks
Claudio

This is great!  Like you said the free editors for gettext are much better and simpler than any I have found for xliff.

Hi,

I follow your steps but the translation don't work.

I enable i18n in settings.yml how you comment and modifi factories.yml also with "default_culture: en" (but I try with en_US also).

At the end I place the files messages.po and messages.mo in "apps//i18n/en/"
(but I try with en_US also)

What are wrong?

Thanks a lot and sorry for my bad english :p

Ok, solved... I used the gettext syntax _() instead of symfony syntax __().

I'd like to know how you do to generate the mo files from the po files ?

Are you using a gettext tool or does it exist something in symfony to do that ?

Thanks

Actually I use the gettext utils (on windows the exe is called msgfmt.exe).
I 'm not aware of a symfony tool to do this.

This symfony factories.yml configuration reference explains all user configuration options: http://www.symfonyreference.com/factories-yml

Thanks for sharing

If you need a localization tool to manage the translation of gettext files, or of XLIFF files, I suggest you have a look at the localization platform POEditor.

It has a simple interface that can be used to localize strings collaboratively and to crowdsource translations.

Hello! Wish you a Happy New Year with many goals achieved. To localize an app, I have used POEditor, a collaborative localization management platform with a very user-friendly interface, but before that, I converted yaml to po files with this free tool http://yml2po.com/. In case you need it, it's a good solution.

Add new comment