Skip to content

Translation Service

正豪 edited this page Sep 19, 2024 · 6 revisions

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. Switch Translation Service

Translation Service List

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. image

Custom Extension of Translation Services

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.

Developing a Translation Service Plugin

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.

  1. Create Plugin: Create a VSCode plugin project, Documentation
  2. Declare Service: Declare the translation service configuration in package.json. The configuration key is translates, declaring the service name and title, with keywords set to translateSource, so it can be searched.
     "contributes": {
         "translates": [
             {
                 "translate": "deepl",
                 "title": "DeepL translate"
             }
         ]
     },
     "keywords": [
         "translateSource",
         "comment translate",
         "deepl",
         "deepl translate",
         "translation",
         "comments",
         "翻訳"
     ]
  3. Implement Translation Service: Implement the ITranslate interface, and realize the translation logic
    import { 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
       }
    
       // ...
    }
  4. Register Service: Plugin entry extension.ts, register service
    import { 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);
           }
       };
    }
  5. 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. Service Selection
  6. Publish Plugin: Publish the plugin to share it with more users. Documentation