function t

You are here

7 bootstrap.inc t($string, array $args = array(), array $options = array())
4.6 common.inc t($string, $args = 0)
4.7 common.inc t($string, $args = 0)
5 common.inc t($string, $args = 0)
6 common.inc t($string, $args = array(), $langcode = NULL)
8 bootstrap.inc t($string, array $args = array(), array $options = array())

Translates a string to the current language or to a given language.

The t() function serves two purposes. First, at run-time it translates user-visible text into the appropriate language. Second, various mechanisms that figure out what text needs to be translated work off t() -- the text inside t() calls is added to the database of strings to be translated. These strings are expected to be in English, so the first argument should always be in English. To enable a fully-translatable site, it is important that all human-readable text that will be displayed on the site or sent to a user is passed through the t() function, or a related function. See the Localization API pages for more information, including recommendations on how to break up or not break up strings for translation.

Translating Variables

You should never use t() to translate variables, such as calling

t($text); 

, unless the text that the variable holds has been passed through t() elsewhere (e.g., $text is one of several translated literal strings in an array). It is especially important never to call

t($user_text); 

, where $user_text is some text that a user entered - doing that can lead to cross-site scripting and other security problems. However, you can use variable substitution in your string, to put variable text such as user names or link URLs into translated text. Variable substitution looks like this:

$text = t("@name's blog", array('@name' => user_format_name($account)));

Basically, you can put variables like @name into your string, and t() will substitute their sanitized values at translation time. (See the Localization API pages referenced above and the documentation of format_string() for details about how to define variables in your string.) Translators can then rearrange the string as necessary for the language (e.g., in Spanish, it might be "blog de @name").

Parameters

$string: A string containing the English string to translate.

$args: An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See format_string() for details.

$options: An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): The language code to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to.

Return value

The translated string.

See also

format_string()

Related topics

2288 calls to t()
AccessDeniedTest::testAccessDenied in core/modules/system/lib/Drupal/system/Tests/System/AccessDeniedTest.php
AccessPluginBase::summaryTitle in core/modules/views/lib/Drupal/views/Plugin/views/access/AccessPluginBase.php
Returns the summary of the settings in the display.
AccessRoleUITest::testAccessRoleUI in core/modules/user/lib/Drupal/user/Tests/Views/AccessRoleUITest.php
Tests the role access plugin UI.
ActionListBuilder::buildHeader in core/modules/action/lib/Drupal/action/ActionListBuilder.php
Builds the header row for the entity listing.
ActionListBuilder::getDefaultOperations in core/modules/action/lib/Drupal/action/ActionListBuilder.php
Gets this list's default operations.

... See full list

79 string references to 't'
BasicSyntaxTest::testLikeBackslash in core/modules/system/lib/Drupal/system/Tests/Database/BasicSyntaxTest.php
Tests a LIKE query containing a backslash.
BasicSyntaxTest::testLikeEscape in core/modules/system/lib/Drupal/system/Tests/Database/BasicSyntaxTest.php
Tests escaping of LIKE wildcards.
BulkDeleteTest::testDeleteFieldInstance in core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php
Verify that deleting an instance leaves the field data items in the database and that the appropriate Field API functions can operate on the deleted data and instance.
ContentEntityDatabaseStorage::doLoadFieldItems in core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php
Loads values of configurable fields for a group of entities.
ContentEntityDatabaseStorage::readFieldItemsToPurge in core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php
Reads values to be purged for a single field of a single entity.

... See full list

File

core/includes/bootstrap.inc, line 1021
Functions that need to be loaded on every Drupal request.

Code

function t($string, array $args = array(), array $options = array()) {
  return \Drupal::translation()->translate($string, $args, $options);
}