t() : Usage of T Function in Drupal 7

t function is one of the most useful functions in Drupal core, it makes multi-lingual sites so easy to maintain.

This is t().

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

A very basic example

We have a title string “Backyard Garden” that we want it to be translatable, we simply do this.

t('Backyard Garden');
Note that the $string should be in the default language of the site, if the source language is different fron default language it might cause some confusion. (If you don’t use default language in your t function you should define ‘langcode’ in $options.)
String with plain text variables

This is when you have variables in your string, use @variable in the string then define later in $args. @variable with the “@” prefix means the text should be run through check_plain.

t('@name\'s Backyard Garden',array('@name'=>$user->name));
##### String With plain text variables and markups

If you want to use markup such as span or a in your string and you have no choice but to put the markup here, put them in the $string, not in the $arg.

t('@name\'s Backyard Garden (@count) ',array('@name'=>$user->name,'@count'=>$count));
##### String With l() (l function)

If you want to have a link in the string, use !variable which indicates that the text should be inserted as-is.

t('Welcome to the Backyard Garden. !url.',array('!url'=> l(t('My garden'),'user/'.$user->uid.'/garden')));
##### String wrapped with theme_placeholder()

You can also use %variable with prefix %, which indicates that the string should be HTML escaped and highlighted with theme_placeholder() and shows up by default as emphasized.

t('Welcome to %name Backyard Garden.',array('%name'=>$user->name));
##### String With context

It happens a lot when you want the same strings to be translated differently in different context, use ‘context’.

t('Backyard Garden',array(),array('context'=>'Site section title'));