dotiw 5.1.0 → 5.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +8 -8
- data/CHANGELOG.md +21 -0
- data/README.markdown +26 -1
- data/benchmarks/time_hash_bench.rb +19 -0
- data/lib/dotiw/action_view/helpers/date_helper.rb +27 -7
- data/lib/dotiw/locale/ar.yml +54 -3
- data/lib/dotiw/locale/da.yml +35 -0
- data/lib/dotiw/locale/de.yml +36 -1
- data/lib/dotiw/locale/en.yml +35 -0
- data/lib/dotiw/locale/es.yml +36 -1
- data/lib/dotiw/locale/fr.yml +35 -0
- data/lib/dotiw/locale/id.yml +35 -0
- data/lib/dotiw/locale/it.yml +35 -0
- data/lib/dotiw/locale/ja.yml +35 -0
- data/lib/dotiw/locale/ko.yml +23 -0
- data/lib/dotiw/locale/nb.yml +36 -1
- data/lib/dotiw/locale/nl.yml +35 -0
- data/lib/dotiw/locale/pl.yml +35 -0
- data/lib/dotiw/locale/pt-BR.yml +35 -0
- data/lib/dotiw/locale/ru.yml +37 -0
- data/lib/dotiw/locale/sv.yml +48 -0
- data/lib/dotiw/locale/vi.yml +23 -0
- data/lib/dotiw/locale/zh-CN.yml +23 -0
- data/lib/dotiw/locale/zh-TW.yml +60 -0
- data/lib/dotiw/methods.rb +22 -7
- data/lib/dotiw/time_hash.rb +20 -14
- data/lib/dotiw/version.rb +1 -1
- data/lib/dotiw.rb +2 -1
- data/spec/lib/dotiw_spec.rb +171 -36
- data/spec/lib/i18n/sv.yml +2 -0
- data/spec/lib/i18n/zh-TW.yml +2 -0
- metadata +10 -3
data/lib/dotiw/locale/nl.yml
CHANGED
@@ -23,3 +23,38 @@ nl:
|
|
23
23
|
one: 1 jaar
|
24
24
|
other: "%{count} jaar"
|
25
25
|
less_than_x: "minder dan %{distance}"
|
26
|
+
dotiw_compact:
|
27
|
+
seconds:
|
28
|
+
one: 1s
|
29
|
+
other: "%{count}s"
|
30
|
+
minutes:
|
31
|
+
one: 1m
|
32
|
+
other: "%{count}m"
|
33
|
+
hours:
|
34
|
+
one: 1u
|
35
|
+
other: "%{count}u"
|
36
|
+
days:
|
37
|
+
one: 1d
|
38
|
+
other: "%{count}d"
|
39
|
+
weeks:
|
40
|
+
one: 1w
|
41
|
+
other: "%{count}w"
|
42
|
+
months:
|
43
|
+
one: 1ma
|
44
|
+
other: "%{count}ma"
|
45
|
+
years:
|
46
|
+
one: 1j
|
47
|
+
other: "%{count}j"
|
48
|
+
less_than_x: "<%{distance}"
|
49
|
+
words_connector: ""
|
50
|
+
two_words_connector: ""
|
51
|
+
last_word_connector: ""
|
52
|
+
about_x_years:
|
53
|
+
one: ~1j
|
54
|
+
any: "~%{count}j"
|
55
|
+
over_x_years:
|
56
|
+
one: ">1j"
|
57
|
+
other: ">%{count}j"
|
58
|
+
almost_x_years:
|
59
|
+
one: ~1j
|
60
|
+
any: "~%{count}j"
|
data/lib/dotiw/locale/pl.yml
CHANGED
@@ -37,3 +37,38 @@ pl:
|
|
37
37
|
many: "%{count} lat"
|
38
38
|
other: "%{count} lat"
|
39
39
|
less_than_x: "mniej niż %{distance}"
|
40
|
+
dotiw_compact:
|
41
|
+
seconds:
|
42
|
+
one: 1s
|
43
|
+
other: "%{count}s"
|
44
|
+
minutes:
|
45
|
+
one: 1m
|
46
|
+
other: "%{count}m"
|
47
|
+
hours:
|
48
|
+
one: 1g
|
49
|
+
other: "%{count}g"
|
50
|
+
days:
|
51
|
+
one: 1d
|
52
|
+
other: "%{count}d"
|
53
|
+
weeks:
|
54
|
+
one: 1t
|
55
|
+
other: "%{count}t"
|
56
|
+
months:
|
57
|
+
one: 1mi
|
58
|
+
other: "%{count}mi"
|
59
|
+
years:
|
60
|
+
one: 1r
|
61
|
+
other: "%{count}l"
|
62
|
+
less_than_x: "<%{distance}"
|
63
|
+
words_connector: ""
|
64
|
+
two_words_connector: ""
|
65
|
+
last_word_connector: ""
|
66
|
+
about_x_years:
|
67
|
+
one: ~1r
|
68
|
+
any: "~%{count}r"
|
69
|
+
over_x_years:
|
70
|
+
one: ">1r"
|
71
|
+
other: ">%{count}r"
|
72
|
+
almost_x_years:
|
73
|
+
one: ~1r
|
74
|
+
any: "~%{count}r"
|
data/lib/dotiw/locale/pt-BR.yml
CHANGED
@@ -23,3 +23,38 @@ pt-BR:
|
|
23
23
|
one: 1 ano
|
24
24
|
other: "%{count} anos"
|
25
25
|
less_than_x: "menos de %{distance}"
|
26
|
+
dotiw_compact:
|
27
|
+
seconds:
|
28
|
+
one: 1s
|
29
|
+
other: "%{count}s"
|
30
|
+
minutes:
|
31
|
+
one: 1m
|
32
|
+
other: "%{count}m"
|
33
|
+
hours:
|
34
|
+
one: 1h
|
35
|
+
other: "%{count}h"
|
36
|
+
days:
|
37
|
+
one: 1d
|
38
|
+
other: "%{count}d"
|
39
|
+
weeks:
|
40
|
+
one: 1s
|
41
|
+
other: "%{count}s"
|
42
|
+
months:
|
43
|
+
one: 1mê
|
44
|
+
other: "%{count}mê"
|
45
|
+
years:
|
46
|
+
one: 1a
|
47
|
+
other: "%{count}a"
|
48
|
+
less_than_x: "<%{distance}"
|
49
|
+
words_connector: ""
|
50
|
+
two_words_connector: ""
|
51
|
+
last_word_connector: ""
|
52
|
+
about_x_years:
|
53
|
+
one: ~1a
|
54
|
+
any: "~%{count}a"
|
55
|
+
over_x_years:
|
56
|
+
one: ">1a"
|
57
|
+
other: ">%{count}a"
|
58
|
+
almost_x_years:
|
59
|
+
one: ~1a
|
60
|
+
any: "~%{count}a"
|
data/lib/dotiw/locale/ru.yml
CHANGED
@@ -37,3 +37,40 @@ ru:
|
|
37
37
|
many: "%{count} лет"
|
38
38
|
other: "%{count} года"
|
39
39
|
less_than_x: "меньше, чем %{distance}"
|
40
|
+
dotiw_compact:
|
41
|
+
seconds:
|
42
|
+
one: 1с
|
43
|
+
other: "%{count}с"
|
44
|
+
minutes:
|
45
|
+
one: 1м
|
46
|
+
other: "%{count}м"
|
47
|
+
hours:
|
48
|
+
one: 1ч
|
49
|
+
other: "%{count}ч"
|
50
|
+
days:
|
51
|
+
one: 1д
|
52
|
+
other: "%{count}д"
|
53
|
+
weeks:
|
54
|
+
one: 1н
|
55
|
+
other: "%{count}н"
|
56
|
+
months:
|
57
|
+
one: 1ме
|
58
|
+
other: "%{count}ме"
|
59
|
+
years:
|
60
|
+
one: "%{count}г"
|
61
|
+
few: "%{count}г"
|
62
|
+
many: "%{count}л"
|
63
|
+
other: "%{count}г"
|
64
|
+
less_than_x: "<%{distance}"
|
65
|
+
words_connector: ""
|
66
|
+
two_words_connector: ""
|
67
|
+
last_word_connector: ""
|
68
|
+
about_x_years:
|
69
|
+
one: ~1г
|
70
|
+
any: "~%{count}г"
|
71
|
+
over_x_years:
|
72
|
+
one: ">1г"
|
73
|
+
other: ">%{count}г"
|
74
|
+
almost_x_years:
|
75
|
+
one: ~1г
|
76
|
+
any: "~%{count}г"
|
@@ -0,0 +1,48 @@
|
|
1
|
+
sv:
|
2
|
+
datetime:
|
3
|
+
dotiw:
|
4
|
+
seconds:
|
5
|
+
one: 1 sekund
|
6
|
+
other: "%{count} sekunder"
|
7
|
+
minutes:
|
8
|
+
one: 1 minut
|
9
|
+
other: "%{count} minuter"
|
10
|
+
hours:
|
11
|
+
one: 1 timme
|
12
|
+
other: "%{count} timmar"
|
13
|
+
days:
|
14
|
+
one: 1 dag
|
15
|
+
other: "%{count} dagar"
|
16
|
+
weeks:
|
17
|
+
one: 1 vecka
|
18
|
+
other: "%{count} veckor"
|
19
|
+
months:
|
20
|
+
one: 1 månad
|
21
|
+
other: "%{count} månader"
|
22
|
+
years:
|
23
|
+
one: 1 år
|
24
|
+
other: "%{count} år"
|
25
|
+
less_than_x: "mindre än %{distance}"
|
26
|
+
dotiw_compact:
|
27
|
+
seconds:
|
28
|
+
one: 1s
|
29
|
+
other: "%{count}s"
|
30
|
+
minutes:
|
31
|
+
one: 1m
|
32
|
+
other: "%{count}m"
|
33
|
+
hours:
|
34
|
+
one: 1t
|
35
|
+
other: "%{count}t"
|
36
|
+
days:
|
37
|
+
one: 1d
|
38
|
+
other: "%{count}d"
|
39
|
+
weeks:
|
40
|
+
one: 1v
|
41
|
+
other: "%{count}v"
|
42
|
+
months:
|
43
|
+
one: 1m
|
44
|
+
other: "%{count}m"
|
45
|
+
years:
|
46
|
+
one: 1å
|
47
|
+
other: "%{count}å"
|
48
|
+
less_than_x: "<%{distance}"
|
data/lib/dotiw/locale/vi.yml
CHANGED
@@ -23,3 +23,26 @@ vi:
|
|
23
23
|
one: 1 năm
|
24
24
|
other: "%{count} năm"
|
25
25
|
less_than_x: "ít hơn %{distance}"
|
26
|
+
dotiw_compact:
|
27
|
+
seconds:
|
28
|
+
one: 1 giây
|
29
|
+
other: "%{count}giây"
|
30
|
+
minutes:
|
31
|
+
one: 1 phút
|
32
|
+
other: "%{count}phút"
|
33
|
+
hours:
|
34
|
+
one: 1 giờ
|
35
|
+
other: "%{count}giờ"
|
36
|
+
days:
|
37
|
+
one: 1 ngày
|
38
|
+
other: "%{count}ngày"
|
39
|
+
weeks:
|
40
|
+
one: 1 tuần
|
41
|
+
other: "%{count}tuần"
|
42
|
+
months:
|
43
|
+
one: 1 tháng
|
44
|
+
other: "%{count}tháng"
|
45
|
+
years:
|
46
|
+
one: 1 năm
|
47
|
+
other: "%{count}năm"
|
48
|
+
less_than_x: "< %{distance}"
|
data/lib/dotiw/locale/zh-CN.yml
CHANGED
@@ -23,3 +23,26 @@ zh-CN:
|
|
23
23
|
one: 1 年
|
24
24
|
other: "%{count} 年"
|
25
25
|
less_than_x: "小于 %{distance}"
|
26
|
+
dotiw_compact:
|
27
|
+
seconds:
|
28
|
+
one: 1秒
|
29
|
+
other: "%{count}秒"
|
30
|
+
minutes:
|
31
|
+
one: 1分钟
|
32
|
+
other: "%{count}分钟"
|
33
|
+
hours:
|
34
|
+
one: 1小时
|
35
|
+
other: "%{count}小时"
|
36
|
+
days:
|
37
|
+
one: 1天
|
38
|
+
other: "%{count}天"
|
39
|
+
weeks:
|
40
|
+
one: 1周
|
41
|
+
other: "%{count}周"
|
42
|
+
months:
|
43
|
+
one: 1月
|
44
|
+
other: "%{count}月"
|
45
|
+
years:
|
46
|
+
one: 1年
|
47
|
+
other: "%{count}年"
|
48
|
+
less_than_x: "小于 %{distance}"
|
@@ -0,0 +1,60 @@
|
|
1
|
+
zh-TW:
|
2
|
+
datetime:
|
3
|
+
dotiw:
|
4
|
+
seconds:
|
5
|
+
one: 1 秒
|
6
|
+
other: "%{count} 秒"
|
7
|
+
minutes:
|
8
|
+
one: 1 分鐘
|
9
|
+
other: "%{count} 分鐘"
|
10
|
+
hours:
|
11
|
+
one: 1 小時
|
12
|
+
other: "%{count} 小時"
|
13
|
+
days:
|
14
|
+
one: 1 天
|
15
|
+
other: "%{count} 天"
|
16
|
+
weeks:
|
17
|
+
one: 1 週
|
18
|
+
other: "%{count} 週"
|
19
|
+
months:
|
20
|
+
one: 1 月
|
21
|
+
other: "%{count} 月"
|
22
|
+
years:
|
23
|
+
one: 1 年
|
24
|
+
other: "%{count} 年"
|
25
|
+
less_than_x: "小於 %{distance}"
|
26
|
+
dotiw_compact:
|
27
|
+
seconds:
|
28
|
+
one: 1秒
|
29
|
+
other: "%{count}秒"
|
30
|
+
minutes:
|
31
|
+
one: 1分鐘
|
32
|
+
other: "%{count}分鐘"
|
33
|
+
hours:
|
34
|
+
one: 1小時
|
35
|
+
other: "%{count}小時"
|
36
|
+
days:
|
37
|
+
one: 1天
|
38
|
+
other: "%{count}天"
|
39
|
+
weeks:
|
40
|
+
one: 1週
|
41
|
+
other: "%{count}週"
|
42
|
+
months:
|
43
|
+
one: 1月
|
44
|
+
other: "%{count}月"
|
45
|
+
years:
|
46
|
+
one: 1年
|
47
|
+
other: "%{count}年"
|
48
|
+
less_than_x: "小於%{distance}"
|
49
|
+
words_connector: ""
|
50
|
+
two_words_connector: ""
|
51
|
+
last_word_connector: ""
|
52
|
+
about_x_years:
|
53
|
+
one: 約1年
|
54
|
+
any: "約%{count}年"
|
55
|
+
over_x_years:
|
56
|
+
one: "大於1年"
|
57
|
+
other: "大於%{count}年"
|
58
|
+
almost_x_years:
|
59
|
+
one: 將近1年
|
60
|
+
any: "將近%{count}年"
|
data/lib/dotiw/methods.rb
CHANGED
@@ -12,18 +12,25 @@ module DOTIW
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def distance_of_time(seconds, options = {})
|
15
|
-
options
|
15
|
+
options = options_with_scope(options).reverse_merge(
|
16
|
+
include_seconds: true
|
17
|
+
)
|
18
|
+
options.delete(:compact)
|
16
19
|
_display_time_in_words DOTIW::TimeHash.new(seconds, nil, nil, options).to_hash, options
|
17
20
|
end
|
18
21
|
|
19
22
|
def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options = {}, options = {})
|
23
|
+
raise ArgumentError, "nil can't be converted to a Time value" if from_time.nil? || to_time.nil?
|
24
|
+
|
20
25
|
if include_seconds_or_options.is_a?(Hash)
|
21
26
|
options = include_seconds_or_options
|
22
27
|
else
|
28
|
+
options = options.dup
|
23
29
|
options[:include_seconds] ||= !!include_seconds_or_options
|
24
30
|
end
|
25
31
|
return distance_of_time(from_time, options) if to_time == 0
|
26
32
|
|
33
|
+
options = options_with_scope(options)
|
27
34
|
hash = distance_of_time_in_words_hash(from_time, to_time, options)
|
28
35
|
_display_time_in_words(hash, options)
|
29
36
|
end
|
@@ -34,8 +41,16 @@ module DOTIW
|
|
34
41
|
|
35
42
|
private
|
36
43
|
|
44
|
+
def options_with_scope(options)
|
45
|
+
if options.key?(:compact)
|
46
|
+
options.merge(scope: DOTIW::DEFAULT_I18N_SCOPE_COMPACT)
|
47
|
+
else
|
48
|
+
options
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
37
52
|
def _display_time_in_words(hash, options = {})
|
38
|
-
options.reverse_merge
|
53
|
+
options = options.reverse_merge(
|
39
54
|
include_seconds: false
|
40
55
|
).symbolize_keys!
|
41
56
|
|
@@ -77,17 +92,17 @@ module DOTIW
|
|
77
92
|
highest_measures = 1 if options.delete(:highest_measure_only)
|
78
93
|
output = output[0...highest_measures] if highest_measures
|
79
94
|
|
80
|
-
options[:words_connector] ||= I18n.translate :
|
95
|
+
options[:words_connector] ||= I18n.translate :"#{i18n_scope}.words_connector",
|
81
96
|
default: :'support.array.words_connector',
|
82
97
|
locale: options[:locale]
|
83
|
-
options[:two_words_connector] ||= I18n.translate :
|
98
|
+
options[:two_words_connector] ||= I18n.translate :"#{i18n_scope}.two_words_connector",
|
84
99
|
default: :'support.array.two_words_connector',
|
85
100
|
locale: options[:locale]
|
86
|
-
options[:last_word_connector] ||= I18n.translate :
|
101
|
+
options[:last_word_connector] ||= I18n.translate :"#{i18n_scope}.last_word_connector",
|
87
102
|
default: :'support.array.last_word_connector',
|
88
103
|
locale: options[:locale]
|
89
104
|
|
90
|
-
output.to_sentence(options)
|
105
|
+
output.to_sentence(options.except(:accumulate_on, :compact))
|
91
106
|
end
|
92
107
|
end
|
93
|
-
end
|
108
|
+
end
|
data/lib/dotiw/time_hash.rb
CHANGED
@@ -8,7 +8,7 @@ module DOTIW
|
|
8
8
|
|
9
9
|
def initialize(distance, from_time, to_time = nil, options = {})
|
10
10
|
@output = {}
|
11
|
-
@options = options
|
11
|
+
@options = options.dup
|
12
12
|
@distance = distance
|
13
13
|
@from_time = from_time || Time.current
|
14
14
|
@to_time = to_time || (@to_time_not_given = true && @from_time + distance.seconds)
|
@@ -34,23 +34,29 @@ module DOTIW
|
|
34
34
|
|
35
35
|
attr_reader :options, :output
|
36
36
|
|
37
|
+
ONE_MINUTE = 1.minute.freeze
|
38
|
+
ONE_HOUR = 1.hour.freeze
|
39
|
+
ONE_DAY = 1.day.freeze
|
40
|
+
ONE_WEEK = 7.days.freeze
|
41
|
+
FOUR_WEEKS = 28.days.freeze
|
42
|
+
|
37
43
|
def build_time_hash
|
38
|
-
if accumulate_on = options
|
44
|
+
if accumulate_on = options[:accumulate_on]
|
39
45
|
accumulate_on = accumulate_on.to_sym
|
40
46
|
return build_time_hash if accumulate_on == :years
|
41
47
|
|
42
48
|
TIME_FRACTIONS.index(accumulate_on).downto(0) { |i| send("build_#{TIME_FRACTIONS[i]}") }
|
43
49
|
else
|
44
50
|
while distance > 0
|
45
|
-
if distance <
|
51
|
+
if distance < ONE_MINUTE
|
46
52
|
build_seconds
|
47
|
-
elsif distance <
|
53
|
+
elsif distance < ONE_HOUR
|
48
54
|
build_minutes
|
49
|
-
elsif distance <
|
55
|
+
elsif distance < ONE_DAY
|
50
56
|
build_hours
|
51
|
-
elsif distance <
|
57
|
+
elsif distance < ONE_WEEK
|
52
58
|
build_days
|
53
|
-
elsif distance <
|
59
|
+
elsif distance < FOUR_WEEKS
|
54
60
|
build_weeks
|
55
61
|
else # greater than a week
|
56
62
|
build_years_months_weeks_days
|
@@ -67,19 +73,19 @@ module DOTIW
|
|
67
73
|
end
|
68
74
|
|
69
75
|
def build_minutes
|
70
|
-
output[:minutes], @distance = distance.divmod(
|
76
|
+
output[:minutes], @distance = distance.divmod(ONE_MINUTE.to_i)
|
71
77
|
end
|
72
78
|
|
73
79
|
def build_hours
|
74
|
-
output[:hours], @distance = distance.divmod(
|
80
|
+
output[:hours], @distance = distance.divmod(ONE_HOUR.to_i)
|
75
81
|
end
|
76
82
|
|
77
83
|
def build_days
|
78
|
-
output[:days], @distance = distance.divmod(
|
84
|
+
output[:days], @distance = distance.divmod(ONE_DAY.to_i) unless output[:days]
|
79
85
|
end
|
80
86
|
|
81
87
|
def build_weeks
|
82
|
-
output[:weeks], @distance = distance.divmod(
|
88
|
+
output[:weeks], @distance = distance.divmod(ONE_WEEK.to_i) unless output[:weeks]
|
83
89
|
end
|
84
90
|
|
85
91
|
def build_months
|
@@ -137,9 +143,9 @@ module DOTIW
|
|
137
143
|
output[:weeks] = weeks
|
138
144
|
output[:days] = days
|
139
145
|
|
140
|
-
total_days, @distance = distance.abs.divmod(
|
146
|
+
total_days, @distance = distance.abs.divmod(ONE_DAY.to_i)
|
141
147
|
|
142
148
|
[total_days, @distance]
|
143
149
|
end
|
144
|
-
end
|
145
|
-
end
|
150
|
+
end
|
151
|
+
end
|
data/lib/dotiw/version.rb
CHANGED
data/lib/dotiw.rb
CHANGED
@@ -17,6 +17,7 @@ module DOTIW
|
|
17
17
|
extend self
|
18
18
|
|
19
19
|
DEFAULT_I18N_SCOPE = :'datetime.dotiw'
|
20
|
+
DEFAULT_I18N_SCOPE_COMPACT = :'datetime.dotiw_compact'
|
20
21
|
|
21
22
|
def init_i18n!
|
22
23
|
I18n.load_path.unshift(*locale_files)
|
@@ -36,7 +37,7 @@ module DOTIW
|
|
36
37
|
def files(directory, ext)
|
37
38
|
Dir[File.join File.dirname(__FILE__), directory, ext]
|
38
39
|
end
|
39
|
-
end
|
40
|
+
end
|
40
41
|
|
41
42
|
DOTIW.init_i18n!
|
42
43
|
|