-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow slang methods to change locale #154
Conversation
maya/core.py
Outdated
(default: 'en' - English) | ||
""" | ||
dt = self.datetime(to_timezone=self.local_timezone) | ||
return pendulum.instance(dt).diff_for_humans(locale=locale) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've played around a little with this diff_for_humans()
method and I'm quite confused now. Maybe you have an explanation:
Assume my current local time is about: Wed, 16 May 2018 19:44:12 +0200
and I'm using the latest maya release:
>>> n = maya.now()
>>> n.slang_time()
'7 seconds ago'
>>> # now with pendulum:
>>> import pendulum
>>> dt = n.datetime(naive=True, to_timezone=n.local_timezone)
>>> pendulum.instance(dt).diff_for_humans()
'1 hour from now'
What bugs me is that 1 hour from now
which is from pendulum. (btw. the same happens if there is a tzinfo
in dt
)
Any idea why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the same issue too. The issue is when you use naive
, maya strips timezone information. I guess pendulum assumes UTC if there's no timezone (I haven't looked at the source).
>>> tz = maya.now().local_timezone
>>> tz
'Europe/London'
>>> maya.now().datetime()
datetime.datetime(2018, 5, 16, 19, 2, 20, 671150, tzinfo=<UTC>)
>>> maya.now().datetime(naive=True)
datetime.datetime(2018, 5, 16, 19, 2, 27, 553630)
>>> maya.now().datetime(naive=True, to_timezone=tz)
datetime.datetime(2018, 5, 16, 20, 2, 40, 217788)
I think the '1 hour from now' is actually 1:59 to 1:00 from now.
>>>utc = maya.now().datetime()
datetime.datetime(2018, 5, 16, 19, 16, 1, 671150, tzinfo=<UTC>)
>>> uk = maya.now().datetime(naive=True, to_timezone="Europe/London")
datetime.datetime(2018, 5, 16, 20, 16, 4, 681577)
>>> spain = maya.now().datetime(naive=True, to_timezone="Europe/Madrid")
datetime.datetime(2018, 5, 16, 21, 16, 24, 697862)
>>> pendulum.instance(utc).diff_for_humans()
'5 seconds ago'
>>> pendulum.instance(uk).diff_for_humans()
'59 minutes from now'
>>> pendulum.instance(spain).diff_for_humans()
'1 hour from now'
maya/core.py
Outdated
@@ -336,10 +336,15 @@ def slang_date(self): | |||
dt = self.datetime(naive=True, to_timezone=self.local_timezone) | |||
return humanize.naturaldate(dt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the slang_date()
method? As I user of maya I would assume I can use localization here, too.
The thing is that pendulums diff_for_humans()
is just giving you a diff from two datetimes - no matter if you are interested in the date or time.. Thus, we cannot use it for both slang_date()
and slang_time()
.
I see some problems when would use pendulums Maybe we could instead use |
@timofurrer Sorry I hadn't done I think humanize is not an option to use for localisation because it seems to have gone stale (last commit Dec 2016), and it's current release (2014) only has 3 localisations. If we use pendulums The options I can see to fully have internationalisation:
|
@timofurrer I've added localisation for I've used a mix of pendulums |
@alxwrd sorry for the delay. |
@timofurrer no worries. I've just pushed some tests. I think this now closes #84? I can't see anything else that would need internationalisation? All the other Maya methods are for iso. |
Awesome! Thanks for the contribution! 🎉 |
This attempts to add internationalisation/localisation to the
slang_
methods to address #84.I originally attempted to use the localisation built into humanize. However, I had the issue that the last release for humanize was in 2014, and at the time it only supported 3 locales.
Because of this, I switched to using pendulum which has a much bigger locale selection.
However, pendulum only has
diff_for_humans()
which is for time. This leavesslang_date
without localisation.I think the questions that are left are:
slang_date
just get left for now?humanize
?