All the built-in i18n features in symfony are based on a parameter of the user session called the culture. The culture is the combination of the country and the language of the user, and it determines how the text and culture-dependent information are displayed. Since it is serialized in the user session, the culture is persistent between pages.

13.1.1. Setting the Default Culture

By default, the culture of new users is the default_culture. You can change this setting in the i18n.yml configuration file, as shown in Listing 13-1.

Listing 13-1 - Setting the Default Culture, in myapp/config/i18n.yml

  default_culture:     fr_FR

Note During development, you might be surprised that a culture change in the i18n.yml file doesn't change the current culture in the browser. That's because the session already has a culture from previous pages. If you want to see the application with the new default culture, you need to clear the domain cookies or restart your browser.

Keeping both the language and the country in the culture is necessary because you may have a different French translation for users from France, Belgium, or Canada, and a different Spanish content for users from Spain or Mexico. The language is coded in two lowercase characters, according to the ISO 639-1 standard (for instance, en for English). The country is coded in two uppercase characters, according to the ISO 3166-1 standard (for instance, GB for Great Britain).

13.1.2. Changing the Culture for a User

The user culture can be changed during the browsing session--for instance, when a user decides to switch from the English version to the French version of the application, or when a user logs in to the application and uses the language stored in his preferences. That's why the sfUser class offers getter and setter methods for the user culture. Listing 13-2 shows how to use these methods in an action.

Listing 13-2 - Setting and Retrieving the Culture in an Action

// Culture setter

// Culture getter
$culture = $this->getUser()->getCulture();
 => en_US

13.1.3. Determining the Culture Automatically

In many applications, the user culture is defined during the first request, based on the browser preferences. Users can define a list of accepted languages in their browser, and this data is sent to the server with each request, in the Accept-Language HTTP header. You can retrieve it in symfony through the sfRequest object. For instance, to get the list of preferred languages of a user in an action, type this:

$languages = $this->getRequest()->getLanguages();

The HTTP header is a string, but symfony automatically parses it and converts it into an array. So the preferred language of the user is accessible with $languages[0] in the preceding example.

It can be useful to automatically set the user culture to the preferred browser languages in a site home page or in a filter for all pages.

Caution The Accept-Language HTTP header is not very reliable information, since users rarely know how to modify it in their browser. Most of the time, the preferred browser language is the language of the interface, and browsers are not available in all languages. If you decide to set the culture automatically according to the browser preferred language, make sure you provide a way for the user to choose an alternate language.