6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/time_ago.rb', line 6
def self.format(time, locale: DEFAULT_LOCALE)
I18n.load_path << Dir[File.expand_path("locales/*.yml", __dir__)]
I18n.default_locale = DEFAULT_LOCALE
I18n.locale = locale
distance_in_seconds = (Time.current - time).to_i
if distance_in_seconds < 5
I18n.t("time_ago.just_now")
elsif distance_in_seconds < 60
I18n.t("time_ago.seconds_ago", count: distance_in_seconds)
elsif distance_in_seconds < 3600
minutes_ago = (distance_in_seconds / 60).to_i
I18n.t("time_ago.minutes_ago", count: minutes_ago)
elsif distance_in_seconds < 86400
hours_ago = (distance_in_seconds / 3600).to_i
I18n.t("time_ago.hours_ago", count: hours_ago)
elsif distance_in_seconds < 172_800
I18n.t("time_ago.yesterday_at", time: I18n.l(time, format: :time))
else
I18n.t("time_ago.other", time: I18n.l(time, format: :short))
end
end
|