如何在python中检测操作系统默认语言?

时间:2021-09-23 06:07:27

Is there any universal method to detect the OS default language? (regardless what is the OS that is running the code)

是否有任何通用方法来检测操作系统默认语言? (无论运行代码的操作系统是什么)

import os
os.getenv('LANG')

The above code works under Linux, does it work under other OS?

上面的代码在Linux下工作,它在其他操作系统下工作吗?

2 个解决方案

#1


20  

You could use the getdefaultlocale function in the locale module. It returns the language code and encoding of the system default locale in a tuple:

您可以在语言环境模块中使用getdefaultlocale函数。它返回元组中系统默认语言环境的语言代码和编码:

>>> import locale
>>> locale.getdefaultlocale()
('en_GB', 'cp1252')

#2


18  

Please, you cannot trust locale module for detecting the OS language !!!

请你不能相信locale模块检测OS语言!

Whoever used this information without verifying before, will have a program failing over the world, with those users whose OS language is not the same as the region language.

如果之前未经过验证就使用过这些信息的人将会有一个程序在世界范围内失败,而那些用户的操作系统语言与区域语言不同。

They are different, (1)the OS language and (2)the localization info.

它们是不同的,(1)OS语言和(2)本地化信息。

MSDN states that "A locale ID reflects the local conventions and language for a particular geographical region.", http://msdn.microsoft.com/en-us/library/8w60z792.aspx

MSDN声明“区域设置ID反映了特定地理区域的本地约定和语言。”,http://msdn.microsoft.com/en-us/library/8w60z792.aspx

and python docs,

和python文档,

"The POSIX locale mechanism allows programmers to deal with certain cultural issues in an application, without requiring the programmer to know all the specifics of each country where the software is executed." https://docs.python.org/2/library/locale.html

“POSIX语言环境机制允许程序员处理应用程序中的某些文化问题,而不需要程序员知道执行软件的每个国家的所有细节。” https://docs.python.org/2/library/locale.html

My Windows7 is in English. But I'm living in Spain so... my locale is 'es_ES'.. not 'en_EN'

我的Windows7是英文的。但是我住在西班牙......所以我的地方是'es_ES'..不是'en_EN'

I don't know a cross-platform way, for linux you've got it. For windows I'll give you:

我不知道跨平台的方式,对于linux你已经有了它。对于Windows我会给你:

Another post talks about using win32 GetSystemDefaultUILanguage, Find out the language windows was installed as.

另一篇文章讨论了使用win32 GetSystemDefaultUILanguage,找出安装的语言窗口为。

But if you want getting the windows language identifier, i recommend to use instead GetUserDefaultUILanguage(), because as stated en MSDN, will search recursively until reaches the language:

但是如果你想获得windows语言标识符,我建议使用GetUserDefaultUILanguage(),因为如MSDN所述,将递归搜索直到达到语言:

"Returns the language identifier for the user UI language for the current user. If the current user has not set a language, GetUserDefaultUILanguage returns the preferred language set for the system. If there is no preferred language set for the system, then the system default UI language (also known as "install language") is returned. For more information about the user UI language, see User Interface Language Management."

“返回当前用户的用户UI语言的语言标识符。如果当前用户尚未设置语言,GetUserDefaultUILanguage将返回系统的首选语言集。如果没有为系统设置首选语言,则系统默认返回UI语言(也称为“安装语言”)。有关用户UI语言的更多信息,请参阅用户界面语言管理。“

Code:

码:

>>> import locale
>>> locale.getdefaultlocale()
('es_ES', 'cp1252')            # <------------- Bad! I'm on english OS.

>>> import ctypes
>>> windll = ctypes.windll.kernel32
>>> windll.GetUserDefaultUILanguage()
1033
>>> locale.windows_locale[ windll.GetUserDefaultUILanguage() ]
'en_US'          # <----------- Good work

#1


20  

You could use the getdefaultlocale function in the locale module. It returns the language code and encoding of the system default locale in a tuple:

您可以在语言环境模块中使用getdefaultlocale函数。它返回元组中系统默认语言环境的语言代码和编码:

>>> import locale
>>> locale.getdefaultlocale()
('en_GB', 'cp1252')

#2


18  

Please, you cannot trust locale module for detecting the OS language !!!

请你不能相信locale模块检测OS语言!

Whoever used this information without verifying before, will have a program failing over the world, with those users whose OS language is not the same as the region language.

如果之前未经过验证就使用过这些信息的人将会有一个程序在世界范围内失败,而那些用户的操作系统语言与区域语言不同。

They are different, (1)the OS language and (2)the localization info.

它们是不同的,(1)OS语言和(2)本地化信息。

MSDN states that "A locale ID reflects the local conventions and language for a particular geographical region.", http://msdn.microsoft.com/en-us/library/8w60z792.aspx

MSDN声明“区域设置ID反映了特定地理区域的本地约定和语言。”,http://msdn.microsoft.com/en-us/library/8w60z792.aspx

and python docs,

和python文档,

"The POSIX locale mechanism allows programmers to deal with certain cultural issues in an application, without requiring the programmer to know all the specifics of each country where the software is executed." https://docs.python.org/2/library/locale.html

“POSIX语言环境机制允许程序员处理应用程序中的某些文化问题,而不需要程序员知道执行软件的每个国家的所有细节。” https://docs.python.org/2/library/locale.html

My Windows7 is in English. But I'm living in Spain so... my locale is 'es_ES'.. not 'en_EN'

我的Windows7是英文的。但是我住在西班牙......所以我的地方是'es_ES'..不是'en_EN'

I don't know a cross-platform way, for linux you've got it. For windows I'll give you:

我不知道跨平台的方式,对于linux你已经有了它。对于Windows我会给你:

Another post talks about using win32 GetSystemDefaultUILanguage, Find out the language windows was installed as.

另一篇文章讨论了使用win32 GetSystemDefaultUILanguage,找出安装的语言窗口为。

But if you want getting the windows language identifier, i recommend to use instead GetUserDefaultUILanguage(), because as stated en MSDN, will search recursively until reaches the language:

但是如果你想获得windows语言标识符,我建议使用GetUserDefaultUILanguage(),因为如MSDN所述,将递归搜索直到达到语言:

"Returns the language identifier for the user UI language for the current user. If the current user has not set a language, GetUserDefaultUILanguage returns the preferred language set for the system. If there is no preferred language set for the system, then the system default UI language (also known as "install language") is returned. For more information about the user UI language, see User Interface Language Management."

“返回当前用户的用户UI语言的语言标识符。如果当前用户尚未设置语言,GetUserDefaultUILanguage将返回系统的首选语言集。如果没有为系统设置首选语言,则系统默认返回UI语言(也称为“安装语言”)。有关用户UI语言的更多信息,请参阅用户界面语言管理。“

Code:

码:

>>> import locale
>>> locale.getdefaultlocale()
('es_ES', 'cp1252')            # <------------- Bad! I'm on english OS.

>>> import ctypes
>>> windll = ctypes.windll.kernel32
>>> windll.GetUserDefaultUILanguage()
1033
>>> locale.windows_locale[ windll.GetUserDefaultUILanguage() ]
'en_US'          # <----------- Good work