From 01e6edfbc41d7271122cfd098a89c18ba3bb0999 Mon Sep 17 00:00:00 2001 From: eabdyushev Date: Wed, 13 Jul 2022 11:53:41 +0600 Subject: [PATCH 1/2] Added the ability to translate text not separated by spaces Example: ("SearchDuplicatesServiceName") --- src/extension.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 376c4b0..6b09de5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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) { @@ -121,30 +122,31 @@ function translateSelection(selection: vscode.Selection | vscode.Range): void { } return; } - let selectedText: string = activeEditor.document.getText(new vscode.Range(selection.start, selection.end)); + let selectedText: string[] = activeEditor.document.getText(new vscode.Range(selection.start, selection.end)).split(/(?=[A-Z])/); + let selectedTextResult: string = selectedText.join(" "); if (!languages) { vscode.window.showErrorMessage('Go to user settings and edit "googleTranslateExt.languages".'); return; } if (typeof languages === 'string') { - translate(selectedText, selection, languages); + translate(selectedTextResult, 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, selection, quickPick.selectedItems[0].label); + translate(selectedTextResult, selection, quickPick.selectedItems[0].label); quickPick.dispose(); }); quickPick.onDidHide(() => quickPick.dispose()); quickPick.show(); } else { - translate(selectedText, selection, languages[0]); + translate(selectedTextResult, selection, languages[0]); } } else { languages.forEach((language: string) => { - translate(selectedText, selection, language); + translate(selectedTextResult, selection, language); }); } } From e9e435833e2e6bd56b2f3b3e1c37b7375a4f240a Mon Sep 17 00:00:00 2001 From: eabdyushev Date: Mon, 18 Jul 2022 00:24:35 +0600 Subject: [PATCH 2/2] Added the ability to translate text not separated by spaces Example: ("SearchDuplicatesServiceName") Fix --- src/extension.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 6b09de5..ecaa89a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -122,8 +122,8 @@ function translateSelection(selection: vscode.Selection | vscode.Range): void { } return; } - let selectedText: string[] = activeEditor.document.getText(new vscode.Range(selection.start, selection.end)).split(/(?=[A-Z])/); - let selectedTextResult: string = selectedText.join(" "); + + 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; @@ -151,6 +151,19 @@ function translateSelection(selection: vscode.Selection | vscode.Range): void { } } +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) => {