diff --git a/src/main/java/io/gitlab/jfronny/translater/Translater.java b/src/main/java/io/gitlab/jfronny/translater/Translater.java index f6dabda..0ac6e95 100644 --- a/src/main/java/io/gitlab/jfronny/translater/Translater.java +++ b/src/main/java/io/gitlab/jfronny/translater/Translater.java @@ -2,12 +2,14 @@ package io.gitlab.jfronny.translater; import io.gitlab.jfronny.translater.transformer.CachingTransformer; import io.gitlab.jfronny.translater.transformer.TransformingMap; -import io.gitlab.jfronny.translater.transformer.YnTransformer; +import io.gitlab.jfronny.translater.transformer.TranslatingTransformer; +import io.gitlab.jfronny.translater.translation.YandexTranslationBackend; import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -50,7 +52,7 @@ public class Translater implements ClientModInitializer { public static TransformingMap getMap(Map base) { if (map == null) { - map = new TransformingMap(base, new CachingTransformer(new YnTransformer())); + map = new TransformingMap(base, new CachingTransformer(new TranslatingTransformer<>(new YandexTranslationBackend()))); map.init(); } return map; @@ -59,4 +61,15 @@ public class Translater implements ClientModInitializer { public static TransformingMap getMap() { return map; } + + public static boolean stringInvalid(String str) { + if (str.length() < 2) + return true; + for (char c : str.toCharArray()) { + String tmp = "" + c; + if (StringUtils.isAlphanumeric(tmp)) + return false; + } + return true; + } } diff --git a/src/main/java/io/gitlab/jfronny/translater/transformer/YnTransformer.java b/src/main/java/io/gitlab/jfronny/translater/transformer/TranslatingTransformer.java similarity index 54% rename from src/main/java/io/gitlab/jfronny/translater/transformer/YnTransformer.java rename to src/main/java/io/gitlab/jfronny/translater/transformer/TranslatingTransformer.java index a25ce1c..cc60b38 100644 --- a/src/main/java/io/gitlab/jfronny/translater/transformer/YnTransformer.java +++ b/src/main/java/io/gitlab/jfronny/translater/transformer/TranslatingTransformer.java @@ -1,19 +1,18 @@ package io.gitlab.jfronny.translater.transformer; -import com.github.vbauer.yta.model.Direction; -import com.github.vbauer.yta.model.Language; -import com.github.vbauer.yta.service.YTranslateApiImpl; import io.gitlab.jfronny.translater.Translater; +import io.gitlab.jfronny.translater.translation.ITranslationBackend; import org.apache.commons.lang3.StringUtils; +import java.util.Collection; import java.util.Random; import java.util.Set; //The actual translation methods -public class YnTransformer implements ITransformer { - private static Random rnd; - private static YTranslateApiImpl api; - private static Set languages; +public class TranslatingTransformer implements ITransformer { + private Random rnd; + private Collection languages; + private ITranslationBackend backend; @Override public String transform(String str) { try { @@ -57,7 +56,7 @@ public class YnTransformer implements ITransformer { } } catch (Exception e) { - Translater.Warn("Failed to transform: " + str + " (" + str.length() + " characters)"); + Translater.Warn("Failed to transform: \"" + str + "\" (" + str.length() + " characters)"); Translater.Warn("Please report this bug with the mod containing the lang file"); throw e; } @@ -65,58 +64,36 @@ public class YnTransformer implements ITransformer { private String Break(String str) { try { - if (invalid(str)) + if (Translater.stringInvalid(str)) return str; - Language startLang = StringUtils.isAlpha(Translater.cfg.targetLanguage) && Translater.cfg.targetLanguage.length() == 2 - ? Language.of(Translater.cfg.targetLanguage) - : api.detectionApi().detect(str).orElse(Language.EN); - if (invalid(startLang.code())) { - Translater.Warn("Could not detect language for: " + str); - Translater.Warn("Defaulting to EN"); - startLang = Language.EN; - } + T startLang = StringUtils.isAlpha(Translater.cfg.targetLanguage) && Translater.cfg.targetLanguage.length() == 2 + ? backend.parseLang(Translater.cfg.targetLanguage) + : backend.detect(str); String currentState = str; - Language currentLang = startLang; + T currentLang = startLang; for (int i = 0; i < Translater.cfg.rounds; i++) { - Language newLang = selectRandom(); - currentState = api.translationApi().translate(currentState, Direction.of(Translater.cfg.breakFully ? selectRandom() : currentLang, newLang)).text(); + T newLang = selectRandom(); + currentState = backend.translate(currentState, newLang, Translater.cfg.breakFully ? selectRandom() : currentLang); currentLang = newLang; } - currentState = api.translationApi().translate(currentState, startLang).text(); + currentState = backend.translate(currentState, startLang, currentLang); if (Translater.Cli()) Translater.Log("Transformed: \"" + str + "\" to: \"" + currentState + "\""); return currentState; } catch (Exception e) { - Translater.Warn("Failed to break: " + str + " (" + str.length() + " characters)"); - Translater.Warn("Please report this bug with the mod containing the lang file"); + Translater.Warn("Failed to break: \"" + str + "\" (" + str.length() + " characters). Is your API key valid?"); throw e; } } - private boolean invalid(String str) { - if (str.length() < 2) - return true; - for (char c : str.toCharArray()) { - String tmp = "" + c; - if (StringUtils.isAlphanumeric(tmp)) - return false; - } - return true; - } - - private Language selectRandom() { + private T selectRandom() { return languages.stream().skip(rnd.nextInt(languages.size())).findFirst().get(); } - public YnTransformer() { - try { - rnd = new Random(); - api = new YTranslateApiImpl(Translater.cfg.key); - languages = api.languageApi().all(Language.EN).languages(); - } catch (Exception e) { - Translater.Warn("Failed to initialize transformer. Is your API key valid?"); - Translater.Warn("Minecraft will continue but issues should be expected"); - } + public TranslatingTransformer(ITranslationBackend translationBackend) { + backend = translationBackend; + rnd = new Random(); + languages = backend.getLanguages(); } } diff --git a/src/main/java/io/gitlab/jfronny/translater/translation/ITranslationBackend.java b/src/main/java/io/gitlab/jfronny/translater/translation/ITranslationBackend.java new file mode 100644 index 0000000..6462909 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/translater/translation/ITranslationBackend.java @@ -0,0 +1,10 @@ +package io.gitlab.jfronny.translater.translation; + +import java.util.Collection; + +public interface ITranslationBackend { + String translate(String text, T target, T current); + T detect(String text); + T parseLang(String lang); + Collection getLanguages(); +} diff --git a/src/main/java/io/gitlab/jfronny/translater/translation/YandexTranslationBackend.java b/src/main/java/io/gitlab/jfronny/translater/translation/YandexTranslationBackend.java new file mode 100644 index 0000000..a63e216 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/translater/translation/YandexTranslationBackend.java @@ -0,0 +1,48 @@ +package io.gitlab.jfronny.translater.translation; + +import com.github.vbauer.yta.model.Direction; +import com.github.vbauer.yta.model.Language; +import com.github.vbauer.yta.service.YTranslateApi; +import com.github.vbauer.yta.service.YTranslateApiImpl; +import io.gitlab.jfronny.translater.Translater; + +import java.util.Collection; + +public class YandexTranslationBackend implements ITranslationBackend { + private final YTranslateApi api; + @Override + public String translate(String text, Language target, Language current) { + return api.translationApi().translate(text, Direction.of(current, target)).text(); + } + + @Override + public Language detect(String text) { + Language result = api.detectionApi().detect(text).orElse(Language.EN); + if (Translater.stringInvalid(result.code())) { + Translater.Warn("Could not detect language for: " + text); + Translater.Warn("Defaulting to EN"); + return Language.EN; + } + return result; + } + + @Override + public Language parseLang(String lang) { + return Language.of(lang); + } + + @Override + public Collection getLanguages() { + try { + return api.languageApi().all(Language.EN).languages(); + } catch (Exception e) { + Translater.Warn("Failed to get languages, Is your API key valid?"); + e.printStackTrace(); + return Language.ALL.values(); + } + } + + public YandexTranslationBackend() { + api = new YTranslateApiImpl(Translater.cfg.key); + } +}