Skip to content

Commit

Permalink
Merge pull request #12 from ubclaunchpad/default_region
Browse files Browse the repository at this point in the history
default region
  • Loading branch information
kevinrczhang authored Nov 9, 2024
2 parents 9c938a8 + b456cf9 commit d2851ba
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/i18nilize/services/locale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from geocoder import ip
from geopy.geocoders import Nominatim
from babel.languages import get_official_languages


def get_default_language():
# get user's coordinates based on ip address
g = ip('me')
coord = str(g.latlng[0]) + ", " + str(g.latlng[1])

# convert coordinates to country code
geolocator = Nominatim(user_agent="localization_launchpad")
location = geolocator.reverse(coord, exactly_one=True)
address = location.raw['address']
country_code = address["country_code"]

# pick the first (most popular) language
return get_official_languages(country_code)[0]

0 comments on commit d2851ba

Please sign in to comment.