Liquid
FAQ - Liquid

FAQ - How can I translate and format dates?

4min
a demo of how you can convert dates from english to a different language answer we can use liquid to format our "raw" date integer to a formatted date however, this doesn't give the option to select a different language when outputting the date here is one example of how you can do this using liquid translate the months first, we will need to create an object of months converted from english to whichever language you'd like {% parse json month map %} { "fre" { "january" "januar", "february" "février", "march" "mars", "april" "avril", "may" "mai", "june" "juin", "july" "juillet", "august" "août", "september" "septembre", "october" "octobre", "november" "novembre", "december" "décembre" }, "ger" { "january" "januar" } } {% endparse json %} now we have an object called "months" storing all the translated months, simply change "fre" and the translation to whichever language you'd like search the months now we've translated the months we'll need to check which one needs to be outputted firstly assign a variable with the month you'd like to translate, i'd like to do so with my items "release date" {% assign current month = this\['release date'] | date "%b" %} we use the "date" filter here to format the "raw" date integer to a humanized date this follows ruby's strf format, read here for more info on formatting dates we'll use "%b", as this will store the month as a string now specify which language in "month map" you're using, i've chosen french {% assign language = "fre" %} next, we'll use these two variables to search the "month map" and return the translated month {% assign translated month = month map\[language]\[current month] %} format the translated month and store in a variable now we've found the translated month we'll need to format it into a complete date, and then store this so it can be outputted {% assign date = this\['release date'] | date "%d" %} {% assign year = this\['release date'] | date "%y" %} {% assign date complete = date | append " " | append translated month %} {% assign date complete = date complete | append " " | append year %} we assign the date and year, formatting them into integer dates using the "date" filter, then we append the finished date to the variable "date complete", you can output this like so {{date complete}}