2

I want to change the "submitted by" field. I want to show the date in another language. When I use views to display dates, the date shown is in the correct language.

When I add this to node.tpl.php, the date is in English.

echo date("j F Y", $node->created);

What is the correct way in Drupal to show the date like "17 december 1978" when the language set in Drupal is Dutch?

3 Answers 3

0

You need to format the date correctly using Drupals format_date() function.

    print format_date(time(), 'custom', 'l j F Y');

http://api.drupal.org/api/drupal/includes%21common.inc/function/format_date/7

format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)

The $langcode argument is the language code to translate to. Defaults to the language used to display the page.

10
  • Doesn't work for a reason. The translations of the month names are available at admin/config/regional/translate/translate. But it doesn't work the way you describe. Do I have to translate the month seperatly? Jan 22, 2013 at 14:29
  • Does appear that way... stackoverflow.com/questions/10895856/…
    – Alex Gill
    Jan 22, 2013 at 14:30
  • 1
    I was looking at the views source code and found this: return format_date($value, $format, $custom_format, $timezone); That will do the trick! Jan 22, 2013 at 14:39
  • 1
    Ok, thanks, will try to gather more info for the answer.
    – Alex Gill
    Jan 22, 2013 at 14:42
  • 1
    Passing the output of date() to t() doesn't get the date translated. t() doesn't split the string passed as argument in separated sub-string; even if there is the translation of the month name, t() is not able to translate (even minimally) a string like "January is my favorite month." if the translation of "January is my favorite month." is not provided. It is quite impossible somebody provided the translation of every date in every format.
    – apaderno
    Jan 23, 2013 at 11:03
2

Month names are not added automatically.

An elegant way to do this is described here:

https://drupal.org/comment/8251325#comment-8251325

function monthname_install() {
  $months = array(
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December',
  );

  $options['context'] = 'Long month name';

  foreach($months as $m) {
    t($m, array(), $options);
  }

  drupal_set_message(t('Month names have been made available for translation. The rest is up to the translation team :-)'));
}
1

The correct function to use is format_date().

Keep in mind that, when you don't pass the $langcode parameter, it uses the language currently set in the global $language. If that is not the correct value, you could try using the following code.

echo format_date($node->created, 'custom', "j F Y", NULL, $GLOBALS['language_content']->language);

format_date() translates the strings used for the following format characters: A, a, e, D, l, M, T, and F. (In other words, the name of the months, and the weekdays are translated.)

The code that translates those formats is the following one.

  // Encode markers that should be translated. 'A' becomes '\xEF\AA\xFF'.
  // xEF and xFF are invalid UTF-8 sequences, and we assume they are not in the
  // input string.
  // Paired backslashes are isolated to prevent errors in read-ahead evaluation.
  // The read-ahead expression ensures that A matches, but not \A.
  $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("\xEF\\\\\\\\\xFF", "\xEF\\\\\$1\$1\xFF"), $format);

  // Call date_format().
  $format = date_format($date_time, $format);

  // Pass the langcode to _format_date_callback().
  _format_date_callback(NULL, $langcode);

  // Translate the marked sequences.
  return preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', '_format_date_callback', $format);

format_date() wraps the format characters it can translate with two characters that allows format_date() to find the replaced string, calls date_format(), then translates the strings wrapped with those two characters it added.
The code of _format_date_callback() is then the following one.

  // We cache translations to avoid redundant and rather costly calls to t().
  static $cache, $langcode;

  if (!isset($matches)) {
    $langcode = $new_langcode;
    return;
  }

  $code = $matches[1];
  $string = $matches[2];

  if (!isset($cache[$langcode][$code][$string])) {
    $options = array(
      'langcode' => $langcode,
    );

    if ($code == 'F') {
      $options['context'] = 'Long month name';
    }

    if ($code == '') {
      $cache[$langcode][$code][$string] = $string;
    }
    else {
      $cache[$langcode][$code][$string] = t($string, array(), $options);
    }
  }
  return $cache[$langcode][$code][$string];

Notice that using t(date("j F Y", $node->created)) would only work if somebody called t() as t('23 January 2013'), and made a call for each possible date.

0

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.