diff options
Diffstat (limited to 'app/helpers/time_helper.rb')
-rw-r--r-- | app/helpers/time_helper.rb | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/app/helpers/time_helper.rb b/app/helpers/time_helper.rb index ad473875a53..0a5751c5221 100644 --- a/app/helpers/time_helper.rb +++ b/app/helpers/time_helper.rb @@ -1,20 +1,20 @@ # frozen_string_literal: true module TimeHelper + TIME_UNIT_TRANSLATION = { + seconds: ->(seconds) { n_('%d second', '%d seconds', seconds) % seconds }, + minutes: ->(minutes) { n_('%d minute', '%d minutes', minutes) % minutes }, + hours: ->(hours) { n_('%d hour', '%d hours', hours) % hours }, + days: ->(days) { n_('%d day', '%d days', days) % days }, + weeks: ->(weeks) { n_('%d week', '%d weeks', weeks) % weeks }, + months: ->(months) { n_('%d month', '%d months', months) % months }, + years: ->(years) { n_('%d year', '%d years', years) % years } + }.freeze + def time_interval_in_words(interval_in_seconds) - interval_in_seconds = interval_in_seconds.to_i - minutes = interval_in_seconds / 60 - seconds = interval_in_seconds - minutes * 60 + time_parts = ActiveSupport::Duration.build(interval_in_seconds.to_i).parts - if minutes >= 1 - if seconds % 60 == 0 - n_('%d minute', '%d minutes', minutes) % minutes - else - [n_('%d minute', '%d minutes', minutes) % minutes, n_('%d second', '%d seconds', seconds) % seconds].to_sentence - end - else - n_('%d second', '%d seconds', seconds) % seconds - end + time_parts.map { |unit, value| TIME_UNIT_TRANSLATION[unit].call(value) }.to_sentence end def duration_in_numbers(duration_in_seconds) |