In my Rails 5 app I am using Liquid to let my users generate content.
Based on my users input, I init my template with something like this:
string = "Order {{ order.id }} was created {{ order.date | date: '%A %d/%m-%Y' }}"
template = Liquid::Template.parse(string)
result = template.render({'order' => {'id' => '123', 'date' => order.date}})
This would print something a la:
'Order 123 was created Sunday 14/01-2018'
But how do I build Liquid date localization into my Rails app?
It does not seem to be supported in the documentation. However Shopify themselves seems to have build localization into their Liquid implementation.
I suppose I would need to pass my template a locale (en
, fr
, etc.) and a locale file. My Rails locale file looks like this:
en:
datetime: &datetime
month_names:
[~, January, February, March, April, May, June, July, August, September, October, November, December]
day_names:
[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
abbr_day_names:
[~, Sun, Mon, Thue, Wed, Thu, Fri, Sat]
formats:
default: "%d/%m/%Y"
long: "%A %d/%m-%Y"
And I call it with:
l(order.date, :format => :long, :locale => 'en')
I would love to have access to a similar date localization inside my Liquid template.
I18n.localize
? stackednotion.com/blog/2015/01/03/… seems relevant - it shows how to deliberately pass in a locale and use a customised date format (but note I've spent like maybe 5 whole seconds thinking about this...) – Gainsl(order.date, :format => :long, :locale => 'en')
. I'm gonna write it in as an answer if no one else comes up with a better solution. But it's not a perfect one, because it limits me to a set of predefinedformats
. – Worser