TranslationCatalog
Simulate a dict for DjangoTranslation._catalog so as multiple catalogs with different plural equations are kept separate.
Attributes
| Attribute | Type | Description |
|---|---|---|
| _catalogs | list of dicts | A list of translation dictionaries used to store and retrieve localized strings across multiple domains or plural forms. |
| _plurals | list of callables | A 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
| Name | Type | Description |
|---|---|---|
| trans | object = None | An 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
| Type | Description |
|---|---|
generator | An 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
| Type | Description |
|---|---|
generator | An 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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| key | string | The message ID to look up. |
| default | any = None | The value to return if the message ID does not exist in any catalog. |
Returns
| Type | Description |
|---|---|
any | The 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
| Name | Type | Description |
|---|---|---|
| msgid | string | The base message ID to translate. |
| num | integer | The numerical value used to determine which plural form to use. |
Returns
| Type | Description |
|---|---|
string | The translated string corresponding to the specific plural form of the message. |