Skip to content

Commit

Permalink
Merge pull request #154 from alxwrd/support-locales
Browse files Browse the repository at this point in the history
Allow slang methods to change locale
  • Loading branch information
timofurrer authored May 26, 2018
2 parents ed5d72b + d87236a commit 9964e93
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ In chronological order:
- Evan Mattiza <[email protected]> (`@emattiza <https://github.com/emattiza>`_)
- Dima Spivak <[email protected]> (`@dimaspivak <https://github.com/dimaspivak>`_)
- Tom Barron <[email protected]> (`@dtbarron <https://github.com/tbarron>`_)
- Alex Ward <[email protected]> (`@alxwrd <https://github.com/alxwrd>`_)
51 changes: 43 additions & 8 deletions maya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import snaptime
from tzlocal import get_localzone
from dateutil.relativedelta import relativedelta
from dateparser.languages.loader import default_loader

from .compat import cmp, comparable

Expand Down Expand Up @@ -331,15 +332,39 @@ def epoch(self):

# Human Slang Extras
# ------------------
def slang_date(self):
""""Returns human slang representation of date."""
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
return humanize.naturaldate(dt)
def slang_date(self, locale="en"):
""""Returns human slang representation of date.
def slang_time(self):
""""Returns human slang representation of time."""
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
return humanize.naturaltime(dt)
Keyword Arguments:
locale -- locale to translate to, e.g. 'fr' for french.
(default: 'en' - English)
"""
dt = pendulum.instance(self.datetime())

try:
return _translate(dt, locale)
except KeyError:
pass

dt.set_formatter("alternative")
delta = humanize.time.abs_timedelta(
timedelta(seconds=(self.epoch - now().epoch)))

format_string = "DD MMM"
if delta.days >= 365:
format_string += " YYYY"

return dt.format(format_string, locale=locale).title()

def slang_time(self, locale="en"):
""""Returns human slang representation of time.
Keyword Arguments:
locale -- locale to translate to, e.g. 'fr' for french.
(default: 'en' - English)
"""
dt = self.datetime()
return pendulum.instance(dt).diff_for_humans(locale=locale)


def utc_offset(time_struct=None):
Expand Down Expand Up @@ -759,6 +784,16 @@ def _seconds_or_timedelta(duration):
return dt_timedelta


def _translate(dt, target_locale):
en = default_loader.get_locale("en")
target = default_loader.get_locale(target_locale)
naturaldate = humanize.naturaldate(dt)

base = en.translate(naturaldate, settings=dateparser.conf.settings)

return target.info["relative-type"][base][-1]


def intervals(start, end, interval):
"""
Yields MayaDT objects between the start and end MayaDTs given,
Expand Down
12 changes: 11 additions & 1 deletion tests/test_maya.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,19 @@ def test_slang_date():
assert d.slang_date() == 'tomorrow'


def test_slang_date_locale():
d = maya.when('tomorrow')
assert d.slang_date(locale='fr') == 'demain'


def test_slang_time():
d = maya.when('1 hour ago')
assert d.slang_time() == 'an hour ago'
assert d.slang_time() == '1 hour ago'


def test_slang_time_locale():
d = maya.when('1 hour ago')
assert d.slang_time(locale='de') == 'vor 1 Stunde'


@pytest.mark.parametrize("string,kwds,expected", [
Expand Down

0 comments on commit 9964e93

Please sign in to comment.