Skip to content
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

Merged
merged 7 commits into from
May 26, 2018

Conversation

alxwrd
Copy link
Contributor

@alxwrd alxwrd commented May 15, 2018

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 leaves slang_date without localisation.

I think the questions that are left are:

  • Does slang_date just get left for now?
  • Do we just use the limited locales provided by humanize?

maya/core.py Outdated
(default: 'en' - English)
"""
dt = self.datetime(to_timezone=self.local_timezone)
return pendulum.instance(dt).diff_for_humans(locale=locale)
Copy link
Collaborator

@timofurrer timofurrer May 16, 2018

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?

Copy link
Contributor Author

@alxwrd alxwrd May 16, 2018

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)
Copy link
Collaborator

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().

@timofurrer
Copy link
Collaborator

I see some problems when would use pendulums diff_from_humans() for those simple slang methods, as it just gives as a plain datetime diff, but doesn't care if it's about the date or time.

Maybe we could instead use pendulums format to provide an easy way to localize a MayaDT instance given a specific format?

@alxwrd
Copy link
Contributor Author

alxwrd commented May 16, 2018

@timofurrer Sorry I hadn't done slang_date yet as I was unsure of how to proceed.

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 format for slang_date, we lose 'yesterday', 'today', and 'tomorrow' as pendulum doesn't have support for this. I think that would be a shame.

The options I can see to fully have internationalisation:

  1. Try and restart humanize. Contact the owner about updating/adding collaborator, or a fork?
  2. Use humanize as is, but only get 3 locales.
  3. Suggest the addition of date_for_humans() for pendulum.
  4. Use pendulum as is, but lose 'yesterday', 'today', and 'tomorrow'.

@alxwrd alxwrd force-pushed the support-locales branch from fcf7dcb to 3760af2 Compare May 21, 2018 21:38
@alxwrd
Copy link
Contributor Author

alxwrd commented May 21, 2018

@timofurrer I've added localisation for slang_date if you could let me know what you think.

I've used a mix of pendulums .format() and the translation data from dateparser. This has allowed both "today, tomorrow, yesterday", and dates like "25 Mai".

@timofurrer
Copy link
Collaborator

@alxwrd sorry for the delay.
Looks good to me. Do you mind adding some tests for the i18n features?

@alxwrd
Copy link
Contributor Author

alxwrd commented May 26, 2018

@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.

@alxwrd alxwrd changed the title [wip] Allow slang methods to change locale Allow slang methods to change locale May 26, 2018
@timofurrer
Copy link
Collaborator

Awesome! Thanks for the contribution! 🎉

@timofurrer timofurrer merged commit 9964e93 into kennethreitz:master May 26, 2018
@alxwrd alxwrd deleted the support-locales branch May 26, 2018 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants