From bac1087914b5af7fa6cdfbaaf00201760c230b6c Mon Sep 17 00:00:00 2001 From: hakermi Date: Thu, 12 Oct 2023 16:52:39 +0100 Subject: [PATCH] feat: Add automatic-translation extension - EXO-65429 This PR adds a new automatic extension point --- .../connectors/DeepLTranslateConnectorTest.java | 4 ---- .../automatic-translation/extensions.js | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/services/impl/src/test/java/org/exoplatform/automatic/translation/impl/connectors/DeepLTranslateConnectorTest.java b/services/impl/src/test/java/org/exoplatform/automatic/translation/impl/connectors/DeepLTranslateConnectorTest.java index 63bea61e..51cf3e72 100644 --- a/services/impl/src/test/java/org/exoplatform/automatic/translation/impl/connectors/DeepLTranslateConnectorTest.java +++ b/services/impl/src/test/java/org/exoplatform/automatic/translation/impl/connectors/DeepLTranslateConnectorTest.java @@ -18,7 +18,6 @@ import com.deepl.api.*; import org.exoplatform.commons.api.settings.SettingService; -import org.exoplatform.commons.api.settings.SettingValue; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,13 +25,10 @@ import org.mockito.junit.MockitoJUnitRunner; import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.Locale; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) diff --git a/webapps/src/main/webapp/javascript/automatic-translation/extensions.js b/webapps/src/main/webapp/javascript/automatic-translation/extensions.js index b42b76fd..c5ae9f85 100644 --- a/webapps/src/main/webapp/javascript/automatic-translation/extensions.js +++ b/webapps/src/main/webapp/javascript/automatic-translation/extensions.js @@ -23,10 +23,10 @@ function fetchTranslation(content, event) { let data = 'message='+encodeURIComponent(content)+'&locale='+eXo.env.portal.language; - if (event.type) { + if (event?.type) { data += '&contentType='+event.type; } - if (event.spaceId) { + if (event?.spaceId) { data += '&spaceId='+event.spaceId; } return fetch(`${eXo.env.portal.context}/${eXo.env.portal.rest}/automatic-translation/translate`, { @@ -100,6 +100,17 @@ }, }); + extensionRegistry.registerExtension('notes', 'translation-menu-extension', { + id: 'auto-translate', + rank: 1000, + isEnabled: () => true, + labelKey: 'UIActivity.label.translate', + translate: (message, callback) => { + fetchTranslation(message).then(translated => { + callback(translated.translation); + }); + }, + }); extensionRegistry.registerComponent('ActivityContent', 'activity-content-extensions', { id: 'translatedBody', @@ -112,6 +123,8 @@ vueComponent: Vue.options.components['activity-comment-translated-body'], rank: 1, }); + + document.dispatchEvent(new CustomEvent('automatic-translation-extensions-updated')); } return {