This was fun while it lasted (why you got to deprecate all the APIs tho?)

This commit is contained in:
JFronny 2020-08-07 14:32:44 +02:00
parent 6d846b9b8a
commit 796da1a530
4 changed files with 94 additions and 46 deletions

View File

@ -2,12 +2,14 @@ package io.gitlab.jfronny.translater;
import io.gitlab.jfronny.translater.transformer.CachingTransformer; import io.gitlab.jfronny.translater.transformer.CachingTransformer;
import io.gitlab.jfronny.translater.transformer.TransformingMap; 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.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer; import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer;
import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -50,7 +52,7 @@ public class Translater implements ClientModInitializer {
public static TransformingMap getMap(Map<String, String> base) { public static TransformingMap getMap(Map<String, String> base) {
if (map == null) { if (map == null) {
map = new TransformingMap(base, new CachingTransformer(new YnTransformer())); map = new TransformingMap(base, new CachingTransformer(new TranslatingTransformer<>(new YandexTranslationBackend())));
map.init(); map.init();
} }
return map; return map;
@ -59,4 +61,15 @@ public class Translater implements ClientModInitializer {
public static TransformingMap getMap() { public static TransformingMap getMap() {
return map; 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;
}
} }

View File

@ -1,19 +1,18 @@
package io.gitlab.jfronny.translater.transformer; 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.Translater;
import io.gitlab.jfronny.translater.translation.ITranslationBackend;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.Collection;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
//The actual translation methods //The actual translation methods
public class YnTransformer implements ITransformer { public class TranslatingTransformer<T> implements ITransformer {
private static Random rnd; private Random rnd;
private static YTranslateApiImpl api; private Collection<T> languages;
private static Set<Language> languages; private ITranslationBackend<T> backend;
@Override @Override
public String transform(String str) { public String transform(String str) {
try { try {
@ -57,7 +56,7 @@ public class YnTransformer implements ITransformer {
} }
} }
catch (Exception e) { 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"); Translater.Warn("Please report this bug with the mod containing the lang file");
throw e; throw e;
} }
@ -65,58 +64,36 @@ public class YnTransformer implements ITransformer {
private String Break(String str) { private String Break(String str) {
try { try {
if (invalid(str)) if (Translater.stringInvalid(str))
return str; return str;
Language startLang = StringUtils.isAlpha(Translater.cfg.targetLanguage) && Translater.cfg.targetLanguage.length() == 2 T startLang = StringUtils.isAlpha(Translater.cfg.targetLanguage) && Translater.cfg.targetLanguage.length() == 2
? Language.of(Translater.cfg.targetLanguage) ? backend.parseLang(Translater.cfg.targetLanguage)
: api.detectionApi().detect(str).orElse(Language.EN); : backend.detect(str);
if (invalid(startLang.code())) {
Translater.Warn("Could not detect language for: " + str);
Translater.Warn("Defaulting to EN");
startLang = Language.EN;
}
String currentState = str; String currentState = str;
Language currentLang = startLang; T currentLang = startLang;
for (int i = 0; i < Translater.cfg.rounds; i++) { for (int i = 0; i < Translater.cfg.rounds; i++) {
Language newLang = selectRandom(); T newLang = selectRandom();
currentState = api.translationApi().translate(currentState, Direction.of(Translater.cfg.breakFully ? selectRandom() : currentLang, newLang)).text(); currentState = backend.translate(currentState, newLang, Translater.cfg.breakFully ? selectRandom() : currentLang);
currentLang = newLang; currentLang = newLang;
} }
currentState = api.translationApi().translate(currentState, startLang).text(); currentState = backend.translate(currentState, startLang, currentLang);
if (Translater.Cli()) if (Translater.Cli())
Translater.Log("Transformed: \"" + str + "\" to: \"" + currentState + "\""); Translater.Log("Transformed: \"" + str + "\" to: \"" + currentState + "\"");
return currentState; return currentState;
} catch (Exception e) { } catch (Exception e) {
Translater.Warn("Failed to break: " + str + " (" + str.length() + " characters)"); Translater.Warn("Failed to break: \"" + str + "\" (" + str.length() + " characters). Is your API key valid?");
Translater.Warn("Please report this bug with the mod containing the lang file");
throw e; throw e;
} }
} }
private boolean invalid(String str) { private T selectRandom() {
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() {
return languages.stream().skip(rnd.nextInt(languages.size())).findFirst().get(); return languages.stream().skip(rnd.nextInt(languages.size())).findFirst().get();
} }
public YnTransformer() { public TranslatingTransformer(ITranslationBackend<T> translationBackend) {
try { backend = translationBackend;
rnd = new Random(); rnd = new Random();
api = new YTranslateApiImpl(Translater.cfg.key); languages = backend.getLanguages();
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");
}
} }
} }

View File

@ -0,0 +1,10 @@
package io.gitlab.jfronny.translater.translation;
import java.util.Collection;
public interface ITranslationBackend<T> {
String translate(String text, T target, T current);
T detect(String text);
T parseLang(String lang);
Collection<T> getLanguages();
}

View File

@ -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<Language> {
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<Language> 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);
}
}