Building a Desktop Translation Tool with LLM APIs: From Requirements to Implementation
This article describes how to build a personal desktop translation tool on top of a large language model API. It covers requirements scoping, technology choices, module layering, prompt design, streaming output, error handling, local API key storage, performance tuning, and packaging for distribution.
Starting From a Personal Need
In daily development work, we often run into scattered translation needs: reading foreign-language documentation, handling multilingual product copy, and understanding technical material that is not in our native language. Traditional online translation services are convenient, but they have three main pain points. First, privacy: source and translated text pass through a third-party service, so sensitive content may leak. Second, a fragmented experience: switching between editors, browsers, and document apps makes it hard to fit translation results smoothly into the workflow. Third, weak customization: general-purpose translation engines handle domain-specific terms and jargon poorly, which lowers accuracy in professional settings.
As large language models become widely available, calling a public model API for high-quality translation has become a mature and efficient approach. This article uses “building a personal translation tool for your own computer” as its starting point. It walks through the full process of requirements analysis, technology selection, implementation, packaging, and release, and aims to provide a practical engineering reference for developers with similar needs.
1. Requirements and Feasibility Analysis: Set Clear Boundaries and Avoid Over-Engineering
Before writing any code, the first task is to define what problem the tool is meant to solve. The core requirements are:
- Focused functionality: As a desktop translation tool, its core action is “input text, return translation.” It does not include marginal features such as OCR or speech recognition.
- Controllable privacy: Users configure their own API keys. Source and translated text are transmitted only between the user’s device and the model service, and no logs are stored locally.
- Cross-platform operation: Development targets Windows, while keeping the option to port to other platforms open.
- Lightweight deployment: The target users are not professional developers, so the installer must remove manual setup of environment dependencies (such as the .NET runtime) and let users launch the app with a double-click.
Based on these requirements, the technology choices mainly consider three points:
- UI stack: Prioritize rapid iteration. Choose a desktop framework with a mature front-end ecosystem, plenty of community resources, and a complete packaging toolchain. After comparing options, a cross-platform desktop solution based on web technologies (HTML, CSS, and JavaScript) fits well. Its ecosystem already offers ample UI component libraries and build tools, which can significantly shorten interface development time.
- LLM integration: Call the general text generation capability of a large model through a standard HTTP RESTful API. Use a system prompt to constrain the output format, so the model returns only the translation without extra explanation.
- API key management: Users enter their API key in the app’s settings panel, and the key is saved to a local configuration file. The design includes no authentication proxy or cloud-hosted logic, which avoids extra server costs.
Design decision: The first version leaves out complex features such as automatic switching between multiple engines or terminology management. It focuses on making the core flow reliable and easy to use. This follows the minimum viable product (MVP) principle.
2. System Architecture: Clear Module Boundaries
The application is divided into the following logical modules:
- UI layer: Renders interactive elements such as the text input area, translation output area, language direction selector, and settings panel.
- Logic control layer: Listens for user events, builds prompts, schedules API requests, and handles streaming responses.
- Service layer: Wraps calls to the model API, including authentication, request construction, timeout control, and error parsing.
- Data layer: Reads and writes local configuration, such as the API key and default target language, and persists the optional translation history.
This layering lets each module be tested independently. If the UI framework or the model service changes later, only the corresponding layer needs to be modified.
3. Core Implementation: From Text to a Working Solution
1. Designing a High-Quality Translation Prompt and Parameters
The quality of translation output depends heavily on the system prompt. A poor example is sending the plain text “Translate the following: Hello world,” where the model may return explanations or extra content. To prevent this, the system prompt includes the following constraints:
- Clear role: The model is set as a “professional translation engine” focused on accurately conveying meaning.
- Clear task: Output only the translation in the target language. Do not add any explanation, notes, or the original text.
- Clear terminology handling: When professional terms appear, follow the conventional expressions of the target language.
A reference implementation is shown below:
def build_messages(source_text, target_lang):# The system prompt is written in Chinese in the original code. It asks the model# to translate the user's text into the target language and return only the# translation, with no explanation, prefix, or suffix.system = {"role": "system","content": ("你是一名专业的翻译引擎。请将用户提供的文本翻译为" +target_lang + "。只返回翻译结果,不要添加任何解释、前缀或后缀。")}user = {"role": "user", "content": source_text}return [system, user]
For inference parameters, a lower temperature (for example, 0.1 to 0.3) is recommended. It produces more deterministic output and reduces random variation.
2. Streaming Output and Cancellation: Reducing Waiting Anxiety
For longer texts, if the result appears only after the model finishes generating everything, users may face a blank wait of several seconds. Streaming responses solve this. The app receives content in chunks as the model produces it and appends each chunk to the interface. The experience feels closer to real-time typing, which reduces waiting anxiety.
The implementation is straightforward. Turn on the streaming option in the API request parameters, then parse each data chunk in the callback and update the text state.
```javascript
// Example: handling streaming parsing
stream.on(‘data’, chunk => {
const lines = chunk.toString().split(‘\n’).filter(l =>
