-
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 DeepL trasnlate connector
- Loading branch information
Showing
4 changed files
with
171 additions
and
0 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
81 changes: 81 additions & 0 deletions
81
...n/java/org/exoplatform/automatic/translation/impl/connectors/DeepLTranslateConnector.java
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,81 @@ | ||
/* | ||
* Copyright (C) 2023 eXo Platform SAS. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.exoplatform.automatic.translation.impl.connectors; | ||
|
||
import com.deepl.api.*; | ||
import org.exoplatform.automatic.translation.api.AutomaticTranslationComponentPlugin; | ||
import org.exoplatform.commons.api.settings.SettingService; | ||
import org.exoplatform.services.log.ExoLogger; | ||
import org.exoplatform.services.log.Log; | ||
import java.util.Locale; | ||
|
||
public class DeepLTranslateConnector extends AutomaticTranslationComponentPlugin { | ||
|
||
private static final Log LOG = ExoLogger.getLogger(DeepLTranslateConnector.class); | ||
|
||
private static final String DEEPL_TRANSLATE_SERVICE = "deepl-translate"; | ||
|
||
private Translator translator; | ||
|
||
private final TextTranslationOptions textTranslationOptions; | ||
|
||
public DeepLTranslateConnector(SettingService settingService) { | ||
super(settingService); | ||
textTranslationOptions = new TextTranslationOptions(); | ||
textTranslationOptions.setTagHandling("html"); | ||
textTranslationOptions.setPreserveFormatting(true); | ||
textTranslationOptions.setSentenceSplittingMode(SentenceSplittingMode.All); | ||
} | ||
|
||
@Override | ||
public String translate(String message, Locale targetLocale) { | ||
long startTime = System.currentTimeMillis(); | ||
try { | ||
return getDeeplTranslator().translateText(message, null, getLocaleLanguage(targetLocale), textTranslationOptions).getText(); | ||
} catch (DeepLException e) { | ||
LOG.error("remote_service={} operation={} parameters=\"message length:{},targetLocale:{}\" status=ko " | ||
+ "duration_ms={} error_msg=\"{}\"", | ||
DEEPL_TRANSLATE_SERVICE, | ||
"translate", | ||
message.length(), | ||
targetLocale.getLanguage(), | ||
System.currentTimeMillis() - startTime, | ||
e.getMessage()); | ||
} catch (InterruptedException e) { | ||
LOG.error("DeepL API translation thread has been interrupted", e); | ||
Thread.currentThread().interrupt(); | ||
} | ||
return null; | ||
} | ||
|
||
private String getLocaleLanguage(Locale locale) { | ||
if (locale.getLanguage().equalsIgnoreCase("en")) { | ||
return "en-US"; | ||
} else if (locale.getLanguage().equalsIgnoreCase("pt")) { | ||
return "pt-PT"; | ||
} else { | ||
return locale.getLanguage(); | ||
} | ||
} | ||
|
||
private Translator getDeeplTranslator() { | ||
if (translator == null) { | ||
translator = new Translator(getApiKey()); | ||
} | ||
return translator; | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
...va/org/exoplatform/automatic/translation/impl/connectors/DeepLTranslateConnectorTest.java
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,79 @@ | ||
/* | ||
* Copyright (C) 2023 eXo Platform SAS. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.exoplatform.automatic.translation.impl.connectors; | ||
|
||
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) | ||
public class DeepLTranslateConnectorTest { | ||
|
||
@Mock | ||
private SettingService settingService; | ||
|
||
@Mock | ||
private Translator translator; | ||
|
||
private DeepLTranslateConnector deepLTranslateConnector; | ||
|
||
@Before | ||
public void setUp() { | ||
deepLTranslateConnector = new DeepLTranslateConnector(settingService); | ||
} | ||
|
||
@Test | ||
public void translate() throws NoSuchFieldException, IllegalAccessException, DeepLException, InterruptedException { | ||
// Need real auth-key to have a real result | ||
TextTranslationOptions textTranslationOptions = new TextTranslationOptions(); | ||
textTranslationOptions.setTagHandling("html"); | ||
textTranslationOptions.setPreserveFormatting(true); | ||
textTranslationOptions.setSentenceSplittingMode(SentenceSplittingMode.All); | ||
TextResult textResult = new TextResult("<h1>Hello</h1>", "fr"); | ||
Field textTranslationOptionsField = deepLTranslateConnector.getClass().getDeclaredField("textTranslationOptions"); | ||
textTranslationOptionsField.setAccessible(true); | ||
textTranslationOptionsField.set(deepLTranslateConnector, textTranslationOptions); | ||
when(translator.translateText("<h1>Bonjour</h1>", null, "en-US", textTranslationOptions)).thenReturn(textResult); | ||
Field field = deepLTranslateConnector.getClass().getDeclaredField("translator"); | ||
field.setAccessible(true); | ||
field.set(deepLTranslateConnector, translator); | ||
String text = deepLTranslateConnector.translate("<h1>Bonjour</h1>", new Locale("en")); | ||
assertNotNull(text); | ||
when(translator.translateText("<h1>Bonjour</h1>", | ||
null, | ||
"en-US", | ||
textTranslationOptions)).thenThrow(new DeepLException("error")); | ||
text = deepLTranslateConnector.translate("<h1>Bonjour</h1>", new Locale("en")); | ||
assertNull(text); | ||
|
||
} | ||
} |