TextConstantApi
Die nachfolgenden Inhalte sind nur in englischer Sprache verfügbar.
The text_constant
module lets you fetch and format text constants
Functions
Function | Parameters | Return Value | Description |
---|---|---|---|
cache_clear() | Clears the text constant cache | ||
get_formatted_text_constant(_id, language=None, **kwargs) | _id: CONST language: Sprachcode kwargs: Arguments to replace the placeholders in the text constant | The text constant with the optional arguments formatted into the text | Fetches the text constant and replace placeholders. When no argument was given for a placeholder it is simply ignored. Text constants are only fetched once and then cached. |
get_text_constant(_id, language= | _id: CONST language: Sprachcode default: A string to return when no text constant with the given id exists | The text constant | Fetches a text constant. When default is None and no constant exists with that id, a ValueError is raised. Text constants are only fetched once and then cached. |
safe_format(text, **formatting_parameters) | text: A string with format parameters like 'Hello {user}' formatting_parameters: Arguments to replace the placeholders in the text | The text with placeholders replaced | Safely formats a string without raising an error when not all placeholders were replaced |
Examples
Using safe_format
from ppms.text_constant import safe_format
text = "The quick brown {jumpee} jumped over the lazy {animal}"
# If you used text.format() then it would raise a KeyError
formatted_text = safe_format(text, jumpee='fox')
# formatted_text is now "The quick brown fox jumped over the lazy {animal}"