Offline (in-memory) translation classes.
Classes
|
Format specification for translations strings. |
|
Base class for application of |
|
Default format applier implementation. |
|
Storage class for fetched translations. |
|
Immutable mapping for translated IDs. |
Bases: object
Format specification for translations strings.
Translation formats are similar to regular f-strings, with two important exceptions:
Positional arguments (
'{}') may not be used; correct form is'{key-name}'.Substrings surrounded by
'[]'denote an optional element.
fmt – A translation fstring.
Examples
A format string with an optionl element.
>>> from rics.translation.offline import Format
>>> fmt = Format('{id}:{name}[, nice={is_nice}]')
The Format class when used directly only returns required placeholders by default..
>>> fmt.fstring(), fmt.fstring().format(id=0, name='Tarzan')
('{id}:{name}', '0:Tarzan')
..but the placeholders attribute can be used to retrieve all placeholders, required and optional:
>>> fmt.placeholders
('id', 'name', 'is_nice')
>>> fmt.fstring(fmt.placeholders), fmt.fstring(fmt.placeholders).format(id=1, name='Morris', is_nice=True)
('{id}:{name}, nice={is_nice}', '1:Morris, nice=True')
Pattern which denotes placeholder elements in format strings.
Create a format string for the given placeholders.
placeholders – Keys to keep. Passing None is equivalent to passing required_placeholders.
positional – If True, remove names to return a positional fstring.
An fstring with optional elements removed unless included in placeholders.
KeyError – If required placeholders are missing.
Parse a format.
fmt – Input to parse.
A Format instance.
Bases: ABC, Generic[IdType, NameType, SourceType]
Base class for application of Format specifications.
translations – Matrix of ID translation components returned by fetchers.
ValueError – If default is given and any placeholder names are missing.
Return translation source.
Bases: FormatApplier
Default format applier implementation.
Bases: Mapping, Generic[NameType, SourceType, IdType]
Storage class for fetched translations.
source_translations – Fetched translations {source: PlaceholderTranslations}.
name_to_source – Mappings {name: source}, but may be overridden by the user.
fmt – A translation format. Must be given to use as a mapping.
default_fmt – Alternative format specification to use instead of fmt for fallback translation.
default_fmt_placeholders – Per-source default placeholder values.
Notes
Type checking of fmt and default_fmt_placeholders attributes may fail due to https://github.com/python/mypy/issues/3004
alias of DefaultFormatApplier
Create translations for names. Note: __getitem__ delegates to this method.
name – A name to translate.
fmt – Format to use. If None, fall back to init format.
default_fmt – Alternative format for default translation. Resolution: Arg -> init arg, fmt arg, init fmt arg
Translations for name as a dict {id: translation}.
ValueError – If fmt=None and initialized without fmt.
KeyError – If trying to translate name which is not known.
Return translation sources.
Return name-to-source mapping.
Return the format specification to use instead of fmt for fallback translation.
Return the default translations used for default_fmt_placeholders placeholders.
Return reversed mode status flag.
If set, the mappings returned by
apply()(and therefore also__getitem__are reversed.
Reversal status flag.
Make a copy of this TranslationMap.
Immutable mapping for translated IDs.
If default_value is given, it is used as the default answer for any calls to __getitem__ where the key is not in translated_ids.
real_translations – A dict holding real translations.
default_value – A string with exactly one or zero placeholders.
Modules
Types used for offline translation. |