moment-precise-range icon indicating copy to clipboard operation
moment-precise-range copied to clipboard

i18n

Open Garito opened this issue 10 years ago • 16 comments

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!!!

Garito avatar Nov 21 '14 11:11 Garito

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

codebox avatar Nov 22 '14 08:11 codebox

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!

Garito avatar Nov 22 '14 09:11 Garito

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'

codebox avatar Nov 22 '14 11:11 codebox

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

Garito avatar Nov 22 '14 13:11 Garito

:+1: for this.

nlaplante avatar May 05 '15 15:05 nlaplante

:+1:

miguelcobain avatar Jul 06 '15 15:07 miguelcobain

:+1:

dellheng avatar Nov 23 '15 09:11 dellheng

:+1:

dbrgn avatar Dec 14 '15 21:12 dbrgn

:+1:

ZalemCitizen avatar Mar 07 '16 23:03 ZalemCitizen

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.

codebox avatar Apr 07 '16 09:04 codebox

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

IvanRF avatar Jun 05 '16 22:06 IvanRF

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

TWiStErRob avatar Dec 09 '17 12:12 TWiStErRob

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

borisBelloc avatar Apr 03 '20 13:04 borisBelloc

@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

codebox avatar Apr 03 '20 13:04 codebox

thanks for the fast answer 👍 👍

borisBelloc avatar Apr 03 '20 13:04 borisBelloc

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
}

xcandev avatar Jan 29 '23 18:01 xcandev