Visitar URL original
Make the gettext functions accept a localedir of type Traversable · Issue #149809 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20261031090809/https://github.com/python/cpython/issues/149809
Skip to content

Make the gettext functions accept a localedir of type Traversable #149809

Open

Description

@Jesse205

Feature or enhancement

Proposal:

from gettext import bindtextdomain, find, translation
import importlib.resources

bindtextdomain("demo-zipapp", importlib.resources.files("demo_zipapp").joinpath("locales"))
translation("demo-zipapp", importlib.resources.files("demo_zipapp").joinpath("locales"))
find("demo-zipapp", importlib.resources.files("demo_zipapp").joinpath("locales"))

In ZipApp, since files within the application cannot be read directly through traditional file APIs, it is necessary to use importlib.resources.files to read them.

importlib.resources.files returns a Traversable object, which is not supported by gettext.

Here is a demo app: demo-zipapp.zip (source) | demo-zipapp.zip (zipapp)

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/make-the-gettext-functions-accept-a-localedir-of-type-traversable/107330

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 13, 2026
  2. picnixz commented on May 13, 2026

    @picnixz
    Member

    I feel that special casing this for Traversal is not a good idea. Is Traversal a protocol class? if so, it is ok, but otherwise we should check for protocol-like classes instead IMO.

    just to understand: the purpose is to allow usage of zipapp files wiith gettext and avoid habing those files on the disk right?

  3. Jesse205 commented on May 13, 2026

    @Jesse205
    Author

    Traversable is a protocol class:

    Image

    Because of this, Path is also considered a Traversable:

    Image

    So I explicitly excluded PathLike in the code (Path is also a PathLike type, and the path needs to use os.path.abspath to get the absolute path):

    Image Image

    just to understand: the purpose is to allow usage of zipapp files wiith gettext and avoid habing those files on the disk right?

    Right!

  4. picnixz commented on May 15, 2026

    @picnixz
    Member

    I wonder why we actually need to support zipapp for gettext in the stdlib. Is it common? because it looks like this could make code a bit less readable. I see value in the fact that zipapp allows you to avoid extracting them so that you can share them without installing them as a pip package for instance, but OTOH, this also looks like it is more for a third-party extension.

    To be clear: I am +0 but I do not know if this burdens gettext maintainers and if there are subtleties that are annoying.

    cc @StanFromIreland for translating feedback.

  5. Jesse205 commented on May 15, 2026

    @Jesse205
    Author

    I wonder why we actually need to support zipapp for gettext in the stdlib. Is it common?

    In practice this is indeed uncommon, because the limitation only applies to non‑self‑extracting zipapps. Most users prefer to package and distribute their applications using tools like PyInstaller; distributing applications as zipapps is very rare. Among those who do use zipapps, some employ self‑extracting tools like shiv, which directly bypass the restriction that gettext cannot read content inside a zipapp.

    this also looks like it is more for a third-party extension.

    I actually tried to implement the find and translation methods myself. But in the end I discovered a problem: some other people's code (e.g., argparse) may directly rely on functions defined in gettext. Therefore I had to replace the functions defined in the standard library gettext before importing those modules.


    In fact, the only external code that uses gettext that I rely on is argparse. Or maybe I was wrong from the beginning – I should file an issue to make argparse accept a passed‑in NullTranslations, rather than filing an issue to make the gettext standard library methods accept Traversable objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions