Reputation: 1009
I was programming a program in Python, where I need to output date as per user's locale:
Is there a way to get locale from county/timezone or some other method needs to be followed?
Or do I need to get the locale input from user itself?
EDIT
The program is to be a web-app. The user can provide me his country. But does he have to explicitly provide me the locale also or can I get it from his timezone/country?
Upvotes: 4
Views: 4938
Reputation: 33833
"Locale" is a country + language pair.
You have country + timezone. But no info about the language.
I don't think it's possible to convert country + timezone into a single 'correct' locale... in countries with multiple languages there is not a 1:1 relation between language and timezone.
The closest I can see is to use Babel:
from babel import Locale
Locale.parse('und_BR') # 'und' here means unknown language, BR is country
>>> Locale('pt', territory='BR')
This gives you a single 'most likely' (or default) locale for the country. To handle the languages properly you need to ask the user their preferred language.
Upvotes: 10