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

CHAPTER 19 LOCALIZATION AND TRANSLATION

Figure 19-3. The string Blog entry has not become Journal entry.

What’s wrong? Why was your custom string replacement array ignored? It’s because the string Blog entry is not the same as the string Blog. You can’t just pick substrings for replacement; you have to match the full string.

How do you find all the strings that contain the word Blog so that you can replace each string with its Journal equivalent? The locale module can help with this.

Tip Using string overrides in settings.php is highly performant (for small sets of strings only) because no database call is needed; the replacement string is simply looked up in an array. You don’t even have to have the locale module enabled for string overrides to work. See also the string overrides module at http://drupal.org/project/stringoverrides.

Replacing Strings with the Locale Module

Instead of using string replacement by defining a list of custom string replacements in settings.php, you can use the locale module to find strings for replacement and define what the replacements will be. A language translation is a set of custom string replacements for Drupal. When Drupal prepares to display a string, it will run the string through the t() function as outlined previously. If it finds a replacement in the current language translation, it will use the replacement; if not, it will simply use the original string. This process, which is what the locale() function does, is shown in a simplified form in Figure 19-4. The approach is to create a language with the language code en-US containing only the string(s) we want replaced.

421

CHAPTER 19 LOCALIZATION AND TRANSLATION

Figure 19-4. If the locale module does not find a replacement string in the current language translation, it will fall back to using the original string.

Okay, let’s begin the process of changing any strings containing “blog” to strings containing “journal.” Because Drupal will fall back to using the original string if no translation is found, we need to provide only the strings we want to change. We can put the strings into a custom language and let Drupal fall back to original strings for any strings we don’t provide. First, let’s add a custom language to hold our custom strings. The interface for doing that is shown in Figure 19-5. We’ll call it English-custom and use en-US for the language code and path prefix. Navigate to Configuration -> Languages -> Add a Language.

422

CHAPTER 19 LOCALIZATION AND TRANSLATION

Figure 19-5. Adding a custom language for targeted string translation

Now, enable your new language, and make it the default, as shown in Figure 19-6. Click “Save configuration,” uncheck the Enabled check box next to English, and click “Save configuration” again, as

423

CHAPTER 19 LOCALIZATION AND TRANSLATION

shown in Figure 19-7. With only one language enabled, users will not be presented with the somewhat confusing “Language settings” choice shown in Figure 19-8 when editing their user accounts.

Figure 19-6. Enabling the new language and selecting it as the default

Figure 19-7. Disabling English so that English-custom will be the only enabled language

Figure 19-8. The user interface on the “My account” page, where a user may select the preferred language for e-mail sent by the site. (The interface appears only if multiple languages are enabled.)

424

CHAPTER 19 LOCALIZATION AND TRANSLATION

Okay, you’ve got a single language translation called English-custom enabled. It is currently empty, since we haven’t added any string replacements yet. So for every string, Drupal will go through the process shown in Figure 19-4, fail to find a string replacement in English-custom, and fall back to returning the original English string from the English language. Let’s set up some string replacements. Navigate to Configuration -> Translate interface, which is shown in Figure 19-9.

Figure 19-9. The overview page of the “Translate interface” screen

Drupal uses just-in-time translation. When a page is loaded, each string is passed through the t() function and on through the locale() function where, if the string is not already present in the locales_source and locales_target database tables, it is added to those tables. So the values in the “Built-in interface” column in Figure 19-9 show that 1,020 strings have passed through t() and are available for translation. Go ahead and click around to some other pages in Drupal and then return to this one. You should see that the number of strings has increased as Drupal encounters more and more parts of the interface that will need translation. We’ll now use the locale module’s web interface to translate some strings.

After clicking the Translate tab, we are presented with a search interface that allows us to find strings for translation. Let’s search for all of those 1,020 or more strings that are available to us so far. The search interface is shown in Figure 19-10.

Figure 19-10. The search interface for showing translatable strings

425

CHAPTER 19 LOCALIZATION AND TRANSLATION

Selecting our language (English-custom), searching for all strings, and leaving the search box blank will show us all translatable strings. Each string has an “edit” link next to it. After the list of strings, the search interface is shown again at the bottom of the page. Since the list of strings is quite long, let’s reduce it to only the strings that contain the word “Translate.” Type the word Translate in the “String contains” field, and click the Filter button. The result should be a list of strings that contain the word “Translate,” as shown in Figure 19-11. Let’s change the string Translate interface to Translate language interface by clicking the “edit” link for that string.

Figure 19-11. A list of translatable strings containing the word “Translate” and their statuses

After you’ve edited the string, you are returned to the Translate tab (see figure 19-12). The page should have changed from “Translate interface” to “Translate language interface”.

Figure 19-12. The string "Translate" is now replace by the string "Translate language."

426

CHAPTER 19 LOCALIZATION AND TRANSLATION

Go ahead and search for the string Translate again. You should see in the resulting list of strings that the strikethrough is removed from the Languages column for this entry, indicating that the string has been translated, as shown in Figure 19-13.

Figure 19-13. The list of translatable strings after editing “Translate”

Note that the original string is shown, not the translation. If you return to the Overview tab, you will see that English-custom now has one replacement string available.

Now that you’ve learned how to change strings, we can get on to the business of changing all occurrences of “blog” to “journal.” After enabling the blog module and visiting the blog-related pages (such as /node/add/blog and blog/1), the translatable strings should be available for us to translate. The search at Configuration -> Translate interface is case-sensitive, so one search for “blog” and another for “Blog” will show us all the occurrences and let us change them to equivalent replacement strings using our preferred words “journal” and “Journal.”

427

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