t() function in drupal 7.

t() function make strings used in Drupal translatable. it translates a string to the current language or to a given language as well as formats a string for HTML display by replacing variable placeholders.

Syntex:

t($string, array $args = array(), array $options = array());

Example:

<?php
 // Example 1
 $text = t("Translatable string");
 // Example 2
 $text = t("@name's Article", array('@name' => $account->name));
?>

In above Example 2 t() function will substitute @name's sanitized values at translation time.

In t() function we use 3 placeholder like !, @, %. Details are following...

  1. @<placeholder_name>: @ placeholder escaped to HTML using Drupal check_plain() function.

    Example:

    <?php
     $text = t("@name's Article", array('@name' => $account->name));
     echo $text;
    ?>

    Output:

    Admin's Article
  2. %<placeholder_name>: % placeholder escaped to HTML and emphasized it with %<em%> tags, for this it use drupal_placeholder() function.

    drupal_placeholder() syntex:

    function drupal_placeholder($text) {
         return '<em class="placeholder">' . check_plain($text) . '';
    }

    Example:

    <?php
      $text = t("%name's Article", array('%name' => $account->name));
      echo $text;
    ?>

    Output:

     Admin's Article.
  3. !<placeholder_name>: ! placeholder use for text that has already been sanitized(user-supplied text that has already been run through check_plain() or filter_xss() previously). it means no sanitization or formatting.

    <?php
      $text = t("!name's Article", array('!name' => $account->name));
      echo $text;
    ?>

    Output:

    Admin's Article.
Tags