Skip to content

Commit

Permalink
Merge pull request #4 from dario-baumberger/feature/additional-transl…
Browse files Browse the repository at this point in the history
…ation-commands

Feature/additional translation commands
  • Loading branch information
friebetill authored Oct 16, 2023
2 parents 2b565b8 + 8d2a0ba commit 9d3a5f3
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 10 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ Translate your texts with [DeepL](https://www.deepl.com/) in [Obsidian](https://

## Commands

Currently there is one command for translating selected texts `DeepL: Translate selection`. You can set the language from and to which you want to translate in the settings. Later, more commands are added.
| Command | Description |
| -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Translate selection | You can set the language from and to which you want to translate in the settings. |
| Translate selection: to language | The target language can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation. |
| Translate selection: From a language to another | The source and target languages can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation. |
| Translate selection: To language and append to selection | The target language can be selected by suggestion modal. The translation will be appended to the selection. |

## Requirements

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "deepl",
"name": "DeepL",
"version": "1.0.5",
"version": "1.0.6",
"minAppVersion": "0.15.0",
"description": "Allows translation of selected texts into more than 25 languages with DeepL.",
"author": "Till Friebe",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-deepl",
"version": "1.0.5",
"version": "1.0.6",
"description": "Allows translation of selected texts into more than 25 languages with DeepL for Obsidian.md.",
"main": "main.js",
"scripts": {
Expand Down
41 changes: 41 additions & 0 deletions src/deepl/translateModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { App, SuggestModal } from "obsidian";

interface Language {
code: string;
name: string;
}

export class TranslateModal extends SuggestModal<Language> {
constructor(
app: App,
placeholder: string,
public languages: Language[],
public callback: (result: Language) => void
) {
super(app);
this.setPlaceholder(placeholder);
}

getSuggestions(query: string): Language[] {
if (query) {
return this.languages.filter(
(language) =>
language.code.toLowerCase().includes(query.toLowerCase()) ||
language.name.toLowerCase().includes(query.toLowerCase())
);
}

return this.languages;
}
renderSuggestion(language: Language, el: HTMLElement) {
el.createEl("div", { text: `${language.name} (${language.code})` });
}
onChooseSuggestion(language: Language) {
this.callback(language);
}

onClose() {
let { contentEl } = this;
contentEl.empty();
}
}
137 changes: 136 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { DeepLException } from "./deepl/deeplException";
import { DeepLService } from "./deepl/deeplService";
import {
DeepLPluginSettings,
defaultSettings
defaultSettings,
} from "./settings/pluginSettings";
import { SettingTab } from "./settings/settingTab";
import { addStatusBar } from "./settings/statusbar";
import { TranslateModal } from "./deepl/translateModal";
import { fromLanguages } from "./deepl/fromLanguages";

export default class DeepLPlugin extends Plugin {
public deeplService: DeepLService;
Expand Down Expand Up @@ -49,6 +51,139 @@ export default class DeepLPlugin extends Plugin {
}
},
});

this.addCommand({
id: "deepl-translate-selection-append",
name: "Translate selection: To language and append to selection",
editorCallback: async (editor: Editor) => {
if (editor.getSelection() === "") {
return;
}

const selection = editor.getSelection();

new TranslateModal(
app,
"To",
Object.entries(toLanguages).map(([code, name]) => ({
code,
name,
})),
async (language) => {
try {
const translation =
await this.deeplService.translate(
selection,
language.code,
this.settings.fromLanguage
);
editor.replaceSelection(
`${selection} ${translation[0].text}`
);
} catch (error) {
if (error instanceof DeepLException) {
new Notice(error.message);
} else {
console.error(error, error.stack);
new Notice(
"An unknown error occured. See console for details."
);
}
}
}
).open();
},
});

this.addCommand({
id: "deepl-translate-selection-to",
name: "Translate selection: to language",
editorCallback: async (editor: Editor) => {
if (editor.getSelection() === "") {
return;
}

new TranslateModal(
app,
"To",
Object.entries(toLanguages).map(([code, name]) => ({
code,
name,
})),
async (language) => {
try {
const translation =
await this.deeplService.translate(
editor.getSelection(),
language.code,
this.settings.fromLanguage
);
editor.replaceSelection(translation[0].text);
} catch (error) {
if (error instanceof DeepLException) {
new Notice(error.message);
} else {
console.error(error, error.stack);
new Notice(
"An unknown error occured. See console for details."
);
}
}
}
).open();
},
});

this.addCommand({
id: "deepl-translate-selection-from-to",
name: "Translate selection: From a language to another",
editorCallback: async (editor: Editor) => {
if (editor.getSelection() === "") {
return;
}

new TranslateModal(
app,
"From",
Object.entries(fromLanguages).map(([code, name]) => ({
code,
name,
})),
async (from) => {
new TranslateModal(
app,
"To",
Object.entries(toLanguages).map(([code, name]) => ({
code,
name,
})),
async (to) => {
try {
const translation =
await this.deeplService.translate(
editor.getSelection(),
to.code,
from.code
);
editor.replaceSelection(
translation[0].text
);
} catch (error) {
if (error instanceof DeepLException) {
new Notice(error.message);
} else {
console.error(error, error.stack);
new Notice(
"An unknown error occured. See console for details."
);
}
}
}
).open();
}
).open();
},
});
}

async loadSettings() {
Expand Down
26 changes: 23 additions & 3 deletions src/settings/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,29 @@ export class SettingTab extends PluginSettingTab {
text: "Commands",
});

containerEl.createEl("p", {
text: 'Translate selection: Translates the selected text from the "From language" to the "To language".',
});
new Setting(containerEl)
.setName("Translate selection")
.setDesc(
'Translates the selected text from the "From language" to the "To language".'
);

new Setting(containerEl)
.setName("Translate selection: to language")
.setDesc(
"The target language can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation."
);

new Setting(containerEl)
.setName("Translate selection: From a language to another")
.setDesc(
"The source and target languages can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation."
);

new Setting(containerEl)
.setName("Translate selection: To language and append to selection")
.setDesc(
"The target language can be selected by suggestion modal. The translation will be appended to the selection."
);

containerEl.createEl("h4", {
text: "Language settings",
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"1.0.2": "0.15.0",
"1.0.3": "0.15.0",
"1.0.4": "0.15.0",
"1.0.5": "0.15.0"
"1.0.5": "0.15.0",
"1.0.6": "0.15.0"
}

0 comments on commit 9d3a5f3

Please sign in to comment.