0

If I understand correctly, t() is the Drupal translations function, is l() simply calling the localization settings for translation specifics, or is there something more to it then that?

  • l() Formats an internal or external URL link as an HTML anchor tag. – Shabir A. Dec 16 '15 at 20:53
2

The l() function is primarily for links. It takes the parameters of $text, $path, array $options = array() to properly translate links. It also returns a HTML string containing a link to the given path.

It's use looks something like this:

<?php
  l(t('Link text'), 'about-us', array('attributes' => array('class' => array('about-link'))));
?>

Another note is it looks as though it's going away in D8, as it currently has no entry in the APIs for 8. Instead, Drupal 8 will use the class:link like so:

$link = Link::fromTextAndUrl($text, $url);

t() is much more general and is used to translate strings. It also takes a different set of arguments ($string, array $args = array(), array $options = array()).

  • Wow, that is a very concise explanation. Thank you so much! Very interesting about D8 by the way! – Brian Smith Dec 16 '15 at 21:05
  • 1
    It's being replaced by the link class, I'll update the answer to reflect that. – Jance Dec 16 '15 at 21:08
  • 1
    l() and url() are long gone from Drupal 8, drupal.org/node/2346779 – mpdonadio Dec 16 '15 at 21:09
  • Yes and no. The Link class is a thin wrapper for the link generator service. In procedural code, you can use the Link class. In OO code, you should use the LinkGeneratorTrait or inject the service (preferred). – mpdonadio Dec 16 '15 at 21:20
1

function l correctly handles aliased paths and adds an 'active' class attribute to links that point to the current page (for theming), so all internal links output by modules should be generated by this function if possible.

However, for links enclosed in translatable text you should use t() and embed the HTML anchor tag directly in the translated string. For example:

t('Visit the <a href="@url">settings</a> page', array('@url' => url('admin')));

This keeps the context of the link title ('settings' in the example) for translators.

To make strings used in Drupal translatable. Every string in a t() function can be translated through the Drupal UI. That way people who use a Drupal site in a different language can adapt it to their needs.

2-why can't i use a variable in it ?

You can, but you have to do it through a placeholder like one of these:

<?php
$text = t("This is !name's website", array('!name' => $username));
$text = t("This is @name's website", array('@name' => $username));
$text = t("This is %name's website", array('%name' => $username));
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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