This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
forked from pypa/sampleproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_indications.py
100 lines (83 loc) · 2.39 KB
/
time_indications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
"""
smallparts.l10n.time_indications
Time indication translations
"""
from smallparts.l10n import languages
SECONDS = 'seconds'
MINUTES = 'minutes'
HOURS = 'hours'
DAYS = 'days'
WEEKS = 'weeks'
MONTHS = 'months'
YEARS = 'years'
SUPPORTED_UNITS = (SECONDS, MINUTES, HOURS, DAYS,
WEEKS, MONTHS, YEARS)
# Singular and plural forms for time units
NUMBER_CATEGORIES = {
languages.EN: {
SECONDS: ('second', SECONDS),
MINUTES: ('minute', MINUTES),
HOURS: ('hour', HOURS),
DAYS: ('day', DAYS),
WEEKS: ('week', WEEKS),
MONTHS: ('month', MONTHS),
YEARS: ('year', YEARS)
},
languages.DE: {
SECONDS: ('Sekunde', 'Sekunden'),
MINUTES: ('Minute', 'Minuten'),
HOURS: ('Stunde', 'Stunden'),
DAYS: ('Tag', 'Tage'),
WEEKS: ('Woche', 'Wochen'),
MONTHS: ('Monat', 'Monate'),
YEARS: ('Jahr', 'Jahre')
},
languages.ES: {
SECONDS: ('segundo', 'segundos'),
MINUTES: ('minuto', 'minutos'),
HOURS: ('hora', 'horas'),
DAYS: ('día', 'dias'),
WEEKS: ('semana', 'semanas'),
MONTHS: ('mes', 'meses'),
YEARS: ('año', 'años')
},
languages.FR: {
SECONDS: ('seconde', 'secondes'),
MINUTES: ('minute', 'minutes'),
HOURS: ('heure', 'heures'),
DAYS: ('jour', 'jours'),
WEEKS: ('semaine', 'semaines'),
MONTHS: ('mois', 'mois'),
YEARS: ('an', 'ans')
}
}
#
# Functions
#
def format_component(lang=languages.DEFAULT, **kwargs):
"""Format the time component in singular or plural form
in the language selected by lang=...
"""
try:
number_category = NUMBER_CATEGORIES[lang]
except KeyError:
raise ValueError(languages.missing_translation(lang))
#
for unit in SUPPORTED_UNITS:
amount = kwargs.get(unit)
if amount is not None:
singular, plural = number_category[unit]
if amount == 1:
matching_form = singular
else:
matching_form = plural
#
break
#
else:
raise ValueError('Please specify an amount of time as keyword'
' argument {0!r}!'.format(SUPPORTED_UNITS))
#
return '{0} {1}'.format(amount, matching_form)
# vim:fileencoding=utf-8 autoindent ts=4 sw=4 sts=4 expandtab: