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
Activity
fmoreau commented on 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.
abadger commented on 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.
//setlocale (LC_ALL, ""); setlocale (LC_ALL, "pt_BR.utf-8"); bindtextdomain ("testc", "/srv/python/cpython/tmp"); textdomain ("testc");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.
StanFromIreland commented on Feb 23, 2025
gettext.find should be modified to fallback to locale.getlocale() which checks the variable for the python process.
picnixz commented on Feb 25, 2025
cc @tomasr8
tomasr8 commented on 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 commented on Feb 25, 2025
Testing will not be too simple, environment variables are not something we can really control.
tomasr8 commented on Feb 25, 2025
I don't think it'll be that bad, we have EnvironmentVarGuard for that :)
StanFromIreland commented on Feb 27, 2025
Can I write some tests for find() or do you already have some ready?
tomasr8 commented on Feb 27, 2025
Feel free! I will open a PR for _expand_lang but I haven't started on find yet. I opened #130655 for it