且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法从国家或时区获取语言环境?

更新时间:2022-01-17 23:18:15

我认为不可能将国家/时区转换为单个正确"的语言环境...在使用多种语言的国家/地区中,没有1:语言和时区之间有1种关系.

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.

我能看到的最接近的是使用 Babel :

The closest I can see is to use Babel:

from babel import Locale
Locale.parse('und_BR')  # 'und' here means unknown
>>> 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.