liquidjs icon indicating copy to clipboard operation
liquidjs copied to clipboard

Date locale

Open flaviotordini opened this issue 2 years ago • 6 comments
trafficstars

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.

flaviotordini avatar Dec 11 '22 15:12 flaviotordini

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

harttle avatar Dec 11 '22 17:12 harttle

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

pdehaan avatar Dec 11 '22 20:12 pdehaan

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.

flaviotordini avatar Dec 11 '22 21:12 flaviotordini

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.

harttle avatar Dec 12 '22 11:12 harttle

@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.

prassie avatar Feb 02 '23 18:02 prassie

Makes sense. Thank you @prassie for the clarification.

harttle avatar Feb 04 '23 03:02 harttle

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

harttle avatar Jul 21 '24 16:07 harttle