| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- class translation:
- """ This ciass is responsible for single translation in the library.
- This store single translation, and its state. Could be casted to string
- and formated. When translated phrase must contain variables, that
- could be passed by format function, and `#{ name }` in the phrase.
- Attributes
- ----------
- text : str
- Phrase content as string.
- valid : bool
- True when phrase was translated correctly or False when not.
- Methods
- -------
- format(params: dict) -> dict
- That method could format translated phrase with given dict.
- """
- def __init__(self, content: str, success: bool = True) -> None:
- """ This create new translaed phrase.
-
- It require string content of the translated phrase, and also state
- of the translation. When phrase was translated successfull, then
- state is True but when phrase could not being found, state is False.
- Parameters
- ----------
- content : str
- Content of the translated phrase.
- success : bool, default: True
- State of the translation.
- """
- self.__success = success
- self.__content = content
- def __str__(self) -> str:
- """ This returns content of the phrase.
- Returns
- -------
- str
- Content of the translated phrase.
- """
- return self.__content
- @property
- def text(self) -> str:
- """ String content of the phrase.
- Returns
- -------
- str
- Content of the translated phrase.
- """
- return self.__content
- @property
- def valid(self) -> bool:
- """ This returns that phrase was translated propertly.
- Returns
- -------
- bool
- True when phrase was translated propertly or false when not.
- """
- return self.__success
- def format(self, params: dict) -> str:
- """ This format translated phrase by inserting given items into it.
- Parameters
- ----------
- params : str
- Items to insert into translated string.
- Returns
- -------
- str
- Translated content with inserted given values.
- """
- if not self.__success:
- return self.__content
- parts = self.__content.split("#{")
- results = parts.pop(0)
- for count in parts:
- elements = count.split("}")
- if len(elements) == 1:
- results = results + count
- continue
- name = elements.pop(0).strip()
- rest = str("}").join(elements)
- if not name in params:
- results = results + rest
- continue
- results = results + str(params[name]) + rest
- return results
|