8.2.x bootstrap.inc t($string, array $args = array(), array $options = array())
8.0.x bootstrap.inc t($string, array $args = array(), array $options = array())
8.1.x bootstrap.inc t($string, array $args = array(), array $options = array())
8.3.x bootstrap.inc t($string, array $args = [], array $options = [])
4.6.x common.inc t($string, $args = 0)
4.7.x common.inc t($string, $args = 0)
5.x common.inc t($string, $args = 0)
6.x common.inc t($string, $args = array(), $langcode = NULL)
7.x bootstrap.inc t($string, array $args = array(), array $options = array())

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

In order for strings to be localized, make them available in one of the ways supported by the Localization API. When possible, use the \Drupal\Core\StringTranslation\StringTranslationTrait $this->t(). Otherwise create a new \Drupal\Core\StringTranslation\TranslatableMarkup object directly.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

Parameters

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

array $args: (optional) 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 \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

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

  • 'langcode' (defaults to the current language): A 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. See the Internationalization topic for more information about string contexts.

Return value

\Drupal\Core\StringTranslation\TranslatableMarkup An object that, when cast to a string, returns the translated string.

See also

\Drupal\Component\Render\FormattableMarkup::placeholderFormat()

\Drupal\Core\StringTranslation\StringTranslationTrait::t()

\Drupal\Core\StringTranslation\TranslatableMarkup::__construct()

Related topics

2037 calls to t()
AccessDeniedTest::testAccessDenied in core/modules/system/src/Tests/System/AccessDeniedTest.php
AccessPermissionTest::testAccessPerm in core/modules/user/src/Tests/Views/AccessPermissionTest.php
Tests perm access plugin.
AccessRoleUITest::testAccessRoleUI in core/modules/user/src/Tests/Views/AccessRoleUITest.php
Tests the role access plugin UI.
AccountPermissionsCacheContext::getLabel in core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php
ActionListBuilder::buildHeader in core/modules/action/src/ActionListBuilder.php
Builds the header row for the entity listing.

... See full list

94 string references to 't'
BasicSyntaxTest::testGetFullQualifiedTableName in core/modules/system/src/Tests/Database/BasicSyntaxTest.php
Tests \Drupal\Core\Database\Connection::getFullQualifiedTableName().
BasicSyntaxTest::testLikeBackslash in core/modules/system/src/Tests/Database/BasicSyntaxTest.php
Tests a LIKE query containing a backslash.
BasicSyntaxTest::testLikeEscape in core/modules/system/src/Tests/Database/BasicSyntaxTest.php
Tests escaping of LIKE wildcards.
BulkDeleteTest::testDeleteField in core/modules/field/src/Tests/BulkDeleteTest.php
Verify that deleting a field leaves the field data items in the database and that the appropriate Field API functions can operate on the deleted data and field definition.
DatabaseTestController::pagerQueryEven in core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php
Runs a pager query and returns the results.

... See full list

File

core/includes/bootstrap.inc, line 295
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);
}

Comments

dmitrii’s picture

Since 8.0.0-beta16 t() returns a TranslatableMarkup object instead of a string.
See https://www.drupal.org/node/2564451 for details.

selwynpolit’s picture

Here is an example where I used $this->t() in a contrib module called config_devel. This should be in the ConfigDevelAutoImportSubscriber.php file and shows usage of the arguments to provide placeholders for variables. I hope this is useful to someone and saves you some time. I've marked the 3 relevant places that need to be tweaked.

use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\StringTranslation\StringTranslationTrait; //Need this for $this->t()
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;

class ConfigDevelAutoImportSubscriber extends ConfigDevelSubscriberBase implements EventSubscriberInterface {
use StringTranslationTrait; //Need this for $this->t()

/**
* Reinstall changed config files.
*/
public function autoImportConfig() {
$config = $this->getSettings();
$changed = FALSE;
foreach ($config->get('auto_import') as $key => $file) {
if ($new_hash = $this->importOne($file['filename'], $file['hash'])) {
$config->set("auto_import.$key.hash", $new_hash);

//Need this for $this->t()
drupal_set_message($this->t('Automatically reloaded yaml config file: @filename', array('@filename' => $file['filename'])));

$changed = TRUE;
}
}
if ($changed) {
$config->save();
}
}