-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add new automatic append translation feature and newline toggle #12
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,35 @@ export default class DeepLPlugin extends Plugin { | |
}, | ||
}); | ||
|
||
this.addCommand({ | ||
id: "translate-and-append-selection-automatically", | ||
name: "Translate selection: Automatically append selection", | ||
editorCallback: async (editor: Editor) => { | ||
const selection = editor.getSelection(); | ||
if (selection === "") { | ||
new Notice("No text selected."); | ||
return; | ||
} | ||
|
||
try { | ||
const translation = await this.deeplService.translate( | ||
selection, | ||
this.settings.toLanguage, | ||
this.settings.fromLanguage | ||
); | ||
const textToAppend = this.settings.appendNewLine ? '\n' + translation[0].text : translation[0].text; | ||
editor.replaceSelection(selection + textToAppend); | ||
} catch (error) { | ||
if (error instanceof DeepLException) { | ||
new Notice(error.message); | ||
} else { | ||
console.error(error, error.stack); | ||
new Notice("An unknown error occurred. See console for details."); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
this.addCommand({ | ||
id: "deepl-translate-selection-to", | ||
name: "Translate selection: to language", | ||
|
@@ -135,54 +164,45 @@ export default class DeepLPlugin extends Plugin { | |
}); | ||
|
||
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(); | ||
}, | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can only specify one language in the settings. What if I want to translate from another language that is not set? That was the reason I added this functionality back then. I would miss this, if there are better solutions, I am very much in favour of them. The command There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not removing it.. It's only adding a new option without the modal. Not changing the existing modal function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my GitHub diff view, I see that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, now that I'm at the computer and can review this.. you're right actually. This is a mistake, and I appreciate you caught this! It's also super embarrassing.. 🤦♂️ I did not mean to remove this function. This would not be an improvement, and as you say I would miss this function also. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect, then we're talking about the same thing now :) No worries, these things happen. Glad if I could help and it no longer seems like random input on my part. |
||
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 | ||
); | ||
// Append a newline if the setting is enabled | ||
const textToAppend = this.settings.appendNewLine ? `\n${translation[0].text}` : translation[0].text; | ||
editor.replaceSelection(`${selection}${textToAppend}`); | ||
} catch (error) { | ||
if (error instanceof DeepLException) { | ||
new Notice(error.message); | ||
} else { | ||
console.error(error, error.stack); | ||
new Notice( | ||
"An unknown error occurred. See console for details." | ||
); | ||
} | ||
} | ||
} | ||
).open(); | ||
}, | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,10 @@ export class SettingTab extends PluginSettingTab { | |
"The target language can be selected by suggestion modal. The translation will be appended to the selection." | ||
); | ||
|
||
new Setting(containerEl) | ||
.setName("Translate selection: Automatically append selection") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is already respected as long as you select the right command |
||
.setDesc("The translation will be appended to the selection using the 'To language' setting automatically, without the suggestion modal."); | ||
|
||
containerEl.createEl("h4", { | ||
text: "Language settings", | ||
}); | ||
|
@@ -106,6 +110,18 @@ export class SettingTab extends PluginSettingTab { | |
}) | ||
); | ||
|
||
new Setting(containerEl) | ||
.setName("When appending text, add a new line before appending the translation?") | ||
.setDesc("If enabled, adds a new line before appending the translated text.") | ||
.addToggle((toggle) => | ||
toggle | ||
.setValue(this.plugin.settings.appendNewLine) | ||
.onChange(async (value) => { | ||
this.plugin.settings.appendNewLine = value; | ||
await this.plugin.saveSettings(); | ||
}) | ||
); | ||
|
||
containerEl.createEl("h4", { | ||
text: "Authentication settings", | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good if the setting for the new line is respected in all commands that keep the original text.
For example: deepl-translate-selection-append
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only changed it for the setting(s) which appended. I didn't want to change existing functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can understand that. But it would make sense to me if all functions that append would also respect the setting of the new line. With your change, not all would be the same. If the default for the setting is false, it does not change anything for existing functions, unless you set it to true. From a user's perspective, it would irritate me if one command made a new line and another did not. I would therefore adapt it everywhere for the sake of consistency.
But that's just my opinion, I'm not the owner. Not meant to sound offensive, sorry if it was :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No no! I am new to this, and I'm not immune from mistakes or criticisms. Also I appreciate you taking the time to reply!
I'm of course a bit excited since this is really my first pull request, and I shouldn't expect it to be perfect or even accepted.
Regarding the new line, you are right. I was focused on the append parts, and I didn't take too much care of the other functions either. I just translate a lot of things, and I didn't want the existing stuff to be overwritten, and I needed a fast way to compare translations as well. Thus this 'stacked' method and a focus on the appending.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It takes a lot of conviction to change someone else's code, so a certain amount of conviction is necessary. The same with commenting and reviewing. We are all not free from errors. As long as we can talk about it, it's good, we learn something new every day.
And I can understand that from your perspective. I tried to take a different perspective. If we're of the same opinion now, I'd say the discussion was worthwhile :)