icons + more complete namecache

This commit is contained in:
JFronny 2020-07-10 20:46:39 +02:00
parent 1f07664660
commit 9bb250e9f4
10 changed files with 5000 additions and 1206 deletions

View File

@ -27,6 +27,11 @@ dependencies {
modCompile "com.github.vbauer:yandex-translate-api:1.4.2" modCompile "com.github.vbauer:yandex-translate-api:1.4.2"
modCompile "me.sargunvohra.mcmods:autoconfig1u:3.2.0-unstable"
include "me.sargunvohra.mcmods:autoconfig1u:3.2.0-unstable"
modApi "me.shedaniel.cloth:config-2:4.5.6"
include "me.shedaniel.cloth:config-2:4.5.6"
modCompile("io.github.prospector:modmenu:1.12.2+build.17") modCompile("io.github.prospector:modmenu:1.12.2+build.17")
} }

BIN
icon.xcf Normal file

Binary file not shown.

View File

@ -3,8 +3,6 @@ package io.gitlab.jfronny.translater;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import java.io.*; import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
public abstract class CachingTransformer implements StringTransformer { public abstract class CachingTransformer implements StringTransformer {
@ -14,6 +12,8 @@ public abstract class CachingTransformer implements StringTransformer {
public String transform(String str) { public String transform(String str) {
if (cache == null) { if (cache == null) {
cache = new Properties(); cache = new Properties();
if (!ModInit.cfg.forceRegenerate) {
ModInit.cfg.forceRegenerate = false;
if (cacheFile.exists()) { if (cacheFile.exists()) {
try { try {
FileInputStream inS = new FileInputStream(cacheFile); FileInputStream inS = new FileInputStream(cacheFile);
@ -23,7 +23,19 @@ public abstract class CachingTransformer implements StringTransformer {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
if (!ModInit.cfg.breakFully && ModInit.cfg.rounds == 10) {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inS = classLoader.getResourceAsStream("namecache.ini");
if (inS != null) {
cache.load(inS);
inS.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }
} }
if (!cache.containsKey(str)) { if (!cache.containsKey(str)) {

View File

@ -0,0 +1,17 @@
package io.gitlab.jfronny.translater;
import me.sargunvohra.mcmods.autoconfig1u.ConfigData;
import me.sargunvohra.mcmods.autoconfig1u.annotation.Config;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.Comment;
@Config(name = "TanslaterCF")
public class Cfg implements ConfigData {
@Comment("NOTE: remove the cache after you change the config!\nRegenerating the cache can take up to 3 hours for vanilla (5000 strings)\nWatch the log when in doubt\n\nThe amount of times to translate each element")
public int rounds = 10;
@Comment("Whether to fully break the texts content by translating from the wrong language (enable for complete breaking)")
public boolean breakFully = false;
@Comment("The API key for Yandex Translate (this is REQUIRED for updating the cache, since Yandex doesn't give them out for free anymore you might want to google \"trnsl.1.1.\" to find keys on the web)")
public String key = "trnsl.1.1.20130811T164454Z.2facd8a3323b8111.e9f682063308aff12357de3c8a3260d6d6b71be7";
@Comment("Use this if something is broken. This initiate the regeneration of the cache")
public boolean forceRegenerate = false;
}

View File

@ -1,5 +1,7 @@
package io.gitlab.jfronny.translater; package io.gitlab.jfronny.translater;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -8,6 +10,11 @@ import org.apache.logging.log4j.Logger;
public class ModInit implements ModInitializer { public class ModInit implements ModInitializer {
public static final String MOD_NAME = "translater"; public static final String MOD_NAME = "translater";
public static final String MOD_ID = "translater"; public static final String MOD_ID = "translater";
public static Cfg cfg;
public ModInit() {
AutoConfig.register(Cfg.class, JanksonConfigSerializer::new);
cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig();
}
private static final Logger logger = LogManager.getFormatterLogger(MOD_ID); private static final Logger logger = LogManager.getFormatterLogger(MOD_ID);
@Override @Override

View File

@ -0,0 +1,15 @@
package io.gitlab.jfronny.translater;
import io.github.prospector.modmenu.api.ConfigScreenFactory;
import io.github.prospector.modmenu.api.ModMenuApi;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@Environment(EnvType.CLIENT)
public class ModMenu implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return screen -> AutoConfig.getConfigScreen(Cfg.class, screen).get();
}
}

View File

@ -4,46 +4,87 @@ import com.github.vbauer.yta.model.Direction;
import com.github.vbauer.yta.model.Language; import com.github.vbauer.yta.model.Language;
import com.github.vbauer.yta.model.Languages; import com.github.vbauer.yta.model.Languages;
import com.github.vbauer.yta.service.YTranslateApiImpl; import com.github.vbauer.yta.service.YTranslateApiImpl;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.commons.lang3.StringUtils;
import java.util.*; import java.util.*;
public class YnTransformer extends CachingTransformer { public class YnTransformer extends CachingTransformer {
private static final int rounds = 10;
private static final boolean breakFully = true;
private static final String key = "trnsl.1.1.20200118T180605Z.654f2ec649458c36.107c6ad38dc02937f25e660aa1f8f4097d6561a8";
private static final YTranslateApiImpl api = new YTranslateApiImpl(key); private static YTranslateApiImpl api = null;
private static final Set<Language> languages = api.languageApi().all().languages(); private static Set<Language> languages = null;
private static final Random rnd = new Random(); private static final Random rnd = new Random();
@Override @Override
public String getTransformed(String str) { public String getTransformed(String str) {
try {
if (api == null)
api = new YTranslateApiImpl(ModInit.cfg.key);
if (languages == null)
languages = api.languageApi().all().languages();
if (str.contains("%")) { if (str.contains("%")) {
StringBuilder res = new StringBuilder(); StringBuilder res = new StringBuilder();
boolean f = true; boolean f = true;
for (String s : str.split("%")) { for (String s : str.split("%")) {
if (!f) { if (!f) {
res.append(s.charAt(0)).append(Break(s.substring(1)).replace("%", "")); res.append("%");
if (s.length() > 0)
res.append(s.charAt(0)).append(getTransformed(s.substring(1)).replace("%", ""));
} else } else
res.append(Break(s)); res.append(getTransformed(s));
f = false; f = false;
} }
return res.toString(); return res.toString();
} else { }
else if (str.contains("$")) {
StringBuilder res = new StringBuilder();
boolean f = true;
for (String s : str.split("\\$")) {
if (!f) {
res.append("$").append(s.charAt(0)).append(getTransformed(s.substring(1)).replace("$", ""));
} else
res.append(getTransformed(s));
f = false;
}
return res.toString();
}
else if (str.contains("§")) {
StringBuilder res = new StringBuilder();
boolean f = true;
for (String s : str.split("§")) {
if (!f) {
res.append("§").append(s.charAt(0)).append(getTransformed(s.substring(1)).replace("§", ""));
} else
res.append(getTransformed(s));
f = false;
}
return res.toString();
}
else {
return Break(str); return Break(str);
} }
} catch (Exception e) {
ModInit.Warn("Failed to transform: " + str + " (" + str.length() + " characters)");
ModInit.Warn("Please report this bug with the mod containing the lang file");
throw e;
}
} }
private String Break(String str) { private String Break(String str) {
try { try {
if (str.replace(" ", "").replace("[", "").replace("]", "").length() < 2) if (!valid(str))
return str; return str;
Language startLang = api.detectionApi().detect(str).get(); Language startLang = api.detectionApi().detect(str).orElse(Language.EN);
if (!valid(startLang.code())) {
ModInit.Warn("Could not detect language for: " + str);
ModInit.Warn("Defaulting to EN");
startLang = Language.EN;
}
String currentState = str; String currentState = str;
Language currentLang = startLang; Language currentLang = startLang;
for (int i = 0; i < rounds; i++) { for (int i = 0; i < ModInit.cfg.rounds; i++) {
Language newLang = selectRandom(); Language newLang = selectRandom();
currentState = api.translationApi().translate(currentState, Direction.of(breakFully ? selectRandom() : currentLang, newLang)).text(); currentState = api.translationApi().translate(currentState, Direction.of(ModInit.cfg.breakFully ? selectRandom() : currentLang, newLang)).text();
currentLang = newLang; currentLang = newLang;
} }
currentState = api.translationApi().translate(currentState, startLang).text(); currentState = api.translationApi().translate(currentState, startLang).text();
@ -56,6 +97,17 @@ public class YnTransformer extends CachingTransformer {
} }
} }
private boolean valid(String str) {
if (str.length() < 2)
return false;
for (char c : str.toCharArray()) {
String tmp = "" + c;
if (StringUtils.isAlphanumeric(tmp))
return true;
}
return false;
}
private Language selectRandom() { private Language selectRandom() {
return languages.stream().skip(rnd.nextInt(languages.size())).findFirst().get(); return languages.stream().skip(rnd.nextInt(languages.size())).findFirst().get();
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 453 B

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -19,7 +19,11 @@
"entrypoints": { "entrypoints": {
"main": [ "main": [
"io.gitlab.jfronny.translater.ModInit" "io.gitlab.jfronny.translater.ModInit"
],
"modmenu": [
"io.gitlab.jfronny.translater.ModMenu"
] ]
}, },
"mixins": [ "mixins": [
"translater.mixins.json" "translater.mixins.json"

File diff suppressed because it is too large Load Diff