Symfony

Page 191

Using Real or Keyword Messages This example illustrates the two different philosophies when creating messages to be translated: $t = $translator->trans('Symfony2 is great');

Listing 15-10

$t = $translator->trans('symfony2.great');

In the first method, messages are written in the language of the default locale (English in this case). That message is then used as the "id" when creating translations. In the second method, messages are actually "keywords" that convey the idea of the message. The keyword message is then used as the "id" for any translations. In this case, translations must be made for the default locale (i.e. to translate symfony2.great to Symfony2 is great). The second method is handy because the message key won't need to be changed in every translation file if we decide that the message should actually read "Symfony2 is really great" in the default locale. The choice of which method to use is entirely up to you, but the "keyword" format is often recommended. Additionally, the php and yaml file formats support nested ids to avoid repeating yourself if you use keywords instead of real text for your ids: symfony2: is: great: Symfony2 is great amazing: Symfony2 is amazing has: bundles: Symfony2 has bundles user: login: Login

Listing 15-11

The multiple levels are flattened into single id/translation pairs by adding a dot (.) between every level, therefore the above examples are equivalent to the following: symfony2.is.great: Symfony2 is great symfony2.is.amazing: Symfony2 is amazing symfony2.has.bundles: Symfony2 has bundles user.login: Login

Listing 15-12

Using Message Domains As we've seen, message files are organized into the different locales that they translate. The message files can also be organized further into "domains". When creating message files, the domain is the first portion of the filename. The default domain is messages. For example, suppose that, for organization, translations were split into three different domains: messages, admin and navigation. The French translation would have the following message files: • messages.fr.xliff • admin.fr.xliff • navigation.fr.xliff When translating strings that are not in the default domain (messages), you must specify the domain as the third argument of trans(): $this->get('translator')->trans('Symfony2 is great', array(), 'admin');

PDF brought to you by generated on June 20, 2012

Listing 15-13

Chapter 15: Translations | 191


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.