Translate block of text between view and state

Hello,

I am working on a Plugin for Obsidian (a markdown editor) and struggling to implement a translation feature. Also I am mostly limited to using editor extension for the desired effect.

Is it possible to achieve the following effect using CodeMirror editor extensions: Every occurrence of a block of text, marked by specific start and end characters, is translated into another language and then displayed in the view. If the text displayed in the view is edited, it should be written back to the document state in the original language.

I don’t want to worry about how I will translate the text yet. I just need a method to have different, but connected, text between the editor view and the document state.

You can replace pieces of the document with other content (using Decoration.replace), but that content won’t be editable, so this doesn’t sound like it would do what you want. I think you’ll need to do something like this at another level than the editor-view level—i.e. put the translated content in the editor state, possibly along with information about the position and origin of translated blocks, and do the translation outside of it.

1 Like