Skip to content

Commit

Permalink
Merge pull request #8 from benshabatnoam/1.2.0
Browse files Browse the repository at this point in the history
Replace selected text with translation #4
  • Loading branch information
Noam Ben Shabat authored Mar 27, 2018
2 parents 06a9756 + 48ae914 commit 86a30ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Select the text that your want to translate and execute `Translate` command.
> Tip: Use the shortcut `Ctrl+Shift+t` to translate the selected text.
## Features
* Multi languages:<br>you can translate your code to multi languages simply by adding the desired language code to `googleTranslateExt.languages` string array configuration.
* <u>Multi languages:</u><br>Translate your code to multi languages simply by adding the desired language code to `googleTranslateExt.languages` string array configuration.
* <u>Replace Text</u>:<br>Replace selected text with translated text by configuring `googleTranslateExt.replaceText` to 'true'.

## Language code table
<table>
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"default": "",
"description": "Type your google API key in order to make google translate extension to work."
},
"googleTranslateExt.replaceText": {
"type": "boolean",
"default": false,
"description": "Set to true in order to replace selected text with the translated text."
},
"googleTranslateExt.languages": {
"type": "array",
"items": {
Expand Down
36 changes: 29 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

var selectionStart: vscode.Position;
var selectionEnd: vscode.Position;

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
Expand All @@ -12,8 +15,9 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.showErrorMessage('Must select text to translate');
return;
}
let selection = editor.selection;
let selectedText = editor.document.getText(new vscode.Range(selection.start, selection.end));
selectionStart = editor.selection.start;
selectionEnd = editor.selection.end;
let selectedText = editor.document.getText(new vscode.Range(selectionStart, selectionEnd));
if (!selectedText) {
vscode.window.showErrorMessage('Must select text to translate');
return;
Expand All @@ -27,10 +31,17 @@ export function activate(context: vscode.ExtensionContext) {
}
if (typeof languages === "string") {
googleTranslate.translate(selectedText, languages, onTranslated.bind(languages));
} else {
languages.forEach((language: string) => {
googleTranslate.translate(selectedText, language, onTranslated.bind(language));
});
}
else {
let replaceText = vscode.workspace.getConfiguration('googleTranslateExt')['replaceText'];
if (replaceText) {
googleTranslate.translate(selectedText, languages[0], onTranslated.bind(languages[0]));
}
else {
languages.forEach((language: string) => {
googleTranslate.translate(selectedText, language, onTranslated.bind(language));
});
}
}
});
context.subscriptions.push(disposable);
Expand All @@ -45,10 +56,21 @@ function onTranslated(this: typeof String, err: any, translation: any): void {
vscode.window.showErrorMessage(error.error.message);
}
else if (diffLanguages(translation.detectedSourceLanguage, this.toString())) {
vscode.window.showInformationMessage(translation.translatedText);
let replaceText = vscode.workspace.getConfiguration('googleTranslateExt')['replaceText'];
if (replaceText) {
let editor = vscode.window.activeTextEditor;
editor.edit(replaceSelectedText.bind(translation.translatedText))
}
else {
vscode.window.showInformationMessage(translation.translatedText);
}
}
}

function replaceSelectedText(this: typeof String, editBuilder: vscode.TextEditorEdit) {
editBuilder.replace(new vscode.Range(selectionStart, selectionEnd), this.toString());
}

function diffLanguages(detectedSourceLanguage: string, translateToLanguage: string): boolean {
if (translateToLanguage === "iw")
translateToLanguage = "he";
Expand Down

0 comments on commit 86a30ec

Please sign in to comment.