localized duration format for french
Question: What is the Python analog of [Time4J's code
example](https://github.com/MenoData/Time4J/blob/1d73d3ed38a546fe6e91c75a6720c288f54a842a/README.md):

    
    
    // duration in seconds normalized to hours, minutes and seconds
    Duration<?> dur = Duration.of(337540, ClockUnit.SECONDS).with(Duration.STD_CLOCK_PERIOD);
    
    // custom duration format => hh:mm:ss
    String s1 = Duration.Formatter.ofPattern("hh:mm:ss").format(dur);
    System.out.println(s1); // output: 93:45:40
    
    // localized duration format for french
    String s2 = PrettyTime.of(Locale.FRANCE).print(dur, TextWidth.WIDE);
    System.out.println(s2); // output: 93 heures, 45 minutes et 40 secondes
    

It is easy to get `93:45:40`:

    
    
    #!/usr/bin/env python3
    from datetime import timedelta
    
    dur = timedelta(seconds=337540)
    print(dur) # -> 3 days, 21:45:40
    fields = {}
    fields['hours'], seconds = divmod(dur // timedelta(seconds=1), 3600)
    fields['minutes'], fields['seconds'] = divmod(seconds, 60)
    print("%(hours)02d:%(minutes)02d:%(seconds)02d" % fields) # -> 93:45:40
    

but how do I emulate `PrettyTime.of(Locale.FRANCE).print(dur, TextWidth.WIDE)`
Java code in Python (without hardcoding the units)?


Answer: [`babel` module](http://babel.pocoo.org/) allows to get close to desired
output:

    
    
    from babel.dates import format_timedelta # $ pip install babel
    
    print(", ".join(format_timedelta(timedelta(**{unit: fields[unit]}),
                                     granularity=unit.rstrip('s'),
                                     threshold=fields[unit] + 1,
                                     locale='fr')
                    for unit in "hours minutes seconds".split()))
    # -> 93 heures, 45 minutes, 40 secondes
    

It handles locale and plural forms automatically e.g., for `dur =
timedelta(seconds=1)` it produces:

    
    
    0 heure, 0 minute, 1 seconde
    

Perhaps a better solution would be to translate the format string manually
using standard tools such as `gettext`.

