translate.js
Introduction
translate.js is an AI-powered i18n solution designed for frontend developers.
It focuses on seamless multilingual switching capabilities—just two lines of JavaScript to automatically translate your entire HTML page.
With support for over 100 languages, you can switch languages with a single click. No need to modify your page, no language config files required.
It also supports dozens of fine-tuning directives to meet all your customization needs, and is SEO-friendly.
translate.js is open-sourced under the MIT License: GitHub | Gitee — completely free and commercially usable.
This article will guide you through how to enable multilingual switching on your website using the API service provided by SiliconFlow.
Model Optimization
When it comes to translation, you might immediately think of a typical scenario: entering a sentence into a text box, followed by something like “Translate this into XXX language.”
But this is not how it works here.
Large language models offer significantly better translation quality compared to traditional machine translation. However, in real-world online applications, stable and reliable output is essential.
This system has undergone deep optimization and multiple layers of self-check and correction specifically for practical translation scenarios using large (and small) models. The goal is to ensure consistent and dependable performance. For a more in-depth discussion, see here.
In simple terms, using SiliconCloud, you can translate irregular sentences, mixed-language texts, and grammatically awkward phrases. The output not only offers smoother readability than traditional machine translation, but also matches its lightning-fast response speed.
Quick Start
No prior setup or registration is required — you can get started with just a few lines of JavaScript.
Code
Simply add the following code to the bottom of your existing HTML file to enable integration:
Description
Simply add the line translate.service.use('siliconflow');
in your JavaScript to use the free Qwen3-8B translation model provided by SiliconCloud.
We’ve deeply integrated SiliconCloud, so no further configuration is required.
translate.js is supported by server sponsorship from ZhiDianYun, which converts large language models into translation APIs and makes them available for use in translate.js.
With server nodes across Asia, Europe, and the Americas, it ensures smooth performance for users worldwide.
If you run into any issues during integration, you can report them here, or join the QQ group (1034085260) to contact the author for help.
Fine-Tuning Commands
translate.js offers a wide range of advanced commands, allowing precise customization to meet various user demands — even the most unusual ones. (If something’s missing, feel free to suggest it in the QQ group above.)
Examples include:
- Adjusting the position, style, visibility, and language options of the language selector
- Setting a default target language
- Creating a custom glossary
- Automatically triggering actions after translation completes
- Using your own private translation service endpoint
- Monitoring dynamically rendered content and translating it automatically
- Auto-switching to the user’s preferred language
- Controlling language switching via JavaScript
- Translating only specific elements
- Ignoring specific elements from being translated
- Excluding specific text from translation
- Translating text within images
- Mouse-hover (highlight) translation
- Automatically translating content loaded via AJAX
- Translating only selected languages
- Translating attributes of specific HTML tags
For more commands and documentation, click here to view the full list.
Private Deployment with SiliconCloud
You can register your own account at SiliconCloud and deploy this service privately to enable more advanced configurations and significantly improve the user experience.
Advantages
- Translation Quality: You can connect to larger models provided by SiliconCloud, such as Qwen3-32B, to produce more fluent translations and deliver an outstanding reading experience.
- Lightning-Fast Response: Enable memory caching to dramatically increase API response speed.
- Usage Limits: Set usage limits for a specific domain (or API key). This is especially useful for network service providers who want to sell translation services in a tiered package model.
- Management Interface: Use the open management API to monitor real-time usage data such as which domains or keys are active, how many characters have been translated, and more.
- Data Privacy: All API requests and cached data are hosted on your own server, ensuring complete data privacy.
- Concurrency Control: Freely define the maximum number of concurrent requests per second and configure the thread pool size when calling large models to greatly reduce interface latency. You can also use the management API to monitor real-time concurrency.
Deployment
1. Server Specifications
-
CPU: 1 core
-
Architecture: x86_64 (i.e., Intel CPU)
-
Memory: 1 GB
-
Operating System:
- CentOS 7.4 (Other 7.x versions like 7.6, 7.2 are also acceptable)
- openEuler 20 (If version 20 is not available, version 22 is also fine)
-
System Disk: Use the default system disk; no data disk is needed
-
Elastic Public IP: Pay by traffic (10 Mbps bandwidth recommended. For personal use with low translation volume, 1 Mbps bandwidth is sufficient)
-
Region: Choose a mainland China region for faster access to the SiliconCloud API.
For any unspecified options, simply choose the cheapest available.
2. One-Click Deployment
Run the following shell command to deploy with a single click:
3. Obtain the API Key
- Go to SiliconFlow and register an account.
- On the left side of the console, find Account Management > API Key, open it, and create a new one.
4. Configure the API Key
First, locate the configuration file /mnt/service/config.properties
and edit it. Find:
Comment this line out (this is the default trial service), and then add the following configuration:
Of course, there are more configuration options available to unlock advanced capabilities.
These can be ignored during the initial setup phase, but you may explore them as you become more familiar:
- Translation Accuracy Validation and Auto-Correction
- Configure Language Routing Strategy for Multiple Translation Channels
- Configure Traffic Control for Translation API Interface
- Configure Backup Translation Channels
- …
5. Restart the Project
After completing the above steps, restart the translate.service
by executing the restart command:
6. Access and Test
Simply visit your server’s IP address to see it in action:
Try switching to any language. If “Hello, world” gets translated after switching, it means the integration is successful and ready for use.
Usage
Text Translation API Interface
This service is powered by large model capabilities provided by the SiliconFlow platform, with translate.service acting as the middleware for dispatching.
You don’t need to understand what large models are or how to use them. A standard API translation interface is exposed for you to use directly.
Request URL: http://your-server-ip/translate.json
Request Method: POST
Request Parameters:
- to – Target language for translation. For example,
english
. More supported languages can be found by visiting http://your-server-ip/language.json - text – The text(s) to translate. Format should be a JSON array, e.g.,
["Hello", "World"]
.
It supports translating multiple different texts at once, and each text can be in a different language.
Response Example:
{"result":1,"info":"success","to":"english","text":["Hello","World"]}
Note:The Content-Type header should either be unset or set to application/x-www-form-urlencoded.
curl Translation Example:
To help you better understand how to use the API, here is a curl request example. You can copy and run it directly to see the effect:
It’s open and free — no need to purchase expensive text translation services anymore.
SiliconFlow provides it for free, with even better translation fluency!
Click here to view the detailed documentation of the text translation API interface
Web Page Multi-language Switching
To enable this feature, add the following code just before the closing </html>
tag at the end of your webpage.
Typically, a language selection <select>
dropdown will appear at the bottom of the page, allowing you to switch languages and see the effect in real time.
This way, the translation request interface will go through your own server.
For detailed documentation on manually specifying the translation API, refer to: http://translate.zvo.cn/4068.html
Additionally, you can download the translate.js
file and host it within your own project. It has no external dependencies and is written in pure, native JavaScript.
translate.js
is fully open-source, and you can always get the latest version from:
https://raw.githubusercontent.com/xnx3/translate/refs/heads/master/translate.js/translate.js
The core mechanism works by scanning the DOM elements on your web page to automatically identify and extract text content for translation.
This is why the translate.execute();
call must be placed at the very bottom of the page — it ensures that all DOM elements have been rendered before triggering the translation process across the entire page.
Log Viewing
You can check the relevant logs directly on your server.
Logs are stored in the /mnt/service/logs/
directory.
Text Translation Logs
Files named openai_yyyy-MM-dd.log
contain logs of text translations performed using the large model via the OpenAI interface provided by SiliconFlow.
For example: openai_2025-05-17.log
Here, yyyy-MM-dd
refers to the current date. A new log file is generated daily based on the date.
Each log entry records the original text, the translated text, the translation timestamp, the translation quality review score, whether the repair mechanism was triggered, and more.
Each line in the file represents one translation record.
Example:
- time — The timestamp when the event occurred
- originalText — The original text to be translated
- resultText — The translated text
- to — The target language for the translation
- score — The quality score of the translated text after verification, ranging from 1 to 100. Higher scores indicate more accurate translations
- useTime — The time taken for the translation using SiliconCloud, measured in milliseconds
System Operation Logs
translate.service.log
is the system operation log that records the runtime status of the translate.service itself.
Access Request Logs
request_yyyy-MM-dd.log
contains logs of translation requests made to /translate.json
.
The logs are not written immediately after each request; instead, they are buffered and written to the log file either when the number of entries reaches several hundred or when more than 2 minutes have passed since the last write operation.
Here are two sample entries:
- method — The API endpoint of the current request. For example,
translate.json
refers to the text translation API;language.json
refers to the API that retrieves the list of supported languages. - fileCache — For requests to the
translate.json
translation API, if a file cache hit occurs, this parameter records the name of the cached file, which is stored in/mnt/service/cache/
. - originalSize — For requests to the
translate.json
translation API, records the character count of the original text (source text, not the translation). - size — For requests to the
translate.json
translation API, records the character count of the translated text (translation, not the source). - ip — The IP address from which the request originated.
- domain — For requests to the
translate.json
translation API, if the website usestranslate.js
, this is the domain name of that website (automatically detected). - time — The timestamp when the request was triggered.
- to — For requests to the
translate.json
translation API, the target language of the translation. - key — For requests to the
translate.json
translation API, if you have assigned a translation key viadomain.json
configuration, this records that key. - memoryCacheHitsNumber — For requests to the
translate.json
translation API, if an in-memory cache hit occurs, this records the number of cache hits (sincetranslate.json
supports translating multiple texts at once). - memoryCacheHitsSize — For requests to the
translate.json
translation API, if an in-memory cache hit occurs, this records the number of characters hit in the cache (translation text character count, not the source).