Last updated 30 July 2014. Created on 23 August 2008.
Edited by pingwin4eg, patrickd, klausi, Eyal Shalev. 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:

// 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 forums, or join #drupal-support in IRC.


pingwin4eg’s picture

The example above is not the best choice, because it is not very good to store a localized string in a Drupal variable.

sheetal.nepte’s picture

I think variable_get should only read the actual value of variable and not it's translation. For storing translation, one can use multilingual module.
It is not a good practice. Anyone can comment on it?

Rajesh Ashok’s picture

variable_get is not used for storing.