icambron / twix.js

:hourglass::left_right_arrow: A date range plugin for moment.js
https://isaaccambron.com/twix.js/
MIT License
379 stars 54 forks source link

Internationalization formatting day/month swap #83

Closed arm5472 closed 7 years ago

arm5472 commented 8 years ago

Great library, Isaac, many thanks. On the internationalization topic, twix uses the moment locale's settings by default, but not on the order of the day/month.

moment().locale('us').format('LLL'); //=> "February 1, 2016 5:30 PM" moment().locale('en-gb').format('LLL'); //=> "1 February 2016 17:30"

With twix: moment.locale('en-gb'); moment('2017-02-01').twix('2017-02-01',{allDay: true}).format(); => "Feb 1, 2017"

But I believe it should be: "1 Feb 2017"

Many thanks

Amir

icambron commented 8 years ago

Yes, it absolutely should. However, I haven't been able to figure out a good way to make that work. There's some complicated logic in there to figure out what fields to "collapse" for ranges (in your example, all of the fields have collapsed so that it's just one date). One trick would be to decompose "LLL" and use the ordering of its tokens to pick the collapse order of the fields using that. I played around with that and found it too complicated. Another approach would be to have language-specific files that alter the logic; I actually implemented that, but without a whole range of implementations for different languages (I just had French), it was pointless.

The net result of all that is that I'd love to fix it but I'm not really sure how.

jymbob commented 8 years ago

Having a quick scan through the source, would it be possible to start by coding some default slot orders for different locales? I'm assuming standard is 0,1,2,3,4,5,6 en-gb would be (I think) 0,2,1,3,4,5,6

icambron commented 8 years ago

@jymbob we used to have code like this, which is the same idea. It went for a couple years without having anything other than French, so I eventually canned it. I'm a little wary of bringing it back, especially if it'll just be another ghost town.

What I'd really like to do is derive the slot order by taking the LLL Moment token into its actual format and processing that. E.g. from the Moment en-gb locale file:

LLL : 'D MMMM YYYY HH:mm',

which tells me that date comes first, then month, then year. It should be possible to use that to generate the slots. There's a very old fork here where I experimented with doing just that. I got frustrated with it an gave up, though I don't remember what the challenges were.

An alternative would be make it easier for the user to just specify the slot order. I'd take a PR that did that.

timbrandin commented 8 years ago

86

icambron commented 8 years ago

See #89 for the current proposal.

icambron commented 7 years ago

I merged #89, so closing this.

andreyrd commented 5 years ago

Can we add an option to force (non-)americanish formatting without including an entire locale? A sort of override? I want to swap month and day, but I'm still using the standard "en" locale.