from django.utils import timezone


def duration_string(duration, precision='s'):
    """Format hours, minutes and seconds as a human-friendly string (e.g. "2
    hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or
    s = seconds.
    """
    h, m, s = duration_parts(duration)

    duration = ''
    if h > 0:
        duration = '{} hour{}'.format(h, 's' if h > 1 else '')
    if m > 0 and precision != 'h':
        duration += '{}{} minute{}'.format(
            '' if duration == '' else ', ', m, 's' if m > 1 else '')
    if s > 0 and precision != 'h' and precision != 'm':
        duration += '{}{} second{}'.format(
            '' if duration == '' else ', ', s, 's' if s > 1 else '')

    return duration


def duration_parts(duration):
    """Get hours, minutes and seconds from a timedelta.
    """
    if not isinstance(duration, timezone.timedelta):
        raise TypeError('Duration provided must be a timedetla')
    h, remainder = divmod(duration.seconds, 3600)
    h += duration.days * 24
    m, s = divmod(remainder, 60)
    return h, m, s

from django.utils import timezone
from django.utils.translation import ngettext


def duration_string(duration, precision='s'):
    """Format hours, minutes and seconds as a human-friendly string (e.g. "2
    hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or
    s = seconds.
    """
    h, m, s = duration_parts(duration)

    duration = ''
    if h > 0:
        duration = ngettext('%(hours)s hour', '%(hours)s hours', h) % {
            'hours': h
        }
    if m > 0 and precision != 'h':
        if duration != '':
            duration += ', '
        duration += ngettext('%(minutes)s minute', '%(minutes)s minutes', m) % {
            'minutes': m
        }
    if s > 0 and precision != 'h' and precision != 'm':
        if duration != '':
            duration += ', '
        duration += ngettext('%(seconds)s second', '%(seconds)s seconds', s) % {
            'seconds': s
        }

    return duration


def duration_parts(duration):
    """Get hours, minutes and seconds from a timedelta.
    """
    if not isinstance(duration, timezone.timedelta):
        raise TypeError('Duration provided must be a timedetla')
    h, remainder = divmod(duration.seconds, 3600)
    h += duration.days * 24
    m, s = divmod(remainder, 60)
    return h, m, s

Add translation support to `duration_string` utility
Add translation support to `duration_string` utility

  from django.utils import timezone
+ from django.utils.translation import ngettext
  
  
  def duration_string(duration, precision='s'):
      """Format hours, minutes and seconds as a human-friendly string (e.g. "2
      hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or
      s = seconds.
      """
      h, m, s = duration_parts(duration)
  
      duration = ''
      if h > 0:
-         duration = '{} hour{}'.format(h, 's' if h > 1 else '')
+         duration = ngettext('%(hours)s hour', '%(hours)s hours', h) % {
+             'hours': h
+         }
      if m > 0 and precision != 'h':
-         duration += '{}{} minute{}'.format(
-             '' if duration == '' else ', ', m, 's' if m > 1 else '')
+         if duration != '':
+             duration += ', '
+         duration += ngettext('%(minutes)s minute', '%(minutes)s minutes', m) % {
+             'minutes': m
+         }
      if s > 0 and precision != 'h' and precision != 'm':
-         duration += '{}{} second{}'.format(
-             '' if duration == '' else ', ', s, 's' if s > 1 else '')
+         if duration != '':
+             duration += ', '
+         duration += ngettext('%(seconds)s second', '%(seconds)s seconds', s) % {
+             'seconds': s
+         }
  
      return duration
  
  
  def duration_parts(duration):
      """Get hours, minutes and seconds from a timedelta.
      """
      if not isinstance(duration, timezone.timedelta):
          raise TypeError('Duration provided must be a timedetla')
      h, remainder = divmod(duration.seconds, 3600)
      h += duration.days * 24
      m, s = divmod(remainder, 60)
      return h, m, s
  
Add translation support to `duration_string` utility
## Code Before:
from django.utils import timezone


def duration_string(duration, precision='s'):
    """Format hours, minutes and seconds as a human-friendly string (e.g. "2
    hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or
    s = seconds.
    """
    h, m, s = duration_parts(duration)

    duration = ''
    if h > 0:
        duration = '{} hour{}'.format(h, 's' if h > 1 else '')
    if m > 0 and precision != 'h':
        duration += '{}{} minute{}'.format(
            '' if duration == '' else ', ', m, 's' if m > 1 else '')
    if s > 0 and precision != 'h' and precision != 'm':
        duration += '{}{} second{}'.format(
            '' if duration == '' else ', ', s, 's' if s > 1 else '')

    return duration


def duration_parts(duration):
    """Get hours, minutes and seconds from a timedelta.
    """
    if not isinstance(duration, timezone.timedelta):
        raise TypeError('Duration provided must be a timedetla')
    h, remainder = divmod(duration.seconds, 3600)
    h += duration.days * 24
    m, s = divmod(remainder, 60)
    return h, m, s

## Instruction:
Add translation support to `duration_string` utility
## Code After:
from django.utils import timezone
from django.utils.translation import ngettext


def duration_string(duration, precision='s'):
    """Format hours, minutes and seconds as a human-friendly string (e.g. "2
    hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or
    s = seconds.
    """
    h, m, s = duration_parts(duration)

    duration = ''
    if h > 0:
        duration = ngettext('%(hours)s hour', '%(hours)s hours', h) % {
            'hours': h
        }
    if m > 0 and precision != 'h':
        if duration != '':
            duration += ', '
        duration += ngettext('%(minutes)s minute', '%(minutes)s minutes', m) % {
            'minutes': m
        }
    if s > 0 and precision != 'h' and precision != 'm':
        if duration != '':
            duration += ', '
        duration += ngettext('%(seconds)s second', '%(seconds)s seconds', s) % {
            'seconds': s
        }

    return duration


def duration_parts(duration):
    """Get hours, minutes and seconds from a timedelta.
    """
    if not isinstance(duration, timezone.timedelta):
        raise TypeError('Duration provided must be a timedetla')
    h, remainder = divmod(duration.seconds, 3600)
    h += duration.days * 24
    m, s = divmod(remainder, 60)
    return h, m, s
