How to translate Date Formats on Drupal 7?

10,217

You can use the drupal 7 t() function: http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/t/7

Anything that goes through the t() function has to be manually translated in the back-end Configuration > Regional and language > Translate interface. That means you'll have to manually put in the translations for the days and months.

Sample code:

<?php
    global $language;

    $day = t(date('l'));
    $month = t(date('F'));
    $dayNum = date('j');
    $year = date('Y');

    $output = t('@day, @month @dayNum, @year', array(
        '@day' => $day,
        '@month' => $month, 
        '@dayNum' => $dayNum, 
        '@year' => $year
    ));

    print ("<p class='today'>".$output."</p>");
?>
Share:
10,217
Jorge Suárez de Lis
Author by

Jorge Suárez de Lis

Updated on June 04, 2022

Comments

  • Jorge Suárez de Lis
    Jorge Suárez de Lis almost 2 years

    On admin/config/regional/date-time/formats I can set up date formats, like M jS, and map them to date types on admin/config/regional/date-time, like "Short format date".

    Now I want to set up a multilanguage website, so I not only have to translate the month or week names itself, but choose a different date format based on language. That is, Apr 12th is OK on English, but translating Apr to Abr won't make it much better on Spanish: Abr 12th --should be 12 Abr. I need to translate also the format itself, so instead of M jS, the Spanish version of the website will use j M.

    How can I achieve this? I tried searching on admin/config/regional/translate/translate for these date formats. The Apr->Abr part is covered there, but not the format itself. I can't find strings like M jS and so.