I'm trying to let my users search for data by country. The users will type in the country name in their native languages. However my database only contains the alpha2 codes for each country.
My current approach:
user_input = "France"
country_code = pycountry.countries.get(name=user_input).alpha2 # u'FR'
this works fine if the user input is made in English. Since the data is shown in the users preferred language, she will expect to search for it in her preferred language as well.
By using gettext with pycountry's locales I'm able to show the country-names in the users preferred language:
# example for a German user
gettext.translation('iso3166', pycountry.LOCALES_DIR, languages=['de']).install()
country_code = 'FR'
country = _(pycountry.countries.get(alpha2=country_code).name) # 'Frankreich'
now I'm searching for a way to translate the users input back to english (assume there are no typos) and fetch the country code from it:
user_input = "Frankreich"
translated_user_input = ??? # 'France'
country_code = pycountry.countries.get(name=translated_user_input).alpha2 # u'FR'
Has anyone a good idea, how to achieve this? Ideally by only using gettext and pycountry?