105 lines
2.6 KiB
JavaScript
105 lines
2.6 KiB
JavaScript
export default ((locale) => {
|
|
const self = {}
|
|
|
|
if (locale === undefined) {
|
|
self.locales = {
|
|
pastPrefix: '',
|
|
pastSufix: 'ago',
|
|
|
|
futurePrefix: 'in',
|
|
futureSufix: '',
|
|
|
|
seconds: '%p less than a minutei %s',
|
|
minute: '%p about a minute %s',
|
|
minutes: '%p %d minutes %s',
|
|
hour: '%p about an hour %s',
|
|
hours: '%p about %d%r hours %s',
|
|
day: '%p a day %s',
|
|
days: '%p %d%r days %s',
|
|
month: '%p about a month %s',
|
|
months: '%p %d%r months %s',
|
|
year: '%p about a year %s',
|
|
years: '%p %d%r years %s'
|
|
}
|
|
} else {
|
|
self.locales = locale
|
|
}
|
|
|
|
self.inWords = (timeDistance, start) => {
|
|
if (start === undefined) {
|
|
start = new Date()
|
|
} else {
|
|
start = start instanceof(Date) ? start : parseInt(start)
|
|
}
|
|
timeDistance = timeDistance instanceof(Date) ? timeDistance : parseInt(timeDistance)
|
|
const prefix = timeDistance < new Date() ? self.locales.pastPrefix : self.locales.futurePrefix
|
|
const sufix = timeDistance < new Date() ? self.locales.pastSufix : self.locales.futureSufix
|
|
|
|
let seconds = Math.abs(Math.floor((start - timeDistance) / 1000)),
|
|
interval = 0,
|
|
rest = 0,
|
|
intervals = {
|
|
year: seconds / 31536000,
|
|
month: seconds / 2592000,
|
|
day: seconds / 86400,
|
|
hour: seconds / 3600,
|
|
minute: seconds / 60
|
|
}
|
|
|
|
let distance = self.locales.seconds
|
|
let key
|
|
|
|
for (key in intervals) {
|
|
interval = Math.floor(intervals[key])
|
|
rest = intervals[key] - interval
|
|
|
|
if (interval >= 1) {
|
|
distance = self.locales[key + 's']
|
|
break
|
|
} else if (interval === 1) {
|
|
distance = self.locales[key]
|
|
break
|
|
}
|
|
}
|
|
|
|
if (rest > 0.5) {
|
|
distance = distance.replace(/%r/, "½")
|
|
} else if (rest > 0.25) {
|
|
distance = distance.replace(/%r/, "¼")
|
|
} else {
|
|
if (interval === 1) {
|
|
distance = self.locales[key]
|
|
}
|
|
distance = distance.replace(/\s*%r/, "")
|
|
}
|
|
|
|
distance = distance.replace(/%d/i, interval)
|
|
distance = distance.replace(/%p/i, prefix)
|
|
distance = distance.replace(/%s/i, sufix)
|
|
|
|
return distance.trim()
|
|
}
|
|
|
|
return self
|
|
})
|
|
|
|
export const deLocale = {
|
|
pastPrefix: 'seit',
|
|
pastSufix: '',
|
|
|
|
futurePrefix: 'in',
|
|
futureSufix: '',
|
|
|
|
seconds: '%p %d Sekunden',
|
|
minute: '%p einer Minute',
|
|
minutes: '%p %d Minuten',
|
|
hour: '%p einer Stunde',
|
|
hours: '%p %d%r Stunden',
|
|
day: '%p einem Tag',
|
|
days: '%p %d%r Tagen',
|
|
month: '%p einem Monat',
|
|
months: '%p %d%r Monaten',
|
|
year: '%p einem Jahr',
|
|
years: '%p %d%r Jahren'
|
|
}
|