misc: use ngettext in seconds2humanduration to pluralize strings (#30788)

This commit is contained in:
Lauréline Guérin 2020-06-16 15:34:05 +02:00
parent 48d9398a9c
commit d068f7b2b2
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 20 additions and 14 deletions

View File

@ -11,7 +11,7 @@ import time
import datetime
import base64
from django.utils import six
from django.utils import translation
from quixote import cleanup
@ -64,9 +64,19 @@ def test_parse_invalid_file_size():
FileSizeWidget.parse_file_size(test_value)
def test_humantime():
for x in range(3, 100000, 13):
assert humanduration2seconds(seconds2humanduration(x)) == x
@pytest.mark.parametrize('seconds, expected', [
(1, '1 second'),
(3, '3 seconds'),
(100000, '1 day, 3 hours, 46 minutes and 40 seconds'),
(13, '13 seconds'),
(60, '1 minute'),
(3600, '1 hour'),
])
def test_humantime(seconds, expected):
pub = create_temporary_pub()
pub.ngettext = translation.ngettext
assert seconds2humanduration(seconds) == expected
assert humanduration2seconds(seconds2humanduration(seconds)) == seconds
def test_parse_mimetypes():

View File

@ -49,7 +49,7 @@ def _(message):
def ngettext(*args):
pub = get_publisher()
if pub is None:
return message
return args[0]
return force_str(force_text(pub.ngettext(*args)))

View File

@ -16,7 +16,7 @@
import re
from . import _, N_
from . import _, ngettext, N_
_minute = 60
_hour = 60 * 60
@ -66,10 +66,6 @@ def humanduration2seconds(humanduration):
def seconds2humanduration(seconds):
'''Convert a time range in seconds to a human string representation
'''
# years = int(seconds / _year)
# secons = seconds - _year * years
# months = int(seconds / _month)
# seconds = seconds - _month * months
if not type(seconds) is int:
return ""
@ -81,11 +77,11 @@ def seconds2humanduration(seconds):
seconds = seconds - _minute * minutes
human = []
if days:
human.append(_("%s days") % days)
human.append(ngettext('%(total)s day', '%(total)s days', days) % {'total': days})
if hours:
human.append(_("%s hours") % hours)
human.append(ngettext('%(total)s hour', '%(total)s hours', hours) % {'total': hours})
if minutes:
human.append(_("%s minutes") % minutes)
human.append(ngettext('%(total)s minute', '%(total)s minutes', minutes) % {'total': minutes})
if seconds:
human.append(_("%s seconds") % seconds)
human.append(ngettext('%(total)s second', '%(total)s seconds', seconds) % {'total': seconds})
return list2human(human)