Internationalising date strings in Craft

When working with Craft CMS and translating strings, you might need to translate dates as well. After all, countries format these differently. For example, while the US often uses "month day year" we in Norway use "day month year". Other differences can be wether they use dashes, slashes or just completely different date formatting all together. In any case, dates need to be translated as well.

Unfortunately you can't manipulate variables in translation strings, so my suggestion is to do the following in twig, using now as an example of a date you want to display:

{{ now|date('m.d.Y'|t) }}

While adding the following to your translation files:

"m.d.Y" => "d.m.Y"

Of course, this depends on the standards you follow. Some may prefer this type of markup:

{{ now|date('DATE_FORMAT'|t) }}

In which case you would have the following in your translation file:

"DATE_FORMAT" => "d.m.Y"

Using this in a string would look something like this:

{{ "This post was last updated {date}"|t({ date: entry.dateUpdated|date('DATE_FORMAT'|t) }) }}

You can also define multiple date formats if you have several different types of date formats, shorter or longer, throughout your templates.