[translate] Adjust config implementation for new features

This commit is contained in:
Johannes Frohnmeyer 2022-08-28 13:07:34 +02:00
parent 55bfa434ec
commit 6dcb2defa5
Signed by: Johannes
GPG Key ID: E76429612C2929F4
9 changed files with 47 additions and 43 deletions

View File

@ -153,6 +153,7 @@ public class StreamAction {
final ConfigInjectClassTransformer transformer = new ConfigInjectClassTransformer(modId, writer, knownConfigClasses); final ConfigInjectClassTransformer transformer = new ConfigInjectClassTransformer(modId, writer, knownConfigClasses);
reader.accept(transformer, ClassReader.EXPAND_FRAMES); reader.accept(transformer, ClassReader.EXPAND_FRAMES);
klazz = writer.toByteArray(); klazz = writer.toByteArray();
System.out.println("Injected config registration into " + path);
} catch (NotAConfigClassException notAConfigClass) { } catch (NotAConfigClassException notAConfigClass) {
// Use original bytes // Use original bytes
} }

View File

@ -14,7 +14,7 @@ public sealed interface Type {
else if (klazz == String.class) return TString.INSTANCE; else if (klazz == String.class) return TString.INSTANCE;
else if (klazz == boolean.class || klazz == Boolean.class) return TBool.INSTANCE; else if (klazz == boolean.class || klazz == Boolean.class) return TBool.INSTANCE;
else if (klazz instanceof Class<?> k && k.isEnum()) return new TEnum<>(k); else if (klazz instanceof Class<?> k && k.isEnum()) return new TEnum<>(k);
else return new TUnknown<>(klazz); else return new TUnknown(klazz);
} }
default boolean isInt() { default boolean isInt() {
@ -38,6 +38,11 @@ public sealed interface Type {
default boolean isEnum() { default boolean isEnum() {
return false; return false;
} }
@SuppressWarnings("unchecked")
default <T> TEnum<T> asEnum() {
if (this instanceof TEnum<?> c) return (TEnum<T>) c;
throw new ClassCastException("This type is not an enum");
}
@Nullable java.lang.reflect.Type asClass(); @Nullable java.lang.reflect.Type asClass();
@ -189,7 +194,7 @@ public sealed interface Type {
} }
} }
final record TUnknown<T>(java.lang.reflect.Type klazz) implements Type { final record TUnknown(java.lang.reflect.Type klazz) implements Type {
@Override @Override
public @Nullable java.lang.reflect.Type asClass() { public @Nullable java.lang.reflect.Type asClass() {
return klazz; return klazz;

View File

@ -15,8 +15,7 @@ import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -48,11 +47,17 @@ public class EntryInfoWidgetBuilder {
value -> !(Boolean) value, value -> !(Boolean) value,
value -> Text.literal((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED)); value -> Text.literal((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED));
} else if (type.isEnum()) { } else if (type.isEnum()) {
T[] values = ((Type.TEnum<T>)type).options(); T[] values = type.<T>asEnum().options();
factory = toggle(info, state, value -> { factory = toggle(info, state, value -> {
int index = indexOf(values, value) + 1; int index = indexOf(values, value) + 1;
return values[index >= values.length ? 0 : index]; return values[index >= values.length ? 0 : index];
}, value -> Text.translatable(config.getTranslationPrefix() + "enum." + type.getName() + "." + state.cachedValue)); }, value -> {
if (type.asClass() == null) {
return Text.translatable(config.getTranslationPrefix() + info.getName() + "." + state.cachedValue);
} else {
return Text.translatable(config.getTranslationPrefix() + "enum." + type.getName() + "." + state.cachedValue);
}
});
} else { } else {
LibJf.LOGGER.error("Unsupported entry type in " + info.getName() + ": " + type.getName() + " - not displaying config control"); LibJf.LOGGER.error("Unsupported entry type in " + info.getName() + ": " + type.getName() + " - not displaying config control");
factory = ((screenWidth, textRenderer, done) -> new WidgetFactory.Widget(() -> {}, new ButtonWidget(-10, 0, 0, 0, Text.of(""), null))); factory = ((screenWidth, textRenderer, done) -> new WidgetFactory.Widget(() -> {}, new ButtonWidget(-10, 0, 0, 0, Text.of(""), null)));

View File

@ -16,25 +16,13 @@ public interface TranslateService<T extends Language> {
* @return configured translation service * @return configured translation service
*/ */
static TranslateService<?> getConfigured() { static TranslateService<?> getConfigured() {
return switch (TranslateConfig.translationService) { return getAvailable().stream()
case Noop -> NoopTranslateService.INSTANCE; .filter(svc -> svc.getName().equals(TranslateConfig.translationService))
case Google -> { .findFirst()
try { .orElseGet(() -> {
yield GoogleTranslateService.get(); LibJf.LOGGER.error("Falling back to NOOP translate service");
} catch (URISyntaxException | IOException e) { return NoopTranslateService.INSTANCE;
LibJf.LOGGER.error("Could not create google translate service"); });
yield NoopTranslateService.INSTANCE;
}
}
case LibreTranslate -> {
try {
yield LibreTranslateService.get(TranslateConfig.libreTranslateHost);
} catch (TranslateException e) {
LibJf.LOGGER.error("Could not use the specified LibreTranslate host, using NOOP", e);
yield NoopTranslateService.INSTANCE;
}
}
};
} }
/** /**
@ -45,12 +33,15 @@ public interface TranslateService<T extends Language> {
List<TranslateService<?>> result = new LinkedList<>(); List<TranslateService<?>> result = new LinkedList<>();
try { try {
result.add(GoogleTranslateService.get()); result.add(GoogleTranslateService.get());
} catch (URISyntaxException | IOException ignored) { } catch (URISyntaxException | IOException e) {
LibJf.LOGGER.error("Could not create Google Translate service", e);
} }
try { try {
result.add(LibreTranslateService.get(TranslateConfig.libreTranslateHost)); result.add(LibreTranslateService.get(TranslateConfig.libreTranslateHost));
} catch (TranslateException ignored) { } catch (TranslateException e) {
LibJf.LOGGER.error("Could not create LibreTranslate service", e);
} }
if (result.isEmpty()) result.add(NoopTranslateService.INSTANCE);
return List.copyOf(result); return List.copyOf(result);
} }

View File

@ -1,15 +1,18 @@
package io.gitlab.jfronny.libjf.translate.impl; package io.gitlab.jfronny.libjf.translate.impl;
import io.gitlab.jfronny.libjf.config.api.v1.*; import io.gitlab.jfronny.libjf.config.api.v1.JfCustomConfig;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.DSL; import io.gitlab.jfronny.libjf.config.api.v1.dsl.DSL;
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
import io.gitlab.jfronny.libjf.translate.impl.google.GoogleTranslateService;
import io.gitlab.jfronny.libjf.translate.impl.libretranslate.LibreTranslateService;
public class TranslateConfig implements JfCustomConfig { public class TranslateConfig implements JfCustomConfig {
public static Translator translationService = Translator.Google; public static String translationService = GoogleTranslateService.NAME;
public static String libreTranslateHost = "https://translate.argosopentech.com"; public static String libreTranslateHost = "https://translate.argosopentech.com";
public static void ensureValid() { public static void ensureValid() {
if (translationService == null) translationService = Translator.Google; if (translationService == null) translationService = GoogleTranslateService.NAME;
if (translationService == Translator.LibreTranslate && libreTranslateHost == null || libreTranslateHost.isBlank()) if (translationService.equals(LibreTranslateService.NAME) && libreTranslateHost == null || libreTranslateHost.isBlank())
libreTranslateHost = "https://translate.argosopentech.com"; libreTranslateHost = "https://translate.argosopentech.com";
} }
@ -17,13 +20,9 @@ public class TranslateConfig implements JfCustomConfig {
public void register(DSL.Defaulted dsl) { public void register(DSL.Defaulted dsl) {
} }
public enum Translator {
Google, LibreTranslate, Noop
}
static { static {
DSL.create("libjf-translate-v1").register(b -> b DSL.create("libjf-translate-v1").register(b -> b
.value("translationService", translationService, Translator.class, () -> translationService, v -> translationService = v) .value("translationService", translationService, TranslateService.getAvailable().stream().map(TranslateService::getName).toArray(String[]::new), () -> translationService, v -> translationService = v)
.value("libreTranslateHost", libreTranslateHost, () -> libreTranslateHost, v -> libreTranslateHost = v) .value("libreTranslateHost", libreTranslateHost, () -> libreTranslateHost, v -> libreTranslateHost = v)
.addVerifier(TranslateConfig::ensureValid) .addVerifier(TranslateConfig::ensureValid)
); );

View File

@ -12,6 +12,7 @@ import java.util.*;
import java.util.regex.*; import java.util.regex.*;
public class GoogleTranslateService implements TranslateService<GoogleTranslateLanguage> { public class GoogleTranslateService implements TranslateService<GoogleTranslateLanguage> {
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);
private static final Pattern LANGUAGE_KEY = Pattern.compile("<div class=\"language-item\"><a href=\"\\./m\\?sl&amp;tl=([a-zA-Z\\-]+)&amp;hl=[a-zA-Z\\-]+\">([^<]+)</a></div>", Pattern.MULTILINE); private static final Pattern LANGUAGE_KEY = Pattern.compile("<div class=\"language-item\"><a href=\"\\./m\\?sl&amp;tl=([a-zA-Z\\-]+)&amp;hl=[a-zA-Z\\-]+\">([^<]+)</a></div>", Pattern.MULTILINE);
@ -83,7 +84,7 @@ public class GoogleTranslateService implements TranslateService<GoogleTranslateL
@Override @Override
public String getName() { public String getName() {
return "Google"; 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 Exception {

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.libjf.translate.impl.libretranslate; package io.gitlab.jfronny.libjf.translate.impl.libretranslate;
import com.google.common.reflect.TypeToken; import io.gitlab.jfronny.gson.reflect.TypeToken;
import io.gitlab.jfronny.libjf.HttpUtils; import io.gitlab.jfronny.libjf.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.api.TranslateService;
@ -12,6 +12,7 @@ import java.net.URISyntaxException;
import java.util.*; import java.util.*;
public class LibreTranslateService implements TranslateService<LibreTranslateLanguage> { public class LibreTranslateService implements TranslateService<LibreTranslateLanguage> {
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();
private static final LibreTranslateLanguage autoDetect = new LibreTranslateLanguage("auto", "AUTO_DETECT"); private static final LibreTranslateLanguage autoDetect = new LibreTranslateLanguage("auto", "AUTO_DETECT");
@ -101,6 +102,6 @@ public class LibreTranslateService implements TranslateService<LibreTranslateLan
@Override @Override
public String getName() { public String getName() {
return "LibreTranslate"; return NAME;
} }
} }

View File

@ -6,6 +6,7 @@ import io.gitlab.jfronny.libjf.translate.api.TranslateService;
import java.util.List; import java.util.List;
public class NoopTranslateService implements TranslateService<NoopLanguage> { public class NoopTranslateService implements TranslateService<NoopLanguage> {
public static final String NAME = "Noop";
public static final NoopTranslateService INSTANCE = new NoopTranslateService(); public static final NoopTranslateService INSTANCE = new NoopTranslateService();
private NoopTranslateService() { private NoopTranslateService() {
} }
@ -32,6 +33,6 @@ public class NoopTranslateService implements TranslateService<NoopLanguage> {
@Override @Override
public String getName() { public String getName() {
return "Noop"; return NAME;
} }
} }

View File

@ -4,7 +4,7 @@
"libjf-translate-v1.jfconfig.translationService.tooltip": "The service to use for translation. Other mods may access services directly, but this should be used", "libjf-translate-v1.jfconfig.translationService.tooltip": "The service to use for translation. Other mods may access services directly, but this should be used",
"libjf-translate-v1.jfconfig.libreTranslateHost": "LibreTranslate Host", "libjf-translate-v1.jfconfig.libreTranslateHost": "LibreTranslate Host",
"libjf-translate-v1.jfconfig.libreTranslateHost.tooltip": "The host of LibreTranslate to use if that is selected", "libjf-translate-v1.jfconfig.libreTranslateHost.tooltip": "The host of LibreTranslate to use if that is selected",
"libjf-translate-v1.jfconfig.enum.Translator.Google": "Google", "libjf-translate-v1.jfconfig.translationService.Google": "Google",
"libjf-translate-v1.jfconfig.enum.Translator.LibreTranslate": "LibreTranslate", "libjf-translate-v1.jfconfig.translationService.LibreTranslate": "LibreTranslate",
"libjf-translate-v1.jfconfig.enum.Translator.Noop": "NOOP" "libjf-translate-v1.jfconfig.translationService.Noop": "NOOP"
} }