jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
968 stars 121 forks source link

Support locale/humanize #16

Closed miguelcobain closed 9 years ago

miguelcobain commented 10 years ago

Could this plugin support humanize()? It would be awesome if we could use momentjs i18n support: http://momentjs.com/docs/#/i18n/

Or is it up to us to provide a template that uses i18n?

jsmreese commented 10 years ago

Not exactly sure what you mean.

Native moment durations have the humanize method. And humanize(true) which is analogous to the moment.fromNow method. http://momentjs.com/docs/#/durations/humanize/

If you're using this plugin, you can provide whatever template string you want, in whatever language.

The default template function is not exactly i18n friendly (but you could provide your own). The use of the decimal point (.) for fractional units output is not configurable.

Are you looking for the already-existing duration humanize method, or complete i18n support from this plugin?

miguelcobain commented 10 years ago

Thanks for your reply.

Basically, this is my problem: https://github.com/moment/moment/issues/348

I need a precise version of humanize, that's why I used this plugin. Where do you think this fits?

jsmreese commented 10 years ago

OK, got it.

I'll take a look at everything. I'm not sure where this fits. It seems like this plugin handles half of what you need -- would be nice if it played nicely with Moment's existing i18n features.

jsmreese commented 9 years ago

Took a look at this today and I have a bunch of questions.

In the current version of my plugin, you could supply a localized format template, and you could customize the format tokens to match what users might expect in their language, e.g. A or a for años in Spanish.

The only thing you can't change is the decimal point separator ., which moment.js doesn't capture in its locale definitions.

I could auto-detect the decimal point separator based on the environment using Number.prototype.toLocaleString (see http://stackoverflow.com/questions/1074660/with-a-browser-how-do-i-know-which-decimal-separator-does-the-client-use) and allow override via a new decimalSeparator option for my plugin.

That would be pretty simple and I think it makes sense.

Do you also want a new addition to the format grammar to be able to write format template strings that automatically reference the moment locale relativeTime strings?

Something along the lines of this:

moment.locale('es');

moment.duration(257.5, "minutes").format("<h>, <m>");
// "4 horas, 17 minutos"

moment.duration(257.5, "minutes").format("<h>, <m>, <s>");
// "4 horas, 17 minutos, 30 segundos"

...except that I just noticed that for locale es, relativeTime.s is 'unos segundos' and not '%d segundos', and this looks to hold true across locales.

Also, it seems like you might sometimes want the output from longDateFormat.LT and longDateFormat.LTS but without the meridiem component.

I'm having trouble coming up with a version of this feature that is simpler/easier/better than either supplying your own localized format template string to my plugin or using the precise-<locale> solution proposed here: https://github.com/moment/moment/issues/348#issuecomment-6535794

What do you think?

jsmreese commented 9 years ago

I've added the decimalSeparator option, so you can now localize the format grammar, the default format template function, and the decimal separator used in the output. You can provide your own localized template as well.

Closing this for now because I can't figure out how to use the moment.js localization strings for duration formatting in this plugin in a way that makes sense.

yvele commented 8 years ago

Sorry for bumping but any news on this issue (and also https://github.com/moment/moment/issues/348 that have a lot of +1) ?

BTW: HumanizeDuration package works pretty well to humanize duration (with local), but it would be nice to have this feature in moment.