Skip to content

gettext.install() ignores previous call to locale.setlocale() #64243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fmoreau mannequin opened this issue Dec 21, 2013 · 9 comments
Open

gettext.install() ignores previous call to locale.setlocale() #64243

fmoreau mannequin opened this issue Dec 21, 2013 · 9 comments
Labels
stdlib Python modules in the Lib dir triaged The issue has been accepted as valid by a triager.

Comments

@fmoreau
Copy link
Mannequin

fmoreau mannequin commented Dec 21, 2013

BPO 20044
Nosy @jwilk, @abadger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2013-12-21.16:15:20.607>
labels = ['library']
title = 'gettext.install() ignores previous call to locale.setlocale()'
updated_at = <Date 2019-05-12.14:29:47.179>
user = 'https://bugs.python.org/fmoreau'

bugs.python.org fields:

activity = <Date 2019-05-12.14:29:47.179>
actor = 'a.badger'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2013-12-21.16:15:20.607>
creator = 'fmoreau'
dependencies = []
files = []
hgrepos = []
issue_num = 20044
keywords = []
message_count = 2.0
messages = ['206762', '342258']
nosy_count = 3.0
nosy_names = ['jwilk', 'a.badger', 'fmoreau']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue20044'
versions = ['Python 2.7']

Linked PRs

@fmoreau
Copy link
Mannequin Author

fmoreau mannequin commented Dec 21, 2013

It seems that gettext.install() uses environment variables such as LANGUAGE, to find out which language it should use to find the translation file.

This means that any local settings done by setlocale() previoulsy are ignored.

I don't think it's the case with the C implementation.

@fmoreau fmoreau mannequin added the stdlib Python modules in the Lib dir label Dec 21, 2013
@abadger
Copy link
Mannequin

abadger mannequin commented May 12, 2019

I tested a small C program and found that setlocale takes precedence for LC_ALL, LC_MESSAGES, and LANG but not for LANGUAGE.

int main(int argc, char **argv) {
    char *message1;
//setlocale (LC_ALL, "");
setlocale (LC_ALL, "pt_BR.utf-8");
bindtextdomain ("testc", "/srv/python/cpython/tmp");
textdomain ("testc");
    message1 = gettext("lemon");
    printf("%s\n", message1);
    return 0;
}
$ LC_ALL=es_MX.utf-8 LANGUAGE= LC_MESSAGES=es_MX.utf-8 LANG=es_MX.utf-8 ./test
limão

$ LANGUAGE=es_MX  LANG=es_MX.utf-8 ./test
limón

So this could be considered a bug in the stdlib's gettext. If we fix it, we'll need to make sure that we continue to honor LANGUAGE, though.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@StanFromIreland
Copy link
Contributor

StanFromIreland commented Feb 23, 2025

  • locale.setlocale calls _setlocale which sets the variable for the process
  • gettext.find checks the environment variables: LANG, LANGUAGES, LC_ALL, LC_MESSAGES by os.environ.get()

gettext.find should be modified to fallback to locale.getlocale() which checks the variable for the python process.

@picnixz picnixz added triaged The issue has been accepted as valid by a triager. type-bug An unexpected behavior, bug, or error labels Feb 25, 2025
@picnixz
Copy link
Member

picnixz commented Feb 25, 2025

cc @tomasr8

@tomasr8
Copy link
Member

tomasr8 commented Feb 25, 2025

I think it makes sense to align our gettext implementation with C, that is, let the result of locale.getlocale override all the environment variables except for LANGUAGE.

In any case, this part of gettext has no dedicated tests. I have a patch ready which tests gettext._expand_lang but more tests will be needed also for gettext.find. Once there is sufficient coverage we can look into fixing this

@StanFromIreland
Copy link
Contributor

Testing will not be too simple, environment variables are not something we can really control.

@tomasr8
Copy link
Member

tomasr8 commented Feb 25, 2025

I don't think it'll be that bad, we have EnvironmentVarGuard for that :)

@StanFromIreland
Copy link
Contributor

Can I write some tests for find() or do you already have some ready?

@tomasr8
Copy link
Member

tomasr8 commented Feb 27, 2025

Can I write some tests for find() or do you already have some ready?

Feel free! I will open a PR for _expand_lang but I haven't started on find yet. I opened #130655 for it

@encukou encukou removed the type-bug An unexpected behavior, bug, or error label Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir triaged The issue has been accepted as valid by a triager.
Projects
Status: No status
Status: No status
Development

No branches or pull requests

4 participants