-
Notifications
You must be signed in to change notification settings - Fork 83
Translation Service
To ensure the availability of the plugin's service, the plugin supports multiple translation services and allows adding custom translation services via plugins. Users can select the desired translation service through the plugin configuration.
Translation Service | Free Service | Additional Cost | Tutorial |
---|---|---|---|
Google Translate | Unlimited Free | - | Built-in service, network issues in some regions, excessive use in LAN may cause IP rate limiting |
Bing Translate | Unlimited Free | - | Built-in service |
Tencent TranSmart | Unlimited Free | - | Recommended usage, good effect and response. TranSmart Plugin |
Alibaba Translation | 1 million characters per month | 50 CNY/1 million characters | Built-in. Apply for API key here |
DeepL | 500,000 characters per month | 4.99 Euros base fee per month + 20 Euros/1 million characters | DeepL Plugin, Apply for API key here |
Tencent Cloud | 5 million characters per month | 58 CNY/1 million characters | Tecent Cloud Plugin, Apply for API key here |
Chatgpt | None | $0.002 / 1K tokens(GPT 3.5) | ChatGPT Reverse Proxy API Support, ChatGPT, Apply for API key here |
Note: The above costs are for reference only. The actual fees should be based on the service provider's official website. Other translation services provided by plugins can be found in the plugin marketplace by searching @tag:translateSource
.
Given the many translation services, especially the effectiveness of various AI large models over traditional translation services, we also support adding custom translation services via plugins. Developers can refer to Deepl example to implement custom services.
A translation service plugin exposes the translation service to the Comment Translate plugin. It needs to implement the ITranslate
interface and register the service at the plugin's entry point. When users select a translation service, the plugin-configured title and keywords will be displayed. When users switch to the plugin, it will start and send the translation request to the plugin.
Note: The same plugin can contribute multiple translation service items, and existing plugins can also contribute translation services.
- Create Plugin: Create a VSCode plugin project, Documentation
-
Declare Service: Declare the translation service configuration in
package.json
. The configuration key istranslates
, declaring the service name and title, withkeywords
set totranslateSource
, so it can be searched."contributes": { "translates": [ { "translate": "deepl", "title": "DeepL translate" } ] }, "keywords": [ "translateSource", "comment translate", "deepl", "deepl translate", "translation", "comments", "翻訳" ]
-
Implement Translation Service: Implement the
ITranslate
interface, and realize the translation logicimport { ITranslate, ITranslateOptions } from 'comment-translate-manager'; export class DeepLTranslate implements ITranslate { get maxLen() { // Maximum length limit for word request return 5000; } async translate(content: string, options: ITranslateOptions): Promise<string> { // Implement translation logic } async link(content: string, options: ITranslateOptions): Promise<string> { // Implement additional link logic } // ... }
-
Register Service: Plugin entry
extension.ts
, register serviceimport { ITranslateRegistry } from 'comment-translate-manager'; import { DeepLTranslate } from './deepl-translate'; export function activate(context: vscode.ExtensionContext) { //Expose the plug-in return { extendTranslate: function (registry: ITranslateRegistry) { registry('deepl', DeepLTranslate); } }; }
-
Debug Plugin: Debug and run the plugin, execute
Change translation source
to invoke the service configuration page, select the registered translation service, and check the translation effect. - Publish Plugin: Publish the plugin to share it with more users. Documentation