Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support vscode-comment-translate #17

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ You can install it from [**here**](https://wata.github.io/konjac-farm/).

![feature configure](images/feature-configure.gif)

- This extension also functions as a translation source for the [Comment Translate extension](https://marketplace.visualstudio.com/items?itemName=intellsmi.comment-translate) .

## Extension Settings

This extension contributes the following settings:
Expand Down
77 changes: 47 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
"activationEvents": [
"*"
],
"keywords": [
"translate",
"google translate",
"translateSource",
"konjac"
],
"icon": "icon.png",
"main": "./out/extension.js",
"contributes": {
Expand Down Expand Up @@ -82,7 +88,13 @@
"command": "konjac.setclipboard"
}
]
}
},
"translates": [
{
"translate": "konjac",
"title": "Konjac translate"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
Expand All @@ -99,6 +111,7 @@
"@types/node": "16.x",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"comment-translate-manager": "^0.0.5",
"eslint": "^8.1.0",
"glob": "^7.2.0",
"mocha": "^9.2.2",
Expand Down
27 changes: 27 additions & 0 deletions src/KonjacTranslate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { ITranslate, ITranslateOptions } from "comment-translate-manager";
import { konjacFetch } from "./konjacFetch";
import * as vscode from 'vscode';

/** konjac for comment-translate-manager */
export class KonjacTranslate implements ITranslate{
maxLen: number = 3000;
translate(content: string, options: ITranslateOptions): Promise<string> {
const apiUrl = vscode.workspace.getConfiguration('konjac')['apiUrl'];
if (!apiUrl) {
return Promise.reject(new Error('Go to user settings and edit "konjac.apiUrl".'));
}
return konjacFetch({
apiUrl: apiUrl,
text: content,
target: options.to,
source: options.from,
});
}
link(content: string, { to='auto',from='auto' }: ITranslateOptions): string {
const text = encodeURIComponent(content).replace(/[()]/g, (char) => `%${char.charCodeAt(0).toString(16)}`);
return `[Konjac](https://translate.google.com/?op=translate&sl=${from}&tl=${to}&text=${text})`;
}
isSupported(): boolean {
return true;
}
};
33 changes: 14 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

import * as vscode from 'vscode';

const rp = require('request-promise-native');
const encodeUrl = require('encodeurl');
import type { ITranslateRegistry } from 'comment-translate-manager';
import { KonjacTranslate } from './KonjacTranslate';
import { konjacFetch } from './konjacFetch';

export function activate(context: vscode.ExtensionContext) {
const konjac = new Konjac();
Expand All @@ -12,6 +12,12 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('konjac.replace', () => konjac.replace()));
context.subscriptions.push(vscode.commands.registerCommand('konjac.setclipboard', () => konjac.setclipboard()));
context.subscriptions.push(vscode.commands.registerCommand('konjac.configure', () => konjac.configure()));
return {
//Expose the plug-in for comment-translate-manager
extendTranslate: (registry: ITranslateRegistry) => {
registry('konjac', KonjacTranslate);
}
};
}

class Konjac {
Expand Down Expand Up @@ -203,22 +209,11 @@ class Konjac {
if (!target) {
return Promise.reject(new Error('Go to user settings and edit "konjac.target".'));
}
const opts: any = {
uri: encodeUrl(apiUrl),
method: 'POST',
json: true,
followAllRedirects: true,
formData: { text: text, target: target },
transform2xxOnly: true,
transform: function (body: any) {
// Google Apps Script Execution API always returns 200
if (!body.data) {
throw new Error(body.message);
}
return body.data.translatedText;
}
};
return rp(opts);
return konjacFetch({
apiUrl,
text,
target,
});
}

dispose() {
Expand Down
42 changes: 42 additions & 0 deletions src/konjacFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const rp = require('request-promise-native');
const encodeUrl = require('encodeurl');

/**
* translate text using Konjac API
* @returns
*/
export function konjacFetch({
text,
apiUrl,
target,
source,
}: {
/** text to translate */
text: string;
/** api url of konjac */
apiUrl: string;
/** target language */
target?: string;
/** source language */
source?: string;
}): Promise<string> {
const formData : Partial<Record<"source"|"target"|"text", string>> = { text };
if(source && source !== "auto") { formData.source = source; }
if(target && target !== "auto") { formData.target = target; }
const opts: any = {
uri: encodeUrl(apiUrl),
method: 'POST',
json: true,
followAllRedirects: true,
formData,
transform2xxOnly: true,
transform: function (body: any) {
// Google Apps Script Execution API always returns 200
if (!body.data) {
throw new Error(body.message);
}
return body.data.translatedText;
}
};
return rp(opts).then((result: unknown) => String(result));
}