8

I have a text that I must show in 5 languages. I am using t() for that. The text is:

In case of any issues, please Contact Us for assistance.

I want the phrase "Contact Us" to be a link. So this is what I have done:

$text = t('In case of any issues, please ')."<a href="link/to/path">".t('Contact Us')."</a>".t('for assistance');

I suspect that this might not be the correct way to do this as this will break the translation. I fear that the sentence will not be translated as a whole but in parts.

Am I right?

And if I am, what could be the correct way to do it?

3 Answers 3

21

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.

5
  • Can I use HTML inside t() function as you have used <a> tag inside your example of t().
    – kmdhrm
    Dec 9, 2013 at 9:29
  • 2
    Yes, absolutely - bear in mind that's not 'my' example exactly, it's taken straight from the official documentation. Anything you see in the first example, never do - anything you see in the second example, always do :)
    – Clive
    Dec 9, 2013 at 9:30
  • The general 'rule' is to avoid putting block-level tags into a translation, but there are caveats. The docs page has full details
    – Clive
    Dec 9, 2013 at 9:38
  • @clive I think using html tag in t is not good idea because in translation has difficulty.
    – Yuseferi
    Dec 9, 2013 at 9:57
  • @zhilevan It's the recommended way to do things, though. If you read through the docs page it does make sense
    – Clive
    Dec 9, 2013 at 10:03
5

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));
5
  • l() will add the link to complete text. I only want to add it to a prat of text. How can I do that?
    – kmdhrm
    Dec 9, 2013 at 7:59
  • @DipakYadav put it before it try something like this $text= t('first part').l(....).t('endpart');
    – Yuseferi
    Dec 9, 2013 at 8:25
  • @DipakYadav answer updated.
    – Yuseferi
    Dec 9, 2013 at 8:39
  • @zhilevan You shouldn't concatenate strings while using t(), it defeats the point of using that function in the first place
    – Clive
    Dec 9, 2013 at 9:24
  • @Clive, ok but I use something like it before, I update the answer by better solution
    – Yuseferi
    Dec 9, 2013 at 9:59
-3

This solution is work for you.

drupal_set_message(t('Payment Method Added Successfully -  '.l('add anchor link')),'status');
2
  • Can you just tell me what is the wrong with my suggestion?
    – priyank
    Jul 25, 2018 at 8:15
  • Look at the other examples. Read the documentation. Sep 7, 2022 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.