forked from otwcode/otwarchive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation_helper.rb
69 lines (61 loc) · 2.46 KB
/
translation_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module TranslationHelper
# Enigel Nov. 15 08
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
case distance_in_minutes
when 0..1
return (distance_in_minutes==0) ? 'less than 1 minute' : '1 minute' unless include_seconds
case distance_in_seconds
when 0..5 then "less than 5 seconds"
when 6..10 then "less than 10 seconds"
when 11..20 then "less than 20 seconds"
when 21..40 then "half a minute"
when 41..59 then "less than a minute"
else "1 minute"
end
when 2..45 then "#{distance_in_minutes} minutes"
when 46..90 then "1 hour"
when 90..1440 then "#{(distance_in_minutes.to_f / 60.0).round} hours"
when 1441..2880 then "1 day"
else "#{(distance_in_minutes / 1440).round} days"
end
end
def time_ago_in_words(from_time, include_seconds = false)
to_time = Time.now
if from_time.respond_to?(:to_time)
from_time = from_time.to_time
else
return
end
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
case distance_in_minutes
when 0..1
return (distance_in_minutes==0) ? 'less than 1 minute ago' : '1 minute ago' unless include_seconds
case distance_in_seconds
when 0..5 then "less than 5 seconds ago"
when 6..10 then "less than 10 seconds ago"
when 11..20 then "less than 20 seconds ago"
when 21..40 then "half a minute ago"
when 41..59 then "less than a minute ago"
else "1 minute ago"
end
when 2..45 then "#{distance_in_minutes} minutes ago"
when 46..90 then "1 hour ago"
when 90..1440 then "#{(distance_in_minutes.to_f / 60.0).round} hours ago"
when 1441..2880 then "1 day ago"
else "#{(distance_in_minutes / 1440).round} days ago"
end
end
alias distance_of_time_in_words_to_now time_ago_in_words
# Take some text and add whatever punctuation, symbols, and/or spacing
# we use to separate a metadata property from its value, e.g., "Property: ",
# "Propriété : ".
def metadata_property(text)
text.html_safe + t("mailer.general.metadata_label_indicator")
end
end