Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
54
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

CHAPTER 19 LOCALIZATION AND TRANSLATION

Figure 19-18. If the current language is a right-to-left language, an additional style sheet will be included if the additional style sheet exists.

Language Negotiation

Drupal implements most of the common ways of determining a user’s language so that when multiple languages are enabled on a Drupal site, the user’s preferred language is used. In the following sections, we will assume that the French translation of Drupal has been installed as described in the previous section. The way that Drupal determines the language setting is configured at Configuration -> Languages under the Detection and Selection tab. The relevant user interface is shown in Figure 19-19. Let’s examine each of these options.

435

CHAPTER 19 LOCALIZATION AND TRANSLATION

Figure 19-19. The possible settings for language negotiation

Default

This is the default option and the simplest one. The language that is set as the default language is used for all users when displaying pages. See Figure 19-17 to see the user interface in which the default language is specified.

User-Preferred Language

If more than one language is enabled, users will see the fieldset shown in Figure 19-20 when they edit their “My account” pages.

436

CHAPTER 19 LOCALIZATION AND TRANSLATION

Figure 19-20. Choosing a user-specific language for e-mail messages

The language that a user has chosen can be retrieved as follows:

// Retrieve user 3's preferred language.

$account = user_load(1); // in the example I’m using the admin account $language = user_preferred_language($account);

If the user has not set a preferred language, the default language for the site will be returned. The result will be a language object (see the next section for more about the language object). When the “Language negotiation” setting is set to None, the user’s preferred language is used only for determining which language should be used for e-mail sent from the site. The user’s preferred language has no effect on the language used for page display when the “Language negotiation” setting is set to None.

The Global $language Object

You can determine the current language programmatically by looking at the global $language variable, which is an object. The variable is initialized during the DRUPAL_BOOTSTRAP_LANGUAGE portion of bootstrap. You can see what the object looks like by doing a var_dump():

global $language; var_dump($language);

Results are shown here:

object(stdClass) (11) {

["language"]

=> string(2) "fr"

["name"]

=> string(6) "French"

["native"]

=> string(9) "Français"

["direction"]

=> string(1) "0"

["enabled"]

=> int(1)

437

CHAPTER 19 LOCALIZATION AND TRANSLATION

["plurals"]

=>

string(1) "2"

["formula"]

=>

string(6) "($n>1)"

["domain"]

=>

string(0) ""

["prefix"]

=>

string(2) "fr"

["weight"]

=>

string(1) "0"

["javascript"]=>

string(0) ""

}

The RFC 4646 language identifier (such as fr in the previous example) can be retrieved by getting the language property of the $language of the $language object:

global $language;

$lang = $language->language;

Path Prefix Only

When language negotiation is set to Path Prefix Only, there are only two possibilities. Either a language path prefix is found in the path, or the default language is used. For example, suppose you are creating a site that supports users in both English and French. English is the default language for the site, but the French translation has also been installed and enabled. Going to Configuration -> Languages and clicking the “edit” link next to the French language will show you the user interface shown in Figure 1921. Notice that the “Path prefix” field is set to fr. This value could be changed to any string.

438

CHAPTER 19 LOCALIZATION AND TRANSLATION

With the path prefix set to fr, Drupal will determine the current language by looking at the requested URL. The process is shown in Figure 19-22.

Figure 19-21. User interface for the “Edit language” screen showing the “Path prefix” field

439

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]