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

CHAPTER 19 LOCALIZATION AND TRANSLATION

#: misc/drupal.js

msgid "An AJAX HTTP error occurred." msgstr ""

#: misc/drupal.js

msgid "HTTP Result Code: !status" msgstr ""

The .po file consists of some metadata headers followed by the translated strings. Each string has three components: a comment that shows where the string first occurred, an msgid denoting the original string, and an msgstr denoting the translated string to use. For a full description of the .po file format, see www.gnu.org/software/gettext/manual/gettext.html#PO-Files.

The en-US.po file can now be imported into another Drupal site (that has the locale module enabled) using the import tab at Configuration -> Translate interface.

Portable Object Templates

While a translation consists of some metadata and a lot of original and translated strings, a portable object template (.pot) file contains all the strings available for translation, without any translated strings. This is useful if you are starting a language translation from scratch or want to determine whether any new strings were added to Drupal since the last version before modifying your site (another way to find this out would be to upgrade a copy of your Drupal site and search for untranslated strings as shown in the “Replacing Built-In Strings with Custom Strings” section).

Starting a New Translation

You can download the translation files for Drupal core as well as for many contributed modules in dozens of languages from http://localize.drupal.org. On this section of Drupal.org, you will find language-specific translation files (.po files) that can be downloaded and installed, immediately providing multilingual capabilities on your site. You may also wish to install the Localized Drupal (http://drupal.org/project/l10_install) module, which programmatically pulls user interface translations from http://localize.drupal.org, and the Localization Client module (http://drupal.org/ l10n_client), which provides easy ways to localize your site interface through an on-page localization editor interface—allowing customization of the interface translation right on the web pages that are being viewed.

Generating .pot Files with Translation Template Extractor

The contributed translation template extractor module (see http://drupal.org/project/potx) can generate .pot files for you. This is useful if you’ve written your own module or downloaded a contributed module for which there is no existing translation. The translation template extractor module contains both a command-line version and a web-based version of the extractor. If you are familiar with the xgettext program for Unix, think of this module as a Drupal-savvy version of that program. This module is used under the hood at http:// localize.drupal.org to generate translation files.

429

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

CHAPTER 19 LOCALIZATION AND TRANSLATION

Creating a .pot File for Your Module

Let’s generate a .pot file for the job post module we created in Chapter 8.

First, we’ll need to install the translation template extractor module. Copy the download link from http://drupal.org/project/potx, and install the module by navigating to Modules page and clicking the “Install new module” link. Paste the download link in the “Install from a URL” text box and click the Install button.

Using the Command Line

Copy potx.inc and potx-cli.php from the potx module’s directory into the job_post module’s directory at sites/all/modules/custom/job_post. Next, we need to run the extractor, so it can create the

.pot files.

Caution You’re adding to your Drupal site an executable PHP script that needs write privileges to the directory it runs in (so it can write the .pot file). Always do template extraction on a copy of your site on your development machine, never on a live site.

Here are the results from running the extractor:

$ cd sites/all/modules/custom/job_post

$ php potx-cli.php

Processing sponsor.tpl.php...

Processing job_post.module...

Processing job_post.install...

Processing job_post.info...

Let’s see what was generated:

general.pot

job_post.install

potx.inc

installer.pot

job_post.module

sponsor.tpl.php

job_post.info

potx-cli.php

 

Running the extractor script resulted in a new file called general.pot, which contains the strings from sponsor.tpl.php, job_post.module, job_post.info, and job_post.install. The script placed all the strings into general.pot by default, but it can generate separate files if you’d prefer. Run the following to see the various options offered by the extractor script.

$ php potx-cli.php –-help

In the present case, it’s handy to have all of the strings in one file. If we were to share this translation template with others, we’d create a translations subdirectory inside the annotate directory, move the general.pot into the translations directory, and rename it annotate.pot. If we then made a French translation by opening the combined .pot file, translating the strings, and saving it as fr.po, our module directory would look like this:

430

CHAPTER 19 LOCALIZATION AND TRANSLATION

general.pot

job_post.install

potx.inc

installer.pot

job_post.module

sponsor.tpl.php

job_post.info

potx-cli.php

 

translations/

 

 

annotate.pot

 

 

fr.po

 

 

Using the Web-Based Extractor

Instead of using the command line, you can extract strings from your module using the web-based user interface provided by the translation template extractor module. After making sure that you have installed the module, go to the Modules page, and enable both the job_post and translation template extractor modules. Next, go to Configuration -> Translate interface, and notice the new Extract tab. Click it, and you’ll be able to generate a .pot file by expanding the Directory “sites/all/modules” group, and within that expanded list the module that you want to generate the translation for. Next select the “Language independent template” radio button, and click the Extract button, as shown in Figure 19-14. The .pot file will be downloaded via your web browser. You can then place the .pot file in the module’s directory as we did with the command-line extractor.

Figure 19-14. Extracting a .pot file for the annotate module using the web-based user interface of the translation template extractor module

Creating .pot Files for an Entire Site

If you wish to create .pot files for all translatable strings in your site, place the potx.inc and potxcli.php files at the root of your site, ensure you have write access to that current directory, and run potx-cli.php. You would run the script from the command line with the mode parameter set to core if

431

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