Skip to content

Commit

Permalink
feat: Arabic Support
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Feb 28, 2024
1 parent c930283 commit af58d1f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import com.sitepark.translate.Format;
import com.sitepark.translate.Glossary;
import com.sitepark.translate.GlossaryEntry;
import com.sitepark.translate.GlossaryManager;
import com.sitepark.translate.Language;
import com.sitepark.translate.SupportedLanguages;
import com.sitepark.translate.SupportedProvider;
import com.sitepark.translate.TranslationConfiguration;
Expand All @@ -28,206 +19,199 @@
import com.sitepark.translate.TranslationResult;
import com.sitepark.translate.translator.TranslatableText;
import com.sitepark.translate.translator.TranslatableTextListTranslator;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@SuppressWarnings("PMD")
@Tag("IntegrationTest")
@Disabled
class DeeplTranslationProviderIntegrationTest {

private TranslationProvider createProvider() throws IOException, URISyntaxException {
return new DeeplTranslationProvider(this.createConfiguration());
}

private TranslationConfiguration createConfiguration() throws IOException, URISyntaxException {

DeeplTestConnection con = DeeplTestConnection.get();

DeeplTranslationProviderConfiguration.Builder builder =
DeeplTranslationProviderConfiguration.builder()
.url(con.getUrl())
.authKey(con.getAuthKey());

return TranslationConfiguration.builder()
.translationProviderConfiguration(builder.build())
.encodePlaceholder(true)
.build();
}
private TranslationProvider createProvider() throws IOException, URISyntaxException {
return new DeeplTranslationProvider(this.createConfiguration());
}

@Test
void testSupportedLanguages() throws URISyntaxException, IOException, InterruptedException {
private TranslationConfiguration createConfiguration() throws IOException, URISyntaxException {

TranslationProvider translator = this.createProvider();
DeeplTestConnection con = DeeplTestConnection.get();

SupportedLanguages supportedLanguages = translator.getSupportedLanguages();
DeeplTranslationProviderConfiguration.Builder builder =
DeeplTranslationProviderConfiguration.builder().url(con.getUrl()).authKey(con.getAuthKey());

assertTrue(supportedLanguages.getAll().size() > 0, "supportedLanguages should not be empty");
}
return TranslationConfiguration.builder()
.translationProviderConfiguration(builder.build())
.encodePlaceholder(true)
.build();
}

@Test
void testTranslate() throws URISyntaxException, IOException, InterruptedException {
@Test
void testSupportedLanguages() throws URISyntaxException, IOException, InterruptedException {
TranslationProvider translator = this.createProvider();
SupportedLanguages supportedLanguages = translator.getSupportedLanguages();
assertTrue(supportedLanguages.getAll().size() > 0, "supportedLanguages should not be empty");
for (Language lang : supportedLanguages.getAll()) {
System.out.println(lang.getCode() + " - " + lang.getName());
}
Locale.availableLocales()
.filter(l -> l.getLanguage().equals("ar"))
.forEach(System.out::println);
}

TranslationProvider translator = this.createProvider();
@Test
void testTranslate() throws URISyntaxException, IOException, InterruptedException {

TranslationLanguage translationLanguage = TranslationLanguage.builder()
.source("de")
.target("en")
.build();
TranslationProvider translator = this.createProvider();

TranslationParameter parameter = TranslationParameter.builder()
.format(Format.TEXT)
.language(translationLanguage)
.providerType(SupportedProvider.DEEPL)
.build();
TranslationLanguage translationLanguage =
TranslationLanguage.builder().source("de").target("en").build();

TranslationRequest req = TranslationRequest.builder()
.parameter(parameter)
.sourceText("Hallo", "Welt")
.build();
TranslationParameter parameter =
TranslationParameter.builder()
.format(Format.TEXT)
.language(translationLanguage)
.providerType(SupportedProvider.DEEPL)
.build();

TranslationResult result = translator.translate(req);
TranslationRequest req =
TranslationRequest.builder().parameter(parameter).sourceText("Hallo", "Welt").build();

assertArrayEquals(new String[] {"Hello", "World"}, result.getText(), "Unexpected translation");
}
TranslationResult result = translator.translate(req);

@Test
void testTranslateHtml() throws URISyntaxException, IOException, InterruptedException {
assertArrayEquals(new String[] {"Hello", "World"}, result.getText(), "Unexpected translation");
}

TranslationConfiguration translatorConfiguration = this.createConfiguration();
@Test
void testTranslateHtml() throws URISyntaxException, IOException, InterruptedException {

List<TranslatableText> translatableTextList = new ArrayList<>();
translatableTextList.add(new TranslatableText("Hallo Welt & <test> \"Universum\""));
translatableTextList.add(new TranslatableText("Hallo Welt &amp; &lt;test&gt; \"Universum\"", Format.HTML));
TranslationConfiguration translatorConfiguration = this.createConfiguration();

TranslationLanguage language = TranslationLanguage.builder()
.source("de")
.target("en")
.build();
List<TranslatableText> translatableTextList = new ArrayList<>();
translatableTextList.add(new TranslatableText("Hallo Welt & <test> \"Universum\""));
translatableTextList.add(
new TranslatableText("Hallo Welt &amp; &lt;test&gt; \"Universum\"", Format.HTML));

TranslatableTextListTranslator translator = TranslatableTextListTranslator.builder()
.translatorConfiguration(translatorConfiguration)
.build();
TranslationLanguage language = TranslationLanguage.builder().source("de").target("en").build();

TranslationParameter parameter = TranslationParameter.builder()
.language(language)
.providerType(SupportedProvider.DEEPL)
.build();
TranslatableTextListTranslator translator =
TranslatableTextListTranslator.builder()
.translatorConfiguration(translatorConfiguration)
.build();

translator.translate(parameter, translatableTextList);
TranslationParameter parameter =
TranslationParameter.builder()
.language(language)
.providerType(SupportedProvider.DEEPL)
.build();

System.out.println(translatableTextList.get(0).getTargetText());
System.out.println(translatableTextList.get(1).getTargetText());
translator.translate(parameter, translatableTextList);

//assertArrayEquals(new String[] {"Hello", "World"}, res, "Unexpected translation");
}
System.out.println(translatableTextList.get(0).getTargetText());
System.out.println(translatableTextList.get(1).getTargetText());

@Test
void testTranslateHtmlBroken() throws URISyntaxException, IOException, InterruptedException {
// assertArrayEquals(new String[] {"Hello", "World"}, res, "Unexpected translation");
}

TranslationConfiguration translatorConfiguration = this.createConfiguration();
@Test
void testTranslateHtmlBroken() throws URISyntaxException, IOException, InterruptedException {

List<TranslatableText> translatableTextList = new ArrayList<>();
translatableTextList.add(new TranslatableText(
"<div class=\"SP-LinkList SP-LinkList--compact SP-Util__sectionMarginSmall\">\n"
+ "<ul class=\"SP-LinkList__list\">\n"
+ " <li class=\"SP-LinkList__item\">\n"
+ " <a data-variant=\"in-linklist\" href=\"https://www.kunstcaching.de/\" rel=\"noopener\" target=\"_blank\" data-entity-ref=\"1\">www.kunstcaching.de</a>\n"
+ " </li>\n"
+ " <li class=\"SP-LinkList__item\">\n"
+ " <a data-variant=\"in-linklist\" href=\"https://www.pablo-zibes.de\" rel=\"noopener\" target=\"_blank\" data-entity-ref=\"2\">Website von Pablo Zibes</a>\n"
+ " </li>\n"
+ "</ul>\n"
+ "</div>"));
TranslationConfiguration translatorConfiguration = this.createConfiguration();

TranslationLanguage language = TranslationLanguage.builder()
.source("de")
.target("en")
.build();
List<TranslatableText> translatableTextList = new ArrayList<>();
translatableTextList.add(
new TranslatableText(
"<div class=\"SP-LinkList SP-LinkList--compact SP-Util__sectionMarginSmall\">\n"
+ "<ul class=\"SP-LinkList__list\">\n"
+ " <li class=\"SP-LinkList__item\">\n"
+ " <a data-variant=\"in-linklist\" href=\"https://www.kunstcaching.de/\" rel=\"noopener\" target=\"_blank\" data-entity-ref=\"1\">www.kunstcaching.de</a>\n"
+ " </li>\n"
+ " <li class=\"SP-LinkList__item\">\n"
+ " <a data-variant=\"in-linklist\" href=\"https://www.pablo-zibes.de\" rel=\"noopener\" target=\"_blank\" data-entity-ref=\"2\">Website von Pablo Zibes</a>\n"
+ " </li>\n"
+ "</ul>\n"
+ "</div>"));

TranslatableTextListTranslator translator = TranslatableTextListTranslator.builder()
.translatorConfiguration(translatorConfiguration)
.build();
TranslationLanguage language = TranslationLanguage.builder().source("de").target("en").build();

TranslationParameter parameter = TranslationParameter.builder()
.language(language)
.providerType(SupportedProvider.DEEPL)
.build();
TranslatableTextListTranslator translator =
TranslatableTextListTranslator.builder()
.translatorConfiguration(translatorConfiguration)
.build();

translator.translate(parameter, translatableTextList);
TranslationParameter parameter =
TranslationParameter.builder()
.language(language)
.providerType(SupportedProvider.DEEPL)
.build();

System.out.println(translatableTextList.get(0).getTargetText());
translator.translate(parameter, translatableTextList);

//assertArrayEquals(new String[] {"Hello", "World"}, res, "Unexpected translation");
}
System.out.println(translatableTextList.get(0).getTargetText());

@Test
void testTranslateHtmlBrokenAmp() throws URISyntaxException, IOException, InterruptedException {
// assertArrayEquals(new String[] {"Hello", "World"}, res, "Unexpected translation");
}

TranslationConfiguration translatorConfiguration = this.createConfiguration();
@Test
void testTranslateHtmlBrokenAmp() throws URISyntaxException, IOException, InterruptedException {

List<TranslatableText> translatableTextList = new ArrayList<>();
translatableTextList.add(new TranslatableText("Einrichtungen & Beteiligungen"));
TranslationConfiguration translatorConfiguration = this.createConfiguration();

TranslationLanguage language = TranslationLanguage.builder()
.source("de")
.target("en")
.build();
List<TranslatableText> translatableTextList = new ArrayList<>();
translatableTextList.add(new TranslatableText("Einrichtungen & Beteiligungen"));

TranslatableTextListTranslator translator = TranslatableTextListTranslator.builder()
.translatorConfiguration(translatorConfiguration)
.build();
TranslationLanguage language = TranslationLanguage.builder().source("de").target("en").build();

TranslationParameter parameter = TranslationParameter.builder()
.language(language)
.providerType(SupportedProvider.DEEPL)
.build();
TranslatableTextListTranslator translator =
TranslatableTextListTranslator.builder()
.translatorConfiguration(translatorConfiguration)
.build();

translator.translate(parameter, translatableTextList);
TranslationParameter parameter =
TranslationParameter.builder()
.language(language)
.providerType(SupportedProvider.DEEPL)
.build();

System.out.println(translatableTextList.get(0).getTargetText());
translator.translate(parameter, translatableTextList);

//assertArrayEquals(new String[] {"Hello", "World"}, res, "Unexpected translation");
}
System.out.println(translatableTextList.get(0).getTargetText());

// assertArrayEquals(new String[] {"Hello", "World"}, res, "Unexpected translation");
}

@Test
void testGlossarManagement() throws URISyntaxException, IOException, InterruptedException {
@Test
void testGlossarManagement() throws URISyntaxException, IOException, InterruptedException {

TranslationConfiguration translatorConfiguration = this.createConfiguration();
TranslationProviderFactory factory = new TranslationProviderFactory(translatorConfiguration);
TranslationConfiguration translatorConfiguration = this.createConfiguration();
TranslationProviderFactory factory = new TranslationProviderFactory(translatorConfiguration);

TranslationProvider provider = factory.create(SupportedProvider.DEEPL);
TranslationProvider provider = factory.create(SupportedProvider.DEEPL);

GlossaryManager glossarManager = new GlossaryManager(provider);
GlossaryManager glossarManager = new GlossaryManager(provider);

Optional<String> id = glossarManager.getGlossaryId("test:de/en");
System.out.println("exists glossary: " + id.orElse("not found"));
Optional<String> id = glossarManager.getGlossaryId("test:de/en");
System.out.println("exists glossary: " + id.orElse("not found"));

Glossary glossary = Glossary.builder()
.language(TranslationLanguage.builder()
.source("de")
.target("en")
.build()
)
.entry(GlossaryEntry.builder()
.source("Hallo")
.target("Hey")
.build()
)
.entry(GlossaryEntry.builder()
.source("Foo")
.target("Bar")
.build()
)
.build();
Glossary glossary =
Glossary.builder()
.language(TranslationLanguage.builder().source("de").target("en").build())
.entry(GlossaryEntry.builder().source("Hallo").target("Hey").build())
.entry(GlossaryEntry.builder().source("Foo").target("Bar").build())
.build();

String newId = glossarManager.recreate(glossary);
String newId = glossarManager.recreate(glossary);

System.out.println("new glossary: " + newId);
System.out.println("new glossary: " + newId);

Glossary loaded = glossarManager.getGlossary(newId).get();
for (GlossaryEntry entry : loaded.getEntryList()) {
System.out.println(entry.getSource() + ", " + entry.getTarget());
}
}
Glossary loaded = glossarManager.getGlossary(newId).get();
for (GlossaryEntry entry : loaded.getEntryList()) {
System.out.println(entry.getSource() + ", " + entry.getTarget());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public DeeplTranslationProvider(TranslationConfiguration translatorConfiguration
this.translatorConfiguration = translatorConfiguration;
}

@Override
public TranslationResult translate(TranslationRequest req) {

String[] sourceTextToTranslate = req.getSourceText();
Expand Down Expand Up @@ -171,6 +172,7 @@ private int byteCount(String... array) {
return count;
}

@Override
public SupportedLanguages getSupportedLanguages() {

SupportedLanguages.Builder builder = SupportedLanguages.builder();
Expand Down Expand Up @@ -216,7 +218,10 @@ protected List<TransportLanguage> getLanguages(LanguageType type) {
HttpClient client = this.createHttpClient();

try {
return client.send(request, new JsonBodyLanguagesHandler()).body().get();
List<TransportLanguage> list =
new ArrayList<>(client.send(request, new JsonBodyLanguagesHandler()).body().get());
list.add(new TransportLanguage("ar", "Abrabic", false));
return list;
} catch (InterruptedException | IOException e) {
throw new TranslationProviderException(e.getMessage(), e);
}
Expand Down
Loading

0 comments on commit af58d1f

Please sign in to comment.