有没有办法从国家或时区获取语言环境? [英] Is there a way to get locale from country or timezone?

查看:92
本文介绍了有没有办法从国家或时区获取语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写程序,我需要根据用户的语言环境输出日期:

I was programming a program in Python, where I need to output date as per user's locale:

  1. 获取按用户输入指定的国家/地区的时区列表(是否使用pytz)
  2. 获取用户的语言环境(我不知道该怎么做)

是否有从县/时区获取语言环境的方法,或者需要遵循其他方法?

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?

编辑

该程序应为网络应用程序.用户可以向我提供他的国家.但是他是否还必须明确向我提供语言环境,还是可以从他的时区/国家/地区获取它?

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?

推荐答案

我认为不可能将国家/时区转换为单个正确"的语言环境...在使用多种语言的国家/地区中,没有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.

这篇关于有没有办法从国家或时区获取语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆