So far, we’ve been focusing on the translation of Drupal’s user interface. But what about the content? Once the current
language setting has been determined, there’s a good chance that the user wants to see the site content in that language! Let’s
find out how content translation works.
Introducing the Content Translation Module
Drupal comes with a built-in way to manage translation of content: the content translation module. This module adds additional multilingual support and translation management options to Drupal content types.
Multilingual Support
After going to the Modules page and enabling the Locale and Content translation modules, “Multilingual support” options
will show up in the “Publishing options” field set of each content type. To see the settings, go to Structure -> Content types, and click the“edit” link for the Basic page content type. Expanding the “Publishing options” field set should reveal the
new settings for “Multilingual support,” as shown in Figure 19-24.
Click the Enabled radio button and save the content type. Now if you go to Create content -> Page, you will see a new drop-down field on the content creation form that allows you to select which language the content will be written in or whether the
content is “Language neutral.” The field is shown in Figure 19-25.
Figure 19-25. The language selection field on the content creation form
After creating a few pages in different languages, you can see that the administration page for content at Administer -> Content
management -> Content has changed to display the language of the post. Also, an option to filter content by language has been
added, as shown in Figure 19-26.
Figure 19-26. The content administration page with multilingual support enabled
Multilingual Support with Translation
Having the ability to create content in multiple languages is good. However, most sites do not have one piece of content in
English and another unrelated piece of content in French. Instead, the French content is usually a translation of the English
content (or vice versa). When “Multilingual support” for a content type is set to “Enabled, with translation” (see Figure
19-24), that becomes possible. It involves the following approach:
1. A post is created in one language. This is the source post.
2. Translations of the post are created.
Let’s step through these tasks with an example. First, make sure that the current “Multilingual support” setting for the Page
content type is set to “Enabled.” Next, we’ll create a simple page in English. Go to Create content -> Page, and type Hello
for the title and Hello my friends for the body. Set the language selection to English, and click the Save button. You should now see a Translate tab in addition to the usual View and Edit tabs (see Figure 19-27).
Clicking the Translate tab reveals a summary of the post’s translation status. As shown in Figure 19- 28, a source post exists in English, but that’s all. Let’s create a French translation by clicking the “add translation” link.
Clicking the “add translation” link brings up the node editing form again, but this time, the language selection is set to French.Type Bonjour for the title and Ayez un beau jour for the body.When the Save button is clicked, a new node will be added.Drupal will automatically create links between the source node and the translations, labeled with the language. Figure
19-29 shows how the French translation of the source node looks when the source node is in English and an additional
translation exists in French.
The links are built by the implementation of hook_node_view() in modules/translation/translation.module:
/**
* Implements hook_node_view().
*
* Display translation links with native language names, if this node
* is part of a translation set.
*/
function translation_node_view($node, $view_mode) {
if (isset($node->tnid) && $translations = translation_node_get_translations($node->tnid))
{
$path = 'node/' . $node->nid;
$links = language_negotiation_get_switch_links(LANGUAGE_TYPE_INTERFACE, $path); if (is_object($links))
{
$links = $links->links;
// Do not show link to the same node.
unset($links[$node->language]);
$node->content['links']['translation'] = array( '#theme' => 'links translation_node', '#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),);}}}
In addition to the links that are generated, the locale module provides a language switcher block that can be enabled under Structure -> Blocks. The language switcher block will show up only if multiple languages are enabled and the “Detection
and Selection” setting is set to something other than Default. The language switcher block is shown in Figure 19-30.
additional field set called “Translation settings” in the node editing form. This field set contains a single check box labeled “Flag
translations as outdated,” as shown in Figure 19-31.
A source node and translations of the source node have separate node numbers and, in fact, exist as completely separate nodes
in the database. They are related to each other by the tnid column of the node table, which has as its value the node ID of
the source node. Assuming that the English version is the source node and is the first node on the site and the French and
Hebrew translations are the next two nodes added, the node table will look like Figure 19-33.
Notice that the 1 in the translate column indicates an outdated translation.
Localization- and Translation-Related Files
Sometimes, knowing which parts of Drupal are responsible for which localization or translation functions is difficult. Table
19-1 shows these files and their responsibilities.
Additional Resources
Internationalization support is very important to the Drupal project. To follow the progress of this effort or to get involved, see http://groups.drupal.org/i18n. Also check out the Translation Management module at http://drupal.org/project/translation_management.
content type is set to “Enabled.” Next, we’ll create a simple page in English. Go to Create content -> Page, and type Hello
for the title and Hello my friends for the body. Set the language selection to English, and click the Save button. You should now see a Translate tab in addition to the usual View and Edit tabs (see Figure 19-27).
Figure 19-27. The node now has a tab for translation.
Clicking the Translate tab reveals a summary of the post’s translation status. As shown in Figure 19- 28, a source post exists in English, but that’s all. Let’s create a French translation by clicking the “add translation” link.
Figure 19-28. Clicking the Translate tab shows a summary of the translation status.
Clicking the “add translation” link brings up the node editing form again, but this time, the language selection is set to French.Type Bonjour for the title and Ayez un beau jour for the body.When the Save button is clicked, a new node will be added.Drupal will automatically create links between the source node and the translations, labeled with the language. Figure
19-29 shows how the French translation of the source node looks when the source node is in English and an additional
translation exists in French.
Figure 19-29. The French translation of the source node has links to English and Hebrew versions.
The links are built by the implementation of hook_node_view() in modules/translation/translation.module:
/**
* Implements hook_node_view().
*
* Display translation links with native language names, if this node
* is part of a translation set.
*/
function translation_node_view($node, $view_mode) {
if (isset($node->tnid) && $translations = translation_node_get_translations($node->tnid))
{
$path = 'node/' . $node->nid;
$links = language_negotiation_get_switch_links(LANGUAGE_TYPE_INTERFACE, $path); if (is_object($links))
{
$links = $links->links;
// Do not show link to the same node.
unset($links[$node->language]);
$node->content['links']['translation'] = array( '#theme' => 'links translation_node', '#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),);}}}
In addition to the links that are generated, the locale module provides a language switcher block that can be enabled under Structure -> Blocks. The language switcher block will show up only if multiple languages are enabled and the “Detection
and Selection” setting is set to something other than Default. The language switcher block is shown in Figure 19-30.
Figure 19-30. The language switcher block
Let’s get back to our discussion of source nodes and their translations. If a node is a source node, editing it will show anadditional field set called “Translation settings” in the node editing form. This field set contains a single check box labeled “Flag
translations as outdated,” as shown in Figure 19-31.
Figure 19-31. The “Translation settings” fieldset in the node editing form of a source node
The check box is used to indicate that edits to the source node have been major enough to require retranslation. Checking the box to flag translations as outdated simply causes the word “outdated” to be displayed when viewing the translation status of anode. Compare Figure 19-28 with Figure 19-32.
Figure 19-32. The source post has been edited, and the translated post is flagged as outdated.
A source node and translations of the source node have separate node numbers and, in fact, exist as completely separate nodes
in the database. They are related to each other by the tnid column of the node table, which has as its value the node ID of
the source node. Assuming that the English version is the source node and is the first node on the site and the French and
Hebrew translations are the next two nodes added, the node table will look like Figure 19-33.
Figure 19-33. The tnid column tracks relationships between source nodes and their translations.
Localization- and Translation-Related Files
Sometimes, knowing which parts of Drupal are responsible for which localization or translation functions is difficult. Table
19-1 shows these files and their responsibilities.
Table 19-1. Files Related to Localization and Translation Within
Drupal
|
|
File
|
Responsibility
|
includes/bootstrap.inc
|
Runs the DRUPAL_BOOTSTRAP_LANGUAGE phase that determines the current language
|
includes/language.inc
|
Included by bootstrap if multiple languages are enabled; provides
code for
choosing a language and rewriting internal URLs to be language- specific
|
includes/common.inc
|
t() is found here,
as is drupal_add_css(), which supports right-to-left languages.
|
includes/locale.inc
|
Contains user interfaces and functions for managing language translations
|
modules/locale/locale.module
|
Provides
string replacement
and translation imports when
modules or themes are installed
or enabled; adds language settings interface to path, node, and node type forms
|
modules/translation/translation.module
|
Manages source nodes and translations there of
|
modules/translation/translation.admin.inc
|
Provides the translation overview shown
when the Translate tab is clicked (see Figure 19-31)
|
Additional Resources
Internationalization support is very important to the Drupal project. To follow the progress of this effort or to get involved, see http://groups.drupal.org/i18n. Also check out the Translation Management module at http://drupal.org/project/translation_management.
Summary
In this chapter, you’ve learned the following:
• How the t() function works.
• How to customize built-in Drupal strings.
• How to export your customizations.
• What portable object and portable object template files are.
• How to download portable object template files and generate your own.
• How to import an existing Drupal translation.
• How to use style sheets for right-to-left language support.
• How language negotiation settings affect Drupal.
• How content translation works.
Không có nhận xét nào:
Đăng nhận xét