Paul R
Paul R

Reputation: 2797

Get locale from timezone -python, django, pytz

Is it possible to get locale from timezone?

Like 'en-US' from timezone 'America/Chicago'.

Upvotes: 4

Views: 723

Answers (1)

georgeofallages
georgeofallages

Reputation: 504

pytz.country_timezones is your friend.

You can find a list of languages by country here: http://download.geonames.org/export/dump/countryInfo.txt

You can use the country_timezones to map the django "America/Chicago" style timzeones to a country code, and then map the country code to the list of languages from geonames.

If this seems like a pain, I've done it for you. https://github.com/georgemitchell/stackoverflow/tree/master/timezone_to_locale

Look at timezone_to_locale.py for details.

>>> from timezone_to_locale import locale_by_timezone
>>> locale_by_timezone["America/Chicago"]
['en-US', 'es-US', 'haw', 'fr']

As you can see, it even handles the dilemma that Kevin raised.

Upvotes: 3

Related Questions