-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add automatic-translation extension for notes
- Loading branch information
Showing
5 changed files
with
155 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...resources/locale/portlet/automaticTranslation/automaticTranslationExtension_en.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
UIActivity.label.translate=Translate | ||
automaticTranslation.hideTranslation=Hide translation | ||
automaticTranslation.errorTranslation=Translation is not available for the moment | ||
notes.automatic.translation.label=Automatic translation |
135 changes: 135 additions & 0 deletions
135
.../src/main/webapp/javascript/automatic-translation/components/NoteAutomaticTranslation.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<template> | ||
<v-chip | ||
v-if="autoTranslatedContent" | ||
small | ||
close | ||
:outlined="!isAutoTranslationSelected" | ||
color="primary" | ||
class="my-auto mx-1" | ||
@click="setAutoTranslationSelected" | ||
@click:close="resetAutoTranslation"> | ||
{{ $t('notes.automatic.translation.label') }} | ||
</v-chip> | ||
<v-btn | ||
v-else | ||
text | ||
color="primary" | ||
class="px-1 py-0 text-decoration-underline" | ||
@click="autoTranslate"> | ||
{{ $t('UIActivity.label.translate') }} | ||
</v-btn> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
isAutoTranslating: false, | ||
isResetAutoTranslating: false, | ||
autoTranslation: { value: 'autoTranslation', text: this.$t('notes.automatic.translation.label') }, | ||
previouslySelectedVersion: null, | ||
autoTranslatedContent: null, | ||
autoTranslatedTitle: null | ||
}; | ||
}, | ||
props: { | ||
note: { | ||
type: Object, | ||
default: null, | ||
}, | ||
selectedTranslation: { | ||
type: Object, | ||
default: null, | ||
}, | ||
}, | ||
watch: { | ||
isAutoTranslating() { | ||
this.toggleTopBarLoading(this.isAutoTranslating); | ||
}, | ||
isResetAutoTranslating() { | ||
this.toggleTopBarLoading(this.isResetAutoTranslating); | ||
} | ||
}, | ||
computed: { | ||
isAutoTranslationSelected() { | ||
return this.selectedTranslation?.value === this.autoTranslation?.value; | ||
} | ||
}, | ||
methods: { | ||
autoTranslate() { | ||
this.isAutoTranslating = true; | ||
this.fetchAutoTranslation(this.note.title).then(translated => { | ||
this.handleTranslatedTitle(translated.translation); | ||
this.fetchAutoTranslation(this.note.content).then(translated => { | ||
this.handleTranslatedContent(translated.translation); | ||
}); | ||
}); | ||
}, | ||
setAutoTranslationSelected() { | ||
if (!this.isAutoTranslationSelected && this.autoTranslatedTitle && this.autoTranslatedContent) { | ||
this.handleTranslatedTitle(this.autoTranslatedTitle); | ||
this.handleTranslatedContent(this.autoTranslatedContent); | ||
this.updateSelectedTranslation(this.autoTranslation); | ||
} | ||
}, | ||
updateNoteContent(content) { | ||
this.$root.$emit('update-note-content', content); | ||
}, | ||
updateNoteTitle(title) { | ||
this.$root.$emit('update-note-title', title); | ||
}, | ||
updateSelectedTranslation(translation) { | ||
this.$root.$emit('update-selected-translation', translation); | ||
}, | ||
resetAutoTranslation() { | ||
this.isResetAutoTranslating = true; | ||
this.autoTranslatedContent = this.autoTranslatedTitle = null; | ||
this.updateNoteTitle(this.note.title); | ||
this.updateNoteContent(this.note.content); | ||
this.updateSelectedTranslation(this.previouslySelectedVersion); | ||
this.isResetAutoTranslating = false; | ||
}, | ||
handleTranslatedTitle(translatedText) { | ||
this.autoTranslatedTitle = translatedText; | ||
this.updateNoteTitle(translatedText); | ||
this.checkAutoTranslatedStatus(); | ||
}, | ||
handleTranslatedContent(translatedText) { | ||
this.autoTranslatedContent = translatedText.replace('<p></p>', '<p> </p>'); | ||
this.updateNoteContent(this.autoTranslatedContent); | ||
this.previouslySelectedVersion = this.selectedTranslation; | ||
this.updateSelectedTranslation(this.autoTranslation); | ||
this.checkAutoTranslatedStatus(); | ||
}, | ||
checkAutoTranslatedStatus() { | ||
if (this.autoTranslatedTitle && this.autoTranslatedContent) { | ||
this.isAutoTranslating = false; | ||
} | ||
}, | ||
toggleTopBarLoading(loading) { | ||
if (loading) { | ||
document.dispatchEvent(new CustomEvent('displayTopBarLoading')); | ||
} else { | ||
document.dispatchEvent(new CustomEvent('hideTopBarLoading')); | ||
} | ||
}, | ||
fetchAutoTranslation(content) { | ||
const data = `message=${ encodeURIComponent(content) }&locale=${ eXo.env.portal.language}`; | ||
return fetch(`${eXo.env.portal.context}/${eXo.env.portal.rest}/automatic-translation/translate`, { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
method: 'POST', | ||
body: data | ||
}).then(resp => { | ||
if (resp?.ok) { | ||
return resp.json(); | ||
} else { | ||
throw new Error('Unable to get automatic translation result'); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters