This was fun while it lasted (why you got to deprecate all the APIs tho?)
This commit is contained in:
parent
6d846b9b8a
commit
796da1a530
@ -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<String, String> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Language> languages;
|
||||
public class TranslatingTransformer<T> implements ITransformer {
|
||||
private Random rnd;
|
||||
private Collection<T> languages;
|
||||
private ITranslationBackend<T> 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 {
|
||||
public TranslatingTransformer(ITranslationBackend<T> translationBackend) {
|
||||
backend = translationBackend;
|
||||
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");
|
||||
}
|
||||
languages = backend.getLanguages();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user