[Tutor] locales and python

André Dahlqvist andre@beta.telenordia.se
Mon, 12 Jun 2000 23:54:27 +0200

Hi again everyone,

In an application that I'm writing I have separated out messages
displayed to the user in order to ease translation. These messages are
currently stored in dictionaries, named after their language. They look
something like:

# English dictionary
en = {
... }

Then I'm going to create such dictionaries for other languages as well,
and have a variable called lang set to the users locale. This is where
I have some questions. I have checked the library reference, and python
has a module called locale. All I would have to know is what locale the
user is currently using, and if there is a translation for this
language I want to assign this dictionary to the lang variable.

In the library reference it says that to get the users locale you use:

users_locale = locale.setlocale(locale.LC_ALL, "")

On my system this sets users_locale to:


The LC_CTYPE is correctly set to my locale sv_SE (because I have that
environment variable set.) So is it correct to assume that
locale.setlocale(locale.LC_CTYPE) will always return the locale that
the user is using? Also, there are different locales for the same
languages like 'sv' and 'sv_SE' for Swedish, 'en', 'en_US', 'en_GB' for
english, and so on. For my needs the same dictionary would work for
both british english and american english, and the same should apply to
other languages as well. So should I juse check the first two
characters of each locale?

// André