Community Documentation

String translation is mandatory; use it and love it

Last updated September 18, 2011. Created by Senpai on August 23, 2008.
Edited by gary4gar, figaro, dman. 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
$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.

nobody click here