Technical Analysis of a Cross-Platform Text Selection Translation Tool Based on LLM APIs
This article examines the engineering design of a cross-platform text selection translation tool built on large language model (LLM) APIs. It covers the core problems such as trigger timing, context construction, multi-task reuse, and cross-environment consistency, followed by a five-layer architecture, prompt engineering for translation, polishing, and summarization modes, an abstraction for connecting multiple model providers, differences between browser extensions and desktop applications, and considerations for privacy, cost, and stability. Readers will gain a practical understanding of how to build a pluggable, template-driven foreign language assistance tool chain.
1. Technical Background and Problem Definition
Reading, editing, and reworking foreign-language content has long been a frequent pain point for knowledge workers. Traditional translation solutions rely on fixed corpora and statistical models, which perform poorly on long sentence structures, technical terminology, and contextual coherence. In contrast, solutions based on large language model (LLM) APIs complete tasks through natural language instructions and produce output that is closer to human expression.
From an engineering perspective, the core problems such tools must solve can be abstracted into four points:
- Trigger timing: how to invoke the capability with low latency after the user selects text;
- Context construction: how to organize scattered selected words into a prompt the model can understand;
- Multi-task reuse: how translation, polishing, summarization, and code explanation share the same invocation pipeline;
- Cross-environment consistency: how to align capability boundaries between browser extensions and desktop applications.
The open-source text selection translation tools described in the original material are built around these four points. They eventually evolved into a dual form consisting of a browser plugin and a desktop application covering Windows, macOS, and Linux, and the project name was changed once for brand identity compliance reasons. This suggests that beyond technology selection, naming and trademark compliance are also aspects that open-source projects must evaluate upfront.
2. Layered Architecture
A mature implementation is typically divided into five layers, with clear contracts decoupling the layers:
- Interaction layer: text selection monitoring, hotkeys, screenshot region selection, and side panel;
- Context layer: selected text cleaning, paragraph segmentation, automatic language detection, and glossary injection;
- Orchestration layer: task mode routing, prompt template rendering, and multi-turn session state management;
- Model access layer: a unified abstraction of authentication, requests, and streaming responses across different providers;
- Data layer: local persistence of vocabulary notebooks, history records, and user configuration.
The key benefit of layering is that adding a new task mode (for example, “rewrite in a more formal tone”) only requires registering a template in the orchestration layer, without changing the interaction or access code.
3. Prompt Engineering for Three Core Modes
Translation mode emphasizes a balance between fidelity and readability. The prompt must explicitly specify the target language, terminology priority, and format preservation (for example, keeping Markdown and code blocks untranslated).
Polishing mode takes the user’s own draft as input. The prompt should constrain the model to “not change the original meaning, only adjust grammar and pragmatics,” and require an explanation of differences so the user can judge whether the changes are reasonable.
Summarization mode requires control over the information compression rate, with output in three levels: “a one-sentence conclusion / a bullet list / a structured summary.”
Example template rendering logic:
TEMPLATES = {"translate": "Translate the following content into {target}, keeping code blocks and proper nouns in the original:\n{text}","polish": "Polish the following text without changing the original meaning, and list the main changes:\n{text}","summarize": "Summarize the following content in no more than {n} bullet points:\n{text}",}def render(mode: str, text: str, **kw) -> str:return TEMPLATES[mode].format(text=text, **kw)
From an engineering standpoint, it is recommended to store templates separately from code to facilitate A/B comparisons by language and by scenario.
4. Abstract Design for Multiple Provider Access
To avoid being locked into a single provider, the access layer should define a minimal interface, with each implementation filling in the differences (authentication headers, response structure, and streaming support):
class ChatProvider:def chat(self, messages, stream=False): ...class AzureStyleProvider(ChatProvider): # enterprise managed solution...class SelfHostedProvider(ChatProvider): # private deployment or open-weight solution...
Enterprise scenarios often choose model services hosted by cloud vendors, combined with key management, quotas, and access auditing. For scenarios sensitive to data leaving their domain, private deployment is preferred. In either case, the client should implement timeouts, retries, and fallback to avoid a single request failure blocking the reading flow.
5. Differences Between Browser Extensions and Desktop Applications
The advantage of browser extensions is in-place text selection with zero switching cost, but they are limited by page permissions and cross-origin policies, and usually need to proxy requests through a background script. Desktop applications can access the system clipboard, global hotkeys, and screenshot capabilities, so they can provide a more complete “screenshot text extraction—translation—backfill” pipeline and maintain a local vocabulary notebook that generates memory cards based on word frequency. A reasonable approach is to share the same orchestration and access code, and only adapt the interaction layer per platform.
6. Privacy, Cost, and Stability
- Detect and redact sensitive information before sending text;
- Cache high-frequency results locally to reduce repeated calls;
- Use streaming output to improve perceived latency;
- Record call volume and set budget limits per task mode.
7. Evolution Directions
The introduction of Chat Model and prompt template abstractions in versions such as v2.0, along with optimizations based on an orchestration framework, shows that the evolution of such tools follows a main line of “templating + pluggable models + local knowledge accumulation.” If an enterprise wants to quickly obtain equivalent capabilities, it can build an access layer based on the LLM service platform of Baidu AI Cloud, and then use general object storage to store historical corpora, along with logging services and monitoring alerts to ensure operational quality, thereby forming a sustainable foreign language assistance tool chain without being locked into a single implementation.
