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

Added the ability to translate text not separated by spaces Example: ("SearchDuplicatesServiceName") Fix #31

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function onActivate(): void {
vscode.window.showErrorMessage('Must select text to translate');
return;
}

activeEditor = vscode.window.activeTextEditor;
initMembers();
if (selections.length > 1) {
Expand Down Expand Up @@ -121,34 +122,48 @@ function translateSelection(selection: vscode.Selection | vscode.Range): void {
}
return;
}
let selectedText: string = activeEditor.document.getText(new vscode.Range(selection.start, selection.end));

let selectedTextResult: string = splitStringByUpperCaseCharacters(activeEditor.document.getText(new vscode.Range(selection.start, selection.end)));
if (!languages) {
vscode.window.showErrorMessage('Go to user settings and edit "googleTranslateExt.languages".');
return;
}
if (typeof languages === 'string') {
translate(selectedText, <vscode.Selection>selection, languages);
translate(selectedTextResult, <vscode.Selection>selection, languages);
} else if (replaceText) {
if (languages.length > 1) {
const quickPick = vscode.window.createQuickPick();
quickPick.items = languages.sort().map((label: string) => ({ label }));
quickPick.placeholder = 'Select a language...';
quickPick.onDidAccept(() => {
translate(selectedText, <vscode.Selection>selection, quickPick.selectedItems[0].label);
translate(selectedTextResult, <vscode.Selection>selection, quickPick.selectedItems[0].label);
quickPick.dispose();
});
quickPick.onDidHide(() => quickPick.dispose());
quickPick.show();
} else {
translate(selectedText, <vscode.Selection>selection, languages[0]);
translate(selectedTextResult, <vscode.Selection>selection, languages[0]);
}
} else {
languages.forEach((language: string) => {
translate(selectedText, <vscode.Selection>selection, language);
translate(selectedTextResult, <vscode.Selection>selection, language);
});
}
}

function splitStringByUpperCaseCharacters(selectedText : string): string{
let removeSpecialChars = selectedText.replace(/[&\/\\#,+()$~%.'":*?<>{}_-]/g, ' ');

if (removeSpecialChars === removeSpecialChars.toUpperCase()) {
return removeSpecialChars;
}

return removeSpecialChars
.split(/(?=[A-ZА-Я]+?(?=[a-z]))/)
.join(' ')
.replace(/( )(?=[A-Z]*( ))/g, '');
}

function translate(textToTranslate: string, selection: vscode.Selection, language: string): void {
translateApi(textToTranslate, { to: language, client: 'gtx' })
.then((res: any) => {
Expand Down