codebox / moment-precise-range

A moment.js plugin to display human-readable date/time ranges
https://codebox.net/pages/moment-date-range-plugin
MIT License
150 stars 91 forks source link

i18n #6

Open Garito opened 9 years ago

Garito commented 9 years ago

Hi! I would like to use your library with internationalization so I was digging the code

Did you notice that moment by itself has the strings your code needs to make the precise range?

Would be nice to make a deeper integration with the dependency and it will gain automatic translation

Are you interested on this issue?

Thanks!!!

codebox commented 9 years ago

Hi, When I was writing the plugin I looked through the moment.js code to see if it had all the strings I needed, I could find some but not all of them. It would be a much better solution if I could use the built-in strings, for i18n as you describe. Are you sure they are all there (including plurals ie hour/hours etc), if so can you point me to them? many thanks Rob

Garito commented 9 years ago

Sure! You could use _relativeTime. The internationalization mechanism use it to do so

The actual line: https://github.com/moment/moment/blob/develop/moment.js#L947

Cheers!

codebox commented 9 years ago

Yes, I think I saw that when I was looking over the moment.js code - it's close to what I would need, but doesn't quite cover everything. There is no string for '1 second' (the closest is 'a few seconds') and also the singular phrases for minutes, hours etc all use 'a' or 'an' rather than '1' - I think it would look odd if the plugin produced output that was a mixture, like '2 hours a minute 3 seconds' rather than '2 hours 1 minute 3 seconds'

Garito commented 9 years ago

Would be nice to make it very configurable for this kind of things I think that the correct way to act it to ask to the monents creators to add the few options you miss I found you by their website, I think they will be ok with your needs

nlaplante commented 9 years ago

:+1: for this.

miguelcobain commented 9 years ago

:+1:

dellheng commented 8 years ago

:+1:

dbrgn commented 8 years ago

:+1:

ZalemCitizen commented 8 years ago

:+1:

codebox commented 8 years ago

I have added a returnValueObject parameter to the preciseDiff methods, setting this value to true will cause the methods to return a value object containing the numeric results of the diff calculation. Hopefully this should help people who require internationalisation - they can let the library do the maths and use their own language files to turn the result into words.

IvanRF commented 8 years ago

@codebox it's a pity you can't reuse relativeTime, you almost have all translations there:

relativeTime : {
            future : 'in %s',
            past : '%s ago',
            s : 'a few seconds',
            m : 'a minute',
            mm : '%d minutes',
            h : 'an hour',
            hh : '%d hours',
            d : 'a day',
            dd : '%d days',
            M : 'a month',
            MM : '%d months',
            y : 'a year',
            yy : '%d years'
},

What if you suggest to add ss : '%d seconds', to moment.js?

TWiStErRob commented 6 years ago

@IvanRF you mean like this? https://github.com/moment/moment/pull/4183

borisBelloc commented 4 years ago

can someone tell me if there is a solution about this issue ?

I'm trying to translate the result of moment("2020-03-17 12:00:00").preciseDiff(moment());

17 days 2 hours 20 minutes 36 seconds

to french but I don't find how to do it...

Every thread refer to #6 but I don't see any tips about it :(

I "found' those lines in the moment-precise-range.js :

    var STRINGS = {
        nodiff: '',
        year: 'year',
        years: 'years',
        month: 'month',
        months: 'months',
        day: 'day',
        days: 'days',
        hour: 'hour',
        hours: 'hours',
        minute: 'minute',
        minutes: 'minutes',
        second: 'second',
        seconds: 'seconds',
        delimiter: ' '
    };

helas, I have no idea how to overide it

codebox commented 4 years ago

@borisBelloc probably best you can do is to call preciseDiff(moment(), true) which will give you an object back with the numeric values for seconds, minutes, hours etc and then build up the string yourself

borisBelloc commented 4 years ago

thanks for the fast answer 👍 👍

ugurcanalyuz commented 1 year ago

I wrote in Turkish, but this code will work.

const append = () => {
   const times = moment.preciseDiff(text, new moment(), true)
   let returnText = ''
   if (times.days > 0) {
      returnText = `${times.days} Gün, ${times.hours} Saat, ${times.minutes} Dakika`
   } else if (times.hours > 0) {
      returnText = `${times.hours} Saat, ${times.minutes} Dakika`
   } else if (times.minutes > 0) {
      returnText = `${times.minutes} Dakika, ${times.seconds} Saniye`
   } else {
      returnText = `Son Dakika Teklif Verilemez`
   }
   return returnText
}