In DOC, translation-oriented plugins typically have two interaction patterns:
- Selection translation: the user selects a piece of text in a document; the plugin reads the selected text and translates it.
- Full-page translation: the plugin reads the full text of a specified page and performs full-page translation or summarization.
This chapter focuses on two PluginDocAPI APIs and shows how to compose them into a usable plugin flow.
Selection Translation (Selected Text)
Call PluginDocAPI.getSelectedText to get the currently selected text.
You must select text in DOC before calling this API; otherwise it returns an error.
Selection translation works best as a lightweight popup or a toolbar button: show source/translated text, and provide follow-up actions such as copy, replace, and search.
Full-Page Translation (Specified Page Text)
Call PluginDocAPI.getCurrentDocText to get the text content of a specified page in the currently opened document. Based on it, you can do full-page translation, key-point extraction, terminology highlighting, and more.
Page text can be long. Consider splitting it before translating (e.g., by line breaks or sentences), and control concurrency and retries to avoid failures caused by oversized requests.
Error Handling and UX Recommendations
- Permissions/timing: selection translation requires selected text; full-page translation requires DOC to be open and the page number to be valid.
- Empty results: if selection text or page text is empty, show a friendly message instead of failing silently.
- Interaction pacing: selection translation should respond immediately; full-page translation should show loading state and support cancel/retry.