[translate] Don't translate white space
This commit is contained in:
parent
e588fa379b
commit
349d6587d6
@ -0,0 +1,27 @@
|
|||||||
|
package io.gitlab.jfronny.libjf.translate.impl;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.libjf.translate.api.*;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public abstract class AbstractTranslateService<T extends Language> implements TranslateService<T> {
|
||||||
|
private static final Pattern WHITE_SPACE = Pattern.compile("\\s*");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String translate(String textToTranslate, T translateFrom, T translateTo) throws TranslateException {
|
||||||
|
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null");
|
||||||
|
if (translateFrom == null) translateFrom = getAutoDetectLang();
|
||||||
|
if (translateTo == null) throw new TranslateException("translateTo must not be null");
|
||||||
|
if (WHITE_SPACE.matcher(textToTranslate).matches()) return textToTranslate;
|
||||||
|
try {
|
||||||
|
return performTranslate(textToTranslate, translateFrom, translateTo);
|
||||||
|
} catch (TranslateException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new TranslateException("Could not translate text", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract T getAutoDetectLang();
|
||||||
|
protected abstract String performTranslate(String textToTranslate, T translateFrom, T translateTo) throws Exception;
|
||||||
|
}
|
@ -2,7 +2,7 @@ package io.gitlab.jfronny.libjf.translate.impl.google;
|
|||||||
|
|
||||||
import io.gitlab.jfronny.commons.HttpUtils;
|
import io.gitlab.jfronny.commons.HttpUtils;
|
||||||
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
||||||
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService;
|
||||||
import org.apache.commons.lang3.StringEscapeUtils;
|
import org.apache.commons.lang3.StringEscapeUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -15,7 +15,7 @@ import java.util.*;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class GoogleTranslateService implements TranslateService<GoogleTranslateLanguage> {
|
public class GoogleTranslateService extends AbstractTranslateService<GoogleTranslateLanguage> {
|
||||||
public static final String NAME = "Google";
|
public static final String NAME = "Google";
|
||||||
private static GoogleTranslateService INSTANCE;
|
private static GoogleTranslateService INSTANCE;
|
||||||
private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)</div>", Pattern.MULTILINE);
|
private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)</div>", Pattern.MULTILINE);
|
||||||
@ -43,10 +43,12 @@ public class GoogleTranslateService implements TranslateService<GoogleTranslateL
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String translate(String textToTranslate, GoogleTranslateLanguage translateFrom, GoogleTranslateLanguage translateTo) throws TranslateException {
|
protected GoogleTranslateLanguage getAutoDetectLang() {
|
||||||
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null");
|
return GoogleTranslateLanguage.AUTO_DETECT;
|
||||||
if (translateFrom == null) translateFrom = GoogleTranslateLanguage.AUTO_DETECT;
|
}
|
||||||
if (translateTo == null) throw new TranslateException("translateTo must not be null");
|
|
||||||
|
@Override
|
||||||
|
protected String performTranslate(String textToTranslate, GoogleTranslateLanguage translateFrom, GoogleTranslateLanguage translateTo) throws Exception {
|
||||||
String pageSource = "";
|
String pageSource = "";
|
||||||
try {
|
try {
|
||||||
pageSource = getPageSource(textToTranslate, translateFrom.getIdentifier(), translateTo.getIdentifier());
|
pageSource = getPageSource(textToTranslate, translateFrom.getIdentifier(), translateTo.getIdentifier());
|
||||||
@ -91,7 +93,7 @@ public class GoogleTranslateService implements TranslateService<GoogleTranslateL
|
|||||||
return NAME;
|
return NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getPageSource(String textToTranslate, String translateFrom, String translateTo) throws Exception {
|
private static String getPageSource(String textToTranslate, String translateFrom, String translateTo) throws URISyntaxException, IOException {
|
||||||
if (textToTranslate == null)
|
if (textToTranslate == null)
|
||||||
return null;
|
return null;
|
||||||
String pageUrl = String.format("https://translate.google.com/m?hl=en&sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s",
|
String pageUrl = String.format("https://translate.google.com/m?hl=en&sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s",
|
||||||
|
@ -3,7 +3,7 @@ package io.gitlab.jfronny.libjf.translate.impl.libretranslate;
|
|||||||
import io.gitlab.jfronny.commons.HttpUtils;
|
import io.gitlab.jfronny.commons.HttpUtils;
|
||||||
import io.gitlab.jfronny.gson.reflect.TypeToken;
|
import io.gitlab.jfronny.gson.reflect.TypeToken;
|
||||||
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
||||||
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService;
|
||||||
import io.gitlab.jfronny.libjf.translate.impl.libretranslate.model.*;
|
import io.gitlab.jfronny.libjf.translate.impl.libretranslate.model.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -11,7 +11,7 @@ import java.lang.reflect.Type;
|
|||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class LibreTranslateService implements TranslateService<LibreTranslateLanguage> {
|
public class LibreTranslateService extends AbstractTranslateService<LibreTranslateLanguage> {
|
||||||
public static final String NAME = "LibreTranslate";
|
public static final String NAME = "LibreTranslate";
|
||||||
private static final Type languageListType = new TypeToken<List<LibreTranslateLanguage.ApiResult>>(){}.getType();
|
private static final Type languageListType = new TypeToken<List<LibreTranslateLanguage.ApiResult>>(){}.getType();
|
||||||
private static final Type translateDetectResultListType = new TypeToken<List<LibreTranslateDetectResult>>(){}.getType();
|
private static final Type translateDetectResultListType = new TypeToken<List<LibreTranslateDetectResult>>(){}.getType();
|
||||||
@ -56,20 +56,17 @@ public class LibreTranslateService implements TranslateService<LibreTranslateLan
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String translate(String textToTranslate, LibreTranslateLanguage translateFrom, LibreTranslateLanguage translateTo) throws TranslateException {
|
protected LibreTranslateLanguage getAutoDetectLang() {
|
||||||
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null");
|
return autoDetect;
|
||||||
if (translateFrom == null) translateFrom = autoDetect;
|
}
|
||||||
if (translateTo == null) throw new TranslateException("translateTo must not be null");
|
|
||||||
LibreTranslateResult result = null;
|
@Override
|
||||||
try {
|
protected String performTranslate(String textToTranslate, LibreTranslateLanguage translateFrom, LibreTranslateLanguage translateTo) throws Exception {
|
||||||
result = HttpUtils.post(host + "/translate").bodyForm(Map.of(
|
LibreTranslateResult result = HttpUtils.post(host + "/translate").bodyForm(Map.of(
|
||||||
"q", textToTranslate,
|
"q", textToTranslate,
|
||||||
"source", translateFrom.getIdentifier(),
|
"source", translateFrom.getIdentifier(),
|
||||||
"target", translateTo.getIdentifier()
|
"target", translateTo.getIdentifier()
|
||||||
)).sendSerialized(LibreTranslateResult.class);
|
)).sendSerialized(LibreTranslateResult.class);
|
||||||
} catch (IOException | URISyntaxException e) {
|
|
||||||
throw new TranslateException("Could not translate text", e);
|
|
||||||
}
|
|
||||||
return result.translatedText;
|
return result.translatedText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user