Skip to content

Commit

Permalink
First proto of english rendering. Seems to work OK
Browse files Browse the repository at this point in the history
Signed-off-by: Balthazar Rouberol <balthazar.rouberol@mapado.com>
  • Loading branch information
Balthazar Rouberol authored and nchaulet committed Jun 1, 2015
1 parent 9df8385 commit 229c2c6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 27 deletions.
16 changes: 16 additions & 0 deletions datection/data/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@
u'nov': 11,
u'dec': 12,
}


TRANSLATIONS = {
'en_US': {
'today': u"today",
'today_abbrev': u"today",
'tomorrow': u'tomorrow',
'this': u'this',
'midnight': u'midnight',
'every day': u'every day',
'the': u'the',
'and': u'and',
'at': u'et',
'except': u'except',
}
}
16 changes: 15 additions & 1 deletion datection/data/fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
}

SHORT_MONTHS = {
# Untested for now!
u'jan': 1,
u'janv': 1,
u'fév': 2,
Expand All @@ -60,3 +59,18 @@
u'dec': 12,
u'déc': 12,
}

TRANSLATIONS = {
'fr_FR': {
'today': u"aujourd'hui",
'today_abbrev': u"auj.",
'tomorrow': u'demain',
'this': u'ce',
'midnight': u'minuit',
'every day': u'tous les jours',
'the': u'le',
'and': u'et',
'at': u'à',
'except': u'sauf',
},
}
3 changes: 3 additions & 0 deletions datection/lang.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-

"""
Definition of default locales for given languages
"""

DEFAULT_LOCALES = {
'fr': 'fr_FR.UTF8',
'en': 'en_US.UTF8',
}


Expand Down
87 changes: 62 additions & 25 deletions datection/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,8 @@
from datection.lang import DEFAULT_LOCALES
from datection.lang import getlocale

locale = 'fr_FR.UTF8'

TRANSLATIONS = {
'fr_FR': {
'today': u"aujourd'hui",
'today_abbrev': u"auj.",
'tomorrow': u'demain',
'this': u'ce',
'midnight': u'minuit',
'every day': u'tous les jours',
'the': u'le',
'and': u'et',
'at': u'à',
'except': u'sauf',
}
}
locale = 'fr_FR.UTF8'

FormatterTuple = namedtuple("FormatterTuple", ["formatter", "display_args"])

Expand Down Expand Up @@ -212,9 +198,16 @@ def __init__(self):
self.language_code, self.encoding = locale.split('.')
self.templates = None

@cached_property
def translations(self):
"""Return a translation dict using the current locale language"""
lang = self.language_code.split('_')[0]
mod = __import__('datection.data.' + lang, fromlist=['data'])
return mod.TRANSLATIONS

def _(self, key):
"""Return the translation of the key in the instance language."""
return TRANSLATIONS[self.language_code][key]
return self.translations[self.language_code][key]

def get_template(self, key=None):
"""Return the template corresponding to the instance language
Expand Down Expand Up @@ -328,12 +321,24 @@ def __init__(self, date):
'all': u'{prefix} {dayname} {day} {month} {year}',
'no_year': u'{prefix} {dayname} {day} {month}',
'no_year_no_month': u'{prefix} {dayname} {day}',
},
'en_US': {
'all': u'{prefix} {dayname} {day} of {month} {year}',
'no_year': u'{prefix} {dayname} {day} of {month}',
'no_year_no_month': u'{prefix} {dayname} {day}',
}
}

def format_day(self):
"""Format the date day using the current locale."""
return u'1er' if self.date.day == 1 else unicode(self.date.day)
if self.language_code == 'fr_FR':
return u'1er' if self.date.day == 1 else unicode(self.date.day)
elif self.language_code in ['en_US', 'en_GB']:
if 4 <= self.date.day <= 20 or 24 <= self.date.day <= 30:
suffix = 'th'
else:
suffix = ['st', 'nd', 'rd'][self.date.day % 10 - 1]
return u'%d%s' % (self.date.day, suffix)

def format_dayname(self, abbrev=False):
"""Format the date day using the current locale."""
Expand Down Expand Up @@ -493,6 +498,7 @@ def __init__(self, start_date, end_date):
self.end_date = get_date(end_date)
self.templates = {
'fr_FR': u'du {start_date} au {end_date}',
'en_US': u'{start_date} - {end_date}',
}

def same_day_interval(self):
Expand Down Expand Up @@ -583,6 +589,7 @@ def __init__(self, date_list):
self.date_list = [get_date(d) for d in date_list]
self.templates = {
'fr_FR': u'les {date_list} et {last_date}',
'en_US': u'{date_list} and {last_date}',
}

@postprocess()
Expand All @@ -608,7 +615,8 @@ def __init__(self, time):
super(TimeFormatter, self).__init__()
self.time = get_time(time)
self.templates = {
'fr_FR': u'{prefix} {hour} h {minute}'
'fr_FR': u'{prefix} {hour} h {minute}',
'en_US': u'{prefix} {hour}:{minute}',
}

def format_hour(self):
Expand All @@ -618,8 +626,10 @@ def format_hour(self):
def format_minute(self):
"""Format the time hour using the current locale."""
if self.time.minute == 0:
return u''
global locale
if self.language_code == 'fr_FR':
return u''
elif self.language_code in ['en_US', 'en_GB']:
return u'00'
with TemporaryLocale(_locale.LC_TIME, locale):
return self.time.strftime('%M')

Expand Down Expand Up @@ -649,8 +659,12 @@ def __init__(self, start_time, end_time):
self.templates = {
'fr_FR': {
'interval': u'de {start_time} à {end_time}',
'single_time': u'à {time}'
}
'single_time': u'à {time}',
},
'en_US': {
'interval': u'{start_time} - {end_time}',
'single_time': u'at {time}',
},
}

def display(self, prefix=False):
Expand All @@ -677,7 +691,8 @@ def __init__(self, _datetime):
super(DatetimeFormatter, self).__init__()
self.datetime = _datetime
self.templates = {
'fr_FR': u'{date} à {time}'
'fr_FR': u'{date} à {time}',
'en_US': u'{date} at {time}',
}

def display(self, *args, **kwargs):
Expand Down Expand Up @@ -707,6 +722,11 @@ def __init__(self, start_datetime, end_datetime):
'single_day': u'le {date} {time_interval}',
'single_time': u'{date_interval} à {time}',
'date_interval': u'{date_interval} {time_interval}',
},
'en_US': {
'single_day': u'the {date} {time_interval}',
'single_time': u'{date_interval} at {time}',
'date_interval': u'{date_interval} {time_interval}',
}
}

Expand Down Expand Up @@ -753,7 +773,9 @@ def __init__(self, start, end):
self.end = end
self.templates = {
'fr_FR':
u'du {start_date} à {start_time} au {end_date} à {end_time}'
u'du {start_date} à {start_time} au {end_date} à {end_time}',
'en_US':
u'{start_date} at {start_time} - {end_date} at {end_time}'
}

@postprocess()
Expand Down Expand Up @@ -789,6 +811,11 @@ def __init__(self, drr):
'one_day': u'le {weekday}',
'interval': u'du {start_weekday} au {end_weekday}',
'weekday_reccurence': u'{weekdays}, {dates}, {time}',
},
'en_US': {
'one_day': u'the {weekday}',
'interval': u'from {start_weekday} to {end_weekday}',
'weekday_reccurence': u'{weekdays}, {dates}, {time}',
}
}

Expand Down Expand Up @@ -868,7 +895,8 @@ def __init__(self, schedule, start, end):
self.schedule = self.deduplicate(self.schedule)
self.start, self.end = start, end
self.templates = {
'fr_FR': u'{date} + autres dates'
'fr_FR': u'{date} + autres dates',
'en_US': u'{date} + more dates',
}

@postprocess(capitalize=True)
Expand Down Expand Up @@ -914,6 +942,10 @@ def __init__(self, opening_hours):
'fr_FR': {
'one_opening': u'{weekday} {time_interval}',
'several_openings': u'{opening} {time_list} et {last_time}'
},
'en_US': {
'one_opening': u'{weekday} {time_interval}',
'several_openings': u'{opening} {time_list} and {last_time}'
}
}

Expand Down Expand Up @@ -1063,6 +1095,7 @@ def __init__(self, schedule, apply_exlusion=True, format_exclusion=True):
self.format_exclusion = format_exclusion
self.templates = {
'fr_FR': u'{dates} {time}',
'en_US': u'{dates} {time}',
}

@cached_property
Expand Down Expand Up @@ -1257,6 +1290,10 @@ def __init__(self, schedule):
'fr_FR': {
'two_months': u'{month1} et {month2}',
'full': u'{months} {year}'
},
'en_US': {
'two_months': u'{month1} and {month2}',
'full': u'{months} {year}'
}
}

Expand Down
4 changes: 3 additions & 1 deletion datection/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" Some utility functions """
# -*- coding: utf-8 -*-

"""Some utility functions"""

import re
import datection
Expand Down

0 comments on commit 229c2c6

Please sign in to comment.