text module
- class fluent_anvil.text.Text
Bases:
dict
This class models a static text for which there are multiple translations.
The primary use case of this class is best explained using an example: Imagine you are a German speaker living in Germany. Your first language is German and most of your colleagues at work speak German but a few of your colleagues only speak English. You want to write some technical text. You can explain it best in German, but you also want your English-speaking colleagues to understand it. Fortunately, the system you write your text in allows you to enter your text in multiple languages. This is where this class comes into play: It manages multiple translations of the same static text.
If you apply str(my_text_object) on your text, it will also automatically select the language the user prefers. You can also transmit the Text object between client and server because it is a portable class. You can save some bandwidth by using the slice() method before transmission so that only one or a small number of translations is transmitted.
- __init__(data: dict, validator: Validator = None)
Initialize the Text object.
- Parameters:
data – Dictionary with texts in different languages. The dictionary key must be an IETF language tag and the value the corresponding text for that language.
validator – The validator to use. See fluent_anvil.validators. You can also set it later using the Text.validator attribute.
- get(locale, default=None)
Return the value for key if key is in the dictionary, else default.
- is_valid(*args, **kwargs)
Validate the given text. Return True if validation passed. False, otherwise.
- Parameters:
args – Positional arguments to pass on to the validation function.
kwargs – Keyworded arguments to pass on to the validation function.
- slice(locales, count=1)
Create a Text with a subset of available locales.
This is used to save bandwidth during transmission by omitting locales that are not needed at the client. One simply provides a list of preferred locales, and the slice will be created using the count best matching locales.
- Parameters:
locales – The preferred locales to use.
count – The number of locales to include in the slice.
- validate(*args, **kwargs)
Validate the Text object. Raise a ValidationError if not successful.
- Parameters:
args – Positional arguments to pass on to the validation function.
kwargs – Keyworded arguments to pass on to the validation function.
- property locales
Return the available locales.