Skip to main content

TranslationCatalog

Simulate a dict for DjangoTranslation._catalog so as multiple catalogs with different plural equations are kept separate.

Attributes

AttributeTypeDescription
_catalogslist of dictsA list of translation dictionaries used to store and retrieve localized strings across multiple domains or plural forms.
_pluralslist of callablesA list of lambda functions or callables used to determine the correct plural form index for a given numeric value.

Constructor

Signature

def TranslationCatalog(
trans: object = None
) - > null

Parameters

NameTypeDescription
transobject = NoneAn optional translation object containing a catalog and a plural function to initialize the instance.

Methods


items()

@classmethod
def items() - > generator

Yields all message ID and translation pairs from all internal catalogs. This provides a flattened view of all available translations across different pluralization rules.

Returns

TypeDescription
generatorAn iterable of (key, value) tuples for all translations.

keys()

@classmethod
def keys() - > generator

Yields all message IDs present across all internal catalogs. Useful for auditing or listing all translatable strings currently loaded.

Returns

TypeDescription
generatorAn iterable of all message IDs.

update()

@classmethod
def update(
trans: [DjangoTranslation](djangotranslation.md?sid=django_utils_translation_trans_real_djangotranslation)
) - > null

Merges a new translation object into the catalog. If the plural logic matches the current primary catalog, it merges the data; otherwise, it prepends a new catalog level.

Parameters

NameTypeDescription
trans[DjangoTranslation](djangotranslation.md?sid=django_utils_translation_trans_real_djangotranslation)The translation object containing a catalog and a pluralization function to be merged.

Returns

TypeDescription
null

get()

@classmethod
def get(
key: string,
default: any = None
) - > any

Attempts to retrieve a translation for the specified key, returning a default value if the key is not found in any catalog. This avoids the KeyError raised by the bracket operator.

Parameters

NameTypeDescription
keystringThe message ID to look up.
defaultany = NoneThe value to return if the message ID does not exist in any catalog.

Returns

TypeDescription
anyThe translated string if found, otherwise the provided default value.

plural()

@classmethod
def plural(
msgid: string,
num: integer
) - > string

Retrieves the correct plural form of a translation based on the provided count. It iterates through catalogs and applies their specific pluralization logic until a match is found.

Parameters

NameTypeDescription
msgidstringThe base message ID to translate.
numintegerThe numerical value used to determine which plural form to use.

Returns

TypeDescription
stringThe translated string corresponding to the specific plural form of the message.