How can I get the should-be-outputted text from a list of Translation
objects?
#1242
-
... with It probably isn't that hard to get |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Okay this is the method... Note that Note: if you use Plover's Note: See also How can I use Plover as a library from an independent script? · Discussion #1292 · openstenoproject/plover if you get some error. from plover_build_utils.testing import CaptureOutput
from plover.translation import Translation, Translator
from plover.formatting import Formatter
from plover.steno import Stroke
output = CaptureOutput()
formatter = Formatter(); formatter.set_output(output)
translator = Translator(); translator.add_listener(formatter.format)
# optional:
translator.set_min_undo_length(100)
# optional (set existing text, might affect the behavior of suffix):
output.text = ""
# optional (set dictionary to be used by the translator, if `translate_stroke` is used)
dictionary = translator.get_dictionary()
dictionary.set_dicts([StenoDictionary()])
# translate translations
translator.translate_translation(translation)
# alternatively (without undoing `translation.replaced`)
# NOTE private method, might change any time!
translator._do(translation)
# alternatively (no need for the translator)
# although then you will have to handle macros/context (prev) yourself
formatter.format(undo=[], do=[
Translation(...),
Translation(...),
], prev=None)
# required if the translator is used
translator.flush()
# get output:
print(output.instructions)
# format: (NOTE might change between versions)
# ("b", int): backspace * number
# ("s", str): string
# ("c", str): key combination
# ("e", str): engine command
print(output.text) |
Beta Was this translation helpful? Give feedback.
Okay this is the method...
Note that
Translation
objects does not handle macros (macros operate onTranslation
objects), useTranslation.translate_macro(macro)
orTranslation.translate_stroke(stroke)
instead if that's necessary.Note: if you use Plover's
Translation
objects, make sure to not pass it directlyFormatter.format()
(instead, copy it withTranslation(translation.strokes, translation.english))
), so theformatting
attribute is not destroyed/modified (which is important to ensure that the undo actions are correct).Note: See also How can I use Plover as a library from an independent script? · Discussion #1292 · openstenoproject/plover if you get some error.