4

I created a custom date format j F Y at Configuration » Regional and language » Date and time » Formats. I'm pleased with the overall look, but I'd like the months to be written in Dutch instead of the default English. The month names should be translated everywhere they occur and it would be perfect if the days should be translated too (maybe I'll need them in the future).

How can I do this?

2

This is a lot harder to do than I first thought. Formatted dates, including strings for the month (and day) names are produced by format_date(). This function is not themable, and there is no hook to override it.

That means that (AFAIK) there is no right way to change how this function behaves (the wrong way is to hack it - i.e. hack core - which is not an option in my book).

What we can do is to make a tiny custom module with a version of this function that outputs months and days in Dutch, no matter what the default language for the site is. This function will do this:

function MYMODULE_format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) {
  return format_date($timestamp, $type, $format, $timezone, 'nl');
}

(Where MYMODULE is the name of your custom module.)

Now, if you set the default language of your site to English, but keep the Dutch translation available, one of the following calls:

MYMODULE_format_date($timestamp, $type);
MYMODULE_format_date($timestamp, 'custom', 'j F Y');

(where $timestamp is the timestamp to print and $type is the format to use) will output names of months and days in Dutch, but everything else will be in your site's default language. The first will do this for any valid format (Long, Medium, Short, etc.), the second for the custom format 'j F Y'.

This will give you Dutch month names if you call this custom function instead of the standard one when outputting formatted dates.

However, it will not change the output in existing code which of course uses the standard version of the function. This may not be a problem for you, since you want to use it with a custom date format (which obviously is not in use in legacy code), but I still want to make that clear.

If you want to change how months and days are spelled everywhere, you need to override the templates node.tpl.php and comment.tpl.php (and possibly other templates that uses format_date). In the override, just replace any calls to format_date with calls to MYMODULE_format_date.

| improve this answer | |
  • Any Way to do same thing in Drupal 8 ? – Pankaj Yogi Oct 8 '18 at 12:28
2

For a simple case of the publishing date info, it's enough to alter your node.tpl.php following the lead presented here. In order to get translated months you need to push the month name (or the weekday name if you use it) through the t function. So my modified bartik-based node.tpl.php has

  <?php echo "<span class='custom_date custom_day'> " . date( "j",$node->created) . "</span>";
  echo "<span class='custom_date custom_month'> " . t(date( "F",$node->created)) . "</span>";
  echo "<span class='custom_date custom_month'> " . (date( "Y",$node->created)) . "</span>"; ?>

instead of a

<?php print $submitted; ?>
| improve this answer | |

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.