Last updated November 19, 2012. Created by Senpai on August 23, 2008.
Edited by patrickd, klausi, Eyal Shalev, gary4gar. Log in to edit this page.

String Accessibility

Drupal is available worldwide and while we code it in English, its textual string outputs get translated to numerous languages. The t() function is mandatory for all text strings that will be read by a user or an admin, as it is the only way Drupal core can re-translate those strings to something legible for the readers.

String Security

t() is essential for localization, but also has important security functionality that every Drupal developer should know about and understand. It is much maintenance-friendlier to wrap all user-facing text in t() in the first place than to go back in to t()-ify your code when you or someone else decides they want to use it in another language.

Using t()

The introduction to localization is a useful start. It covers how to use placeholder tokens in your dynamic strings, and how you can use HTML within translated strings in moderation.

Whenever you use default text values, also wrap them with the t() function. If you don't, these values cannot be translated through the Translate interface functionality. Example:

<?php
// Bad practice. The word 'Submit' cannot be translated
$button_text = variable_get( 'mymodule_submit_button_text', 'Submit' );
// Good practice. The word 'Submit' get's entered into the translation tables.
// Do NOT pass user provided content into t()!
$button_text = variable_get( 'mymodule_submit_button_text', t('Submit') );
?>

The full API documentation for t() is the most detailed. Be sure to have a look at that.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.