Expose catalog in gettext.GNUTranslations

The GNUTranslations class has a private _catalog variable. If renamed without the underscore prefix, or expsed in some other way using a property or function, the GNUTranslations class can be used as a general purpose gettext parser. Currently, it seems only possible to obtain translations for known messages (gettext, ngettext, etc.).

It is an implementation detail. It is a dictionary. The key can be either a string or a pair of a string and an integer. The string itself can be a msgid, or msgctxt + "\x04" + msgid. This is all an implementation detail, and by exposing it as a public attribute we have to freeze the current structure, which can prevent future extensions or optimizations.

It is also possible to implement the class without reading the whole catalog in memory, but by reading the requested message on the fly (the format supports index).

1 Like

Hi, thank you for your response. Good point about not needing to read the entire file into memory. I guess I need to mostly copy parse() into my application then. Or would you say a function to parse a .mo file into some data structure would fit in the standard library? GNUTranslations could then use that function, at least as long as it loads the entire catalog at once.