18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating Your First <strong>Module</strong><br />

<strong>Drupal</strong> supports dozens of languages. This is one of the strongest features of<br />

<strong>Drupal</strong>'s internationalization and localization effort. The method by which<br />

<strong>Drupal</strong> supports translation is largely through the t() function.<br />

There are three features of this function that every developer should understand:<br />

• What happens when t() is called<br />

• How <strong>Drupal</strong> builds the translation table<br />

• Additional features you get by using the t() function<br />

First, let's look at what the t() function does when it is called. If no language<br />

support is enabled and no second argument is passed to t(), it simply returns the<br />

string unaltered. If more languages are enabled and the user's language is something<br />

other than English, <strong>Drupal</strong> will attempt to replace the English language string with a<br />

string in the appropriate language.<br />

The second thing to look at is how <strong>Drupal</strong> builds the translation information. There<br />

are two aspects to this: The human aspect and the technical one. The translations<br />

themselves are done by dozens and dozens of volunteers who translate not only<br />

<strong>Drupal</strong>'s core, but also many of the add-on modules. Their translations are then<br />

made into downloadable language bundles (.po files) that you can install on<br />

your site.<br />

On the more technical side, this dedicated group of translators does not simply<br />

search the source code looking for calls to the t() function. Instead, an automated<br />

tool culls the code and identifies all of the translatable strings. This automated tool,<br />

though, can only extract string literals. In other words, it looks for calls like this:<br />

t('This is my string');<br />

It cannot do anything with lines like this, though:<br />

$variable = 'This is a string';<br />

t($variable);<br />

Why won't the translation system work in the case above? Because when the<br />

automated translation system runs through the code, it does not execute the code.<br />

It simply reads it. For that reason, it would become cumbersome (and many times<br />

impossible) to determine what the correct value of a variable is.<br />

The locale module can, under certain circumstances, identify<br />

other strings that were not correctly passed into the t() function<br />

and make them available to translators. This, however, should not<br />

be relied upon.<br />

[ 40 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!