Skip to content

Commit

Permalink
feat: Update note auto translation extension - EXO-73673 - Meeds-io/M…
Browse files Browse the repository at this point in the history
…IPs#128 (#89)

Update note auto translation extension
  • Loading branch information
hakermi committed Sep 4, 2024
1 parent 1be75d0 commit 547897b
Showing 1 changed file with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
previouslyTranslatedVersion: null,
autoTranslatedContent: null,
autoTranslatedTitle: null,
hasContent: false
autoTranslatedSummary: null,
};
},
props: {
Expand Down Expand Up @@ -80,23 +80,32 @@ export default {
this.isAutoTranslating = true;
this.$automaticTranslationExtensionsService.fetchAutoTranslation(this.note.title).then(translated => {
this.handleTranslatedTitle(translated.translation);
if (this.note?.content) {
this.hasContent = true;
const content = this.excludeHtmlSpaceEntities(this.note.content);
this.$automaticTranslationExtensionsService.fetchAutoTranslation(content).then(translated => {
this.handleTranslatedContent(translated.translation);
this.setAutoTranslationSelected();
this.previouslyTranslatedVersion = this.selectedTranslation;
this.isAutoTranslating = false;
}).catch(() => this.isAutoTranslating = false);
const summary = this.note?.properties?.summary;
if (summary) {
this.$automaticTranslationExtensionsService.fetchAutoTranslation(summary).then(translated => {
this.handleTranslatedSummary(translated.translation);
if (this.note?.content) {
this.fetchContentTranslation();
}
});
} else if (this.note?.content) {
this.fetchContentTranslation();
} else {
this.hasContent = false;
this.setAutoTranslationSelected();
this.isAutoTranslating = false;
}
}).catch(() => this.isAutoTranslating = false);
}
},
fetchContentTranslation() {
const content = this.excludeHtmlSpaceEntities(this.note.content);
this.$automaticTranslationExtensionsService.fetchAutoTranslation(content).then(translated => {
this.handleTranslatedContent(translated.translation);
this.setAutoTranslationSelected();
this.previouslyTranslatedVersion = this.selectedTranslation;
this.isAutoTranslating = false;
}).catch(() => this.isAutoTranslating = false);
},
excludeHtmlSpaceEntities(content) {
return content.replace(/&nbsp;/gi, '<span class="notranslate">&nbsp;</span>');
},
Expand All @@ -113,6 +122,9 @@ export default {
if (this.autoTranslatedContent) {
this.handleTranslatedContent(this.autoTranslatedContent);
}
if (this.autoTranslatedSummary) {
this.handleTranslatedSummary(this.autoTranslatedSummary);
}
this.updateSelectedTranslation(this.autoTranslation);
},
updateNoteContent(content) {
Expand All @@ -121,6 +133,9 @@ export default {
updateNoteTitle(title) {
this.$root.$emit('update-note-title', title);
},
updateNoteSummary(summary) {
this.$root.$emit('update-note-summary', summary);
},
updateSelectedTranslation(translation) {
this.$root.$emit('update-selected-translation', translation);
},
Expand All @@ -129,13 +144,18 @@ export default {
this.autoTranslatedContent = this.autoTranslatedTitle = null;
this.updateNoteTitle(this.note.title);
this.updateNoteContent(this.note.content);
this.updateNoteSummary(this.note?.properties?.summary);
this.updateSelectedTranslation(this.previouslyTranslatedVersion);
this.isResetAutoTranslating = false;
},
handleTranslatedTitle(translatedText) {
this.autoTranslatedTitle = translatedText;
this.updateNoteTitle(translatedText);
},
handleTranslatedSummary(translatedText) {
this.autoTranslatedSummary = translatedText;
this.updateNoteSummary(translatedText);
},
handleTranslatedContent(translatedText) {
this.autoTranslatedContent = this.restoreHtmlSpaceEntities(translatedText);
this.updateNoteContent(this.autoTranslatedContent);
Expand Down

0 comments on commit 547897b

Please sign in to comment.