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

C H A P T E R 1 9

■ ■ ■

Localization and Translation

Localization is the replacement of strings in the user interface with translated strings appropriate for the user’s locale. Drupal is developed and used by an international community. Therefore it supports localization by default, as well as offering theming support for right-to-left languages such as Arabic and Hebrew. In this chapter, you’ll see how to enable localization and how to use interface translation to selectively replace Drupal’s built-in strings with strings of your own. Then, we’ll look at full-fledged translations and learn how to create, import, and export them. Finally, we’ll examine Drupal’s ability to present the same content in multiple languages (such as a Canadian web site that presents content in English and French) and learn how Drupal selects the appropriate language to display.

Enabling the Locale Module

The locale module, which provides language handling functionality and user interface translation for Drupal, is not enabled when you install Drupal. This is in accordance with Drupal’s philosophy of enabling functionality only when needed. You can enable the locale module on the Modules page. If Drupal has been installed using a language translation other than English, the locale module is enabled as part of the installation process. The examples in this chapter assume the locale module is enabled.

User Interface Translation

The interface for Drupal is made up of words, phrases, and sentences that communicate with the user. In the following sections, you’ll see how they can be changed. Our examples will focus on string replacement, with the understanding that translation has its foundation in string replacement.

Strings

From a programming perspective, a string is a series of characters, such as the five-character string Hello. The translation of strings forms the basis of user interface translation in Drupal. When Drupal prepares a string for output, it checks if the string needs to be translated, so that if the English language is enabled, the word “Hello” is displayed, while if the French language is enabled, the word “Bonjour” is displayed. Let’s examine how that happens.

417

CHAPTER 19 LOCALIZATION AND TRANSLATION

Translating Strings with t()

All strings that will be shown to the end user in Drupal should be run through the t() function; this is Drupal’s translate function, with the function name shortened to “t” for convenience because of its frequent use.

Note Some places in Drupal run t() implicitly, such as strings passed to watchdog() or titles and descriptions in the menu hook. Plurals are translated with format_plural(), which takes care of calling t() (see http://api.drupal.org/api/function/format_plural/7).

The locale-specific part of the t() function looks like this:

function locale($string = NULL, $context = NULL, $langcode = NULL) { global $language;

$locale_t = &drupal_static(__FUNCTION__);

if (!isset($string)) {

// Return all cached strings if no string was specified return $locale_t;

}

$langcode = isset($langcode) ? $langcode : $language->language;

//code that grabs the translations from cache or the database removed from the example for

//brevity’s sake

return ($locale_t[$langcode][$context][$string] === TRUE ? $string : $locale_t[$langcode][$context][$string]);

}

In addition to translation, the t() function also handles insertion of values into placeholders in strings. The values are typically user-supplied input, which must be run through a text transformation before being displayed.

t('Hello, my name is %name.', array('%name' => 'John');

Hello, my name is John.

The placement of the text to be inserted is denoted by placeholders, and the text to be inserted is in a keyed array. This text transformation process is critical to Drupal security (see Chapter 21 for more information). Figure 19-1 shows you how t() handles translation; see Figure 21-1 to see how t() handles placeholders.

418

CHAPTER 19 LOCALIZATION AND TRANSLATION

Figure 19-1. How t() does translation and placeholder insertion, assuming the current language is set to French

Replacing Built-In Strings with Custom Strings

Translating the user interface is essentially replacing one string with another. Let’s start small, choosing just a few strings to change. There are a couple of possible solutions to the translation problem. We’ll approach them from the simplest to the most complex. The first involves editing your settings file, and the second involves the locale module. Let’s start by doing a simple string replacement in the breadcrumb trail and move on to replacing Blog with Journal.

419

Download from Wow! eBook <www.wowebook.com>

CHAPTER 19 LOCALIZATION AND TRANSLATION

String Overrides in settings.php

Find your settings.php file (typically at sites/default/settings.php). You may need to make the file writable before making changes, as Drupal tries its best to keep this file read-only. Scroll to the end of settings.php.We’ll add ‘home’ => ‘Sweet Home’ to the list of values to be translated after removing the leading hash signs (#).

/**

*String overrides:

*To override specific strings on your site with or without enabling locale

*module, add an entry to this list. This functionality allows you to change

*a small number of your site's default English language interface strings.

*Remove the leading hash signs to enable.

*/

$conf['locale_custom_strings_en'] = array( 'forum' => 'Discussion board',

'@count min' => '@count minutes', ‘home’ => ‘Sweet Home’,

);

If you visit your site, you’ll notice that in the breadcrumb trail, Home has been changed to Sweet Home, as shown in Figure 19-2.

Now that you know how to do string overrides, let’s go ahead and replace the word Blog with the word Journal:

$conf['locale_custom_strings_en'] = array( 'Blog' => 'Journal',

);

Figure 19-2. The string Home is replaced with Sweet Home in the breadcrumb trail.

Then enable the blog module on the Modules page. Go to Add content -> Blog entry, and you should see a screen like the one shown in Figure 19-3.

420

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