Is there any way, in Python 3, to find out the language used by the system?
Even a tricky one though, like: reading from a file in a sneaky directory, and finding the string 'ENG' or 'FRE' within the file's content…

  • 3
    Why the directory needs to be sneaky? Cannot it be a normal directory? – grochmal Jul 11 '16 at 22:31
  • 1
    the LANG environmental variable can give one a strong hint. in python3: import os; print(os.environ['LANG']) ...and i see: 'en_US.UTF-8' – Theophrastus Jul 12 '16 at 2:29
  • @grochmal I meant a directory like /etc/stuff/things, but if e.g. the ~/.bash_profile is enough that's even better – joH1 Jul 12 '16 at 9:51

Unix systems don't really have a “system language”. Unix is a multiuser system and each user is free to pick their preferred language. The closest thing to a system language is the default language that users get if they don't configure their account. The location of that setting varies from distribution to distribution; it's picked up at some point during the login process.

In most cases, what is relevant is not the “system language” anyway, but the language that the user wants the application to use. Language preferences are expressed through locale settings. The setting that determines the language that applications should use in their user interface is LC_MESSAGES. There are also settings for the date, currency, etc. These settings are conveyed through environment variables which are usually set when the user logs in from some system- and user-dependent file.

Finding a locale setting is a bit more complicated than reading the LC_MESSAGES variable as several variables come into play (see What should I set my locale to and what are the implications of doing so?). There's a standard library function for that. In Python, use locale.getlocale. You first need to call setlocale to turn on locale awareness.

import locale
locale.setlocale(locale.LC_ALL, "")
message_language = locale.getlocale(locale.LC_MESSAGES)[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.