Skip to content

Commit

Permalink
feat: Add automatic-translation extension - EXO-65429 (#46)
Browse files Browse the repository at this point in the history
This PR adds a new automatic extension point
  • Loading branch information
hakermi authored Oct 13, 2023
1 parent faae69d commit 780c70e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@

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;
import org.mockito.Mock;
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`, {
Expand Down Expand Up @@ -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',
Expand All @@ -112,6 +123,8 @@
vueComponent: Vue.options.components['activity-comment-translated-body'],
rank: 1,
});

document.dispatchEvent(new CustomEvent('automatic-translation-extensions-updated'));
}

return {
Expand Down

0 comments on commit 780c70e

Please sign in to comment.