harttle / liquidjs

A simple, expressive, safe and Shopify compatible template engine in pure JavaScript.
https://liquidjs.com
MIT License
1.52k stars 238 forks source link

Date locale #567

Closed flaviotordini closed 3 months ago

flaviotordini commented 1 year ago

Is it possibile to format dates with the date filter in a specific language? If not, it sounds like a good feature for a templating engine.

harttle commented 1 year ago

We have problem in JavaScript that Date doesn't support format string. We'll need localizations for this purpose, I'm thinking that we can create a separate project to port a strftime lib to Liquid date filter. like this one: https://github.com/samsonjs/strftime#locales

pdehaan commented 1 year ago

JavaScript does have an Intl.DateTimeFormat, which "enables language-sensitive date and time formatting."

flaviotordini commented 1 year ago

In the meantime I used this:

    eleventyConfig.addFilter("localizedDate", (dateString, locale) => {
        const options = { year: 'numeric', month: 'long', day: 'numeric' };
        return new Date(dateString).toLocaleDateString(locale, options);
    });

The downside is that it does not support string patterns.

harttle commented 1 year ago

Yes, you're right. We can customize locale date/time string in JavaScript, but not as flexible as in strftime.

Intl.DateTimeFormat and .toLocaleDateString() both support a format options parameter which contains something like dateStyle, timeStyle, year, month. While I can't find a way to implement strftime with it.

prassie commented 1 year ago

@harttle would an option for global/default date format help, similar to timezoneOffset? In current code, the default format is hard coded. With such an option, engine users will be free to set whatever format they need, however they got it (Intl, user configured, ...).

I use the similar timezoneOffset option, to get all dates in the templates converted to the required tz.

Of course, this is for the developers who setup/init the Liquid engine, and not for the template designers.

harttle commented 1 year ago

Makes sense. Thank you @prassie for the clarification.

harttle commented 3 months ago

weekday and months are now locale specific, details please check: https://liquidjs.com/filters/date.html