| 5 common.inc | t($string, $args = 0) |
| 6 common.inc | t($string, $args = array(), |
| 7 bootstrap.inc | t($string, array $args = array(), array $options = array()) |
| 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.
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' => format_username($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.) Translators can then rearrange the string as necessary for the language (e.g., in Spanish, it might be "blog de @name").
During the Drupal installation phase, some resources used by t() wil not be available to code that needs localization. See st() and get_t() for alternatives.
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
st()
get_t()
Related topics
2466 calls to t()
40 string references to 't'
File
- includes/
bootstrap.inc, line 1478 - Functions that need to be loaded on every Drupal request.
Code
function t($string, array $args = array(), array $options = array()) {
global $language;
static $custom_strings;
// Merge in default.
if (empty($options['langcode'])) {
$options['langcode'] = isset($language->language) ? $language->language : 'en';
}
if (empty($options['context'])) {
$options['context'] = '';
}
// First, check for an array of customized strings. If present, use the array
// *instead of* database lookups. This is a high performance way to provide a
// handful of string replacements. See settings.php for examples.
// Cache the $custom_strings variable to improve performance.
if (!isset($custom_strings[$options['langcode']])) {
$custom_strings[$options['langcode']] = variable_get('locale_custom_strings_' . $options['langcode'], array());
}
// Custom strings work for English too, even if locale module is disabled.
if (isset($custom_strings[$options['langcode']][$options['context']][$string])) {
$string = $custom_strings[$options['langcode']][$options['context']][$string];
}
// Translate with locale module if enabled.
elseif ($options['langcode'] != 'en' && function_exists('locale')) {
$string = locale($string, $options['context'], $options['langcode']);
}
if (empty($args)) {
return $string;
}
else {
return format_string($string, $args);
}
}
Login or register to post comments
Comments
Do not use in hook_schema()
Note: t() should not be used in hook_schema(). See: http://drupal.org/node/332123
Inserting a link in a t()
Allthough it's described, I thoughed the drupal 6 had some good examples.
There are three styles of placeholders:
!variable, which indicates that the text should be inserted as-is. This is useful for inserting variables into things like e-mail.
<?php
$message = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => l(t('My account'), "user/$account->uid")));
?>
@variable, which indicates that the text should be run through check_plain, to escape HTML characters. Use this for any output that's displayed within a Drupal page.
<?php
$title = t("@name's blog", array('@name' => $account->name));
?>
%variable, which indicates that the string should be HTML escaped and highlighted with theme_placeholder() which shows up by default as emphasized.
<?php
$message = t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));
?>
equivalent of tt function in drupal 6?
How is tt() (from i18n module in drupal 6) functionality handled in drupal 7?
tt() handled translation of user entered (ie. admin - f.ex. labels in field displays) text - by giving it a unique label.
i18n_strings
The i18n module comes with a sub module called i18n_string and it offers a i18n_string() function which is the successor to D6's tt().
See http://drupalcontrib.org/api/drupal/contributions%21i18n%21i18n_string%2....
Also, notice that tt() has been deprecated by i18nstrings() in D6 as well, although it is still available as a wrapper: http://drupalcontrib.org/api/drupal/contributions%21i18n%21i18nstrings%2...