Search code examples
drupaldrupal-7translation

Drupal7 : Why t() is not working?


I'm in charge of an old site on Drupal7, that I know too little. This site is a bilingual site English/French. So in a template, I added some fields, with strings that need to be translated :

<div class="offer_content">
    <span class="title" style="text-transform:uppercase;font-size:18px;"><?php echo t('Our Firma').(': ');?></span>
    <?php echo html_entity_decode(t($fields['field_presentation_societe']->content)); ?>
    <span class="title" style="text-transform:uppercase;font-size:18px;"><?php echo t('Job presentation').(': ');?></span>
    <?php echo html_entity_decode(t($fields['body']->content));?>
    <span class="title" style="text-transform:uppercase;font-size:18px;"><?php echo t('Skills').(': ');?></span>
    <?php echo html_entity_decode(t($fields['field_profil_recherche']->content)); ?>

However, in French, that keeps me showing "Our Firma" and "Skills" ... instead of the translation ? Why ?

Thanks you :)


Solution

  • Variables ain't translatable by design. See the t-docs:

    Translating Variables

    You should never use t() to translate variables, such as calling

    t($text);
    

    , unless the text that the variable holds has been passed through t() elsewhere (e.g., $text is one of several translated literal strings in an array). It is especially important never to call

    t($user_text);
    

    , where $user_text is some text that a user entered - doing that can lead to cross-site scripting and other security problems. However, you can use variable substitution in your string, to put variable text such as user names or link URLs into translated text. Variable substitution looks like this:

    $text = t("@name's blog", array(
      '@name' => format_username($account),
    ));
    

    Basically, you can put variables like @name into your string, and t() will substitute their sanitized values at translation time. (See the Localization API pages referenced above and the documentation of format_string() for details about how to define variables in your string.) Translators can then rearrange the string as necessary for the language (e.g., in Spanish, it might be "blog de @name").


    So what you could do instead, is to use Entity Translation to make these fields translatable. You'll then be able to translate your content and the correct translation will get printed based on what value was added to the field in the matching language.


    Or to use i18n_string(), see https://drupal.stackexchange.com/a/184584