id_translation.Translator.translate#
- Translator.translate(translatable, names=None, *, ignore_names=None, copy=True, override_function=None, max_fails=1.0, reverse=False, fmt=None, io_kwargs=None)[source]#
Translate IDs to human-readable strings.
- Simplified process:
The
mapmethod performs name-to-source mapping (seeDirectionalMapping).The
fetchmethod extracts IDs to translate and retrieves data (seeTranslationMap).Finally, the
translatemethod 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. 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 adicton the form{name_in_translatable: source_to_use}.ignore_names – Names not to translate, or a predicate
(NameType) -> bool.copy – If
False, translate in-place and returnNone.override_function – A callable
(name, sources, ids) -> Source | None. SeeMapper.apply()for details.max_fails – 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 stringsuch as ‘{id}:{name}’ use. Default isTranslator.fmt.io_kwargs – Used to initialize the IO implementation.
- Returns:
A translated copy of translatable if
copy=True, 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 max_fails is not a valid fraction.
TooManyFailedTranslationsError – If translation fails for more than max_fails of IDs.
ConnectionStatusError – If
reverse=Truewhile theTranslatoris online.UserMappingError – If override_function returns a source which is not known, and
mapper.on_unknown_user_override != 'ignore'.
See also
The
ID_TRANSLATION_DISABLEDvariable.