id_translation.Translator.translate#
- Translator.translate(translatable, names=None, *, ignore_names=None, inplace=False, override_function=None, maximal_untranslated_fraction=1.0, reverse=False, fmt=None)[source]#
Translate IDs to human-readable strings.
- Simplified process:
The
map
method performs name-to-source mapping (seeDirectionalMapping
).The
fetch
method extracts IDs to translate and retrieves data (seeTranslationMap
).Finally, the
translate
method applies the translations and returns to the caller.
See the Translation primer page for a detailed process description.
See also
π This is a key event method. Exit-events are emitted on the
βΉοΈINFO
-level if theTranslator
isonline
. Enter-events are always emitted on theπͺ²DEBUG
-level. See Key Event Records for details.- Parameters:
translatable β A data structure to translate.
names β Explicit names to translate. Derive from translatable if
None
. Alternatively, you may pass adict
on the form{name_in_translatable: source_to_use}
.ignore_names β Names not to translate, or a predicate
(NameType) -> bool
.inplace β If
True
, translate in-place and returnNone
.override_function β A callable
(name, sources, ids) -> Source | None
. SeeMapper.apply()
for details.maximal_untranslated_fraction β The maximum fraction of IDs for which translation may fail. 1=disabled.
reverse β If
True
, perform translations back to IDs. Offline mode only.fmt β A
format string
such as β{id}:{name}β use. Default isTranslator.fmt
.
- Returns:
A translated copy of translatable if
inplace=False
, otherwiseNone
.
Examples
Manual name-to-source mapping with a temporary name-only
Format
.>>> n2s = {"lions": "animals", "big_cats": "animals"} >>> translator.translate({"lions": 2, "big_cats": 2}, names=n2s, fmt="{name}") {'lions': 'Simba', 'big_cats': 'Simba'}
Name mappings must be complete; any name not present in the keys will be ignored (left as-is).
- Raises:
UntranslatableTypeError β If
type(translatable)
cannot be translated.MissingNamesError β If names are not given and cannot be derived from translatable.
MappingError β If any required (explicitly given) names fail to map to a source.
MappingError β If name-to-source mapping is ambiguous.
ValueError β If maximal_untranslated_fraction is not a valid fraction.
TooManyFailedTranslationsError β If translation fails for more than maximal_untranslated_fraction of IDs.
ConnectionStatusError β If
reverse=True
while theTranslator
is online.UserMappingError β If override_function returns a source which is not known, and
self.mapper.unknown_user_override_action != 'ignore'
.