Drupal - How to use t() function for a text with anchor links?

You're absolutely correct; you should never concatenate strings with t(), the whole point is that it uses placeholders so you don't have to. Specifically:

// DO NOT DO THESE THINGS
$BAD_EXTERNAL_LINK = t('Look at Drupal documentation at !handbook.', array('!handbook' => '<a href="http://drupal.org/handbooks">'. t('the Drupal Handbooks') .'</a>'));

$ANOTHER_BAD_EXTERNAL_LINK = t('Look at Drupal documentation at <a href="http://drupal.org/handbooks">the Drupal Handbooks</a>.');

$BAD_INTERNAL_LINK = t('To get an overview of your administration options, go to !administer in the main menu.', array('!administer' => l(t('the Administer screen'), 'admin'));

Instead of those you should use the following:

// Do this instead.
$external_link = t('Look at Drupal documentation at <a href="@drupal-handbook">the Drupal Handbooks</a>.', array('@drupal-handbook' => 'http://drupal.org/handbooks'));

$internal_link = t('To get an overview of your administration options, go to <a href="@administer-page">the Administer screen</a> in the main menu.', array('@administer-page' => url('admin')));

See Dynamic or static links and HTML in translatable strings for full details.


I suggest you try l() function and learn it, it is very good and simple. and solution for you

l(t('yourtext'),'link/to/path');

I hope can learn you drupal base method to write your links in drupal :)

updated Part:

$link='<a href="link/to/path">'.t('Contact Us').'</a>';
t('first part of your context @link the end part of your text',array('link'=>$link));

Tags:

I18N L10N