Symfony

Page 186

Chapter 15

Translations The term "internationalization" (often abbreviated i18n1) refers to the process of abstracting strings and other locale-specific pieces out of your application and into a layer where they can be translated and converted based on the user's locale (i.e. language and country). For text, this means wrapping each with a function capable of translating the text (or "message") into the language of the user: Listing 15-1

// text will *always* print out in English echo 'Hello World'; // text can be translated into the end-user's language or default to English echo $translator->trans('Hello World');

The term locale refers roughly to the user's language and country. It can be any string that your application uses to manage translations and other format differences (e.g. currency format). We recommended the ISO639-12 language code, an underscore (_), then the ISO3166 Alpha-23 country code (e.g. fr_FR for French/France).

In this chapter, we'll learn how to prepare an application to support multiple locales and then how to create translations for multiple locales. Overall, the process has several common steps: 1. Enable and configure Symfony's Translation component; 2. Abstract strings (i.e. "messages") by wrapping them in calls to the Translator; 3. Create translation resources for each supported locale that translate each message in the application; 4. Determine, set and manage the user's locale in the session.

Configuration Translations are handled by a Translator service that uses the user's locale to lookup and return translated messages. Before using it, enable the Translator in your configuration: 1. http://en.wikipedia.org/wiki/Internationalization_and_localization 2. http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 3. http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes

PDF brought to you by generated on June 20, 2012

Chapter 15: Translations | 186


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