Delay TRANSLATE_SERVICE initialization to reduce the cost on startup
This commit is contained in:
parent
476539c584
commit
805eb24fa6
|
@ -13,5 +13,5 @@ dependencies {
|
|||
// Dev env
|
||||
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-config-ui-tiny-v1:${prop("libjf_version")}")
|
||||
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-devutil:${prop("libjf_version")}")
|
||||
modLocalRuntime("com.terraformersmc:modmenu:6.1.0-rc.1")
|
||||
modLocalRuntime("com.terraformersmc:modmenu:6.2.2")
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# https://fabricmc.net/develop/
|
||||
minecraft_version=1.19.4
|
||||
yarn_mappings=build.1
|
||||
loader_version=0.14.17
|
||||
yarn_mappings=build.2
|
||||
loader_version=0.14.19
|
||||
|
||||
maven_group=io.gitlab.jfronny
|
||||
archives_base_name=google-chat
|
||||
|
@ -13,5 +13,5 @@ curseforge_id=574331
|
|||
curseforge_required_dependencies=libjf
|
||||
curseforge_optional_dependencies=modmenu
|
||||
|
||||
libjf_version=3.6.0
|
||||
fabric_version=0.75.3+1.19.4
|
||||
libjf_version=3.7.2
|
||||
fabric_version=0.81.1+1.19.4
|
||||
|
|
|
@ -1,18 +1,26 @@
|
|||
package io.gitlab.jfronny.googlechat;
|
||||
|
||||
import io.gitlab.jfronny.commons.log.Logger;
|
||||
import io.gitlab.jfronny.libjf.translate.api.*;
|
||||
import io.gitlab.jfronny.libjf.translate.api.Language;
|
||||
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.text.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
|
||||
public class GoogleChat {
|
||||
public class GoogleChat implements ModInitializer {
|
||||
public static final String MOD_ID = "google-chat";
|
||||
public static final Logger LOGGER = Logger.forName(MOD_ID);
|
||||
public static final TranslateService<?> TRANSLATE_SERVICE = TranslateService.getConfigured();
|
||||
public static TranslateService<?> TRANSLATE_SERVICE;
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
ForkJoinPool.commonPool().execute(() -> TRANSLATE_SERVICE = TranslateService.getConfigured());
|
||||
}
|
||||
|
||||
public static Text translateIfNeeded(Text source, Direction direction, boolean respectRegex) {
|
||||
if (shouldSkipOutright(direction)) return source;
|
||||
|
@ -87,14 +95,15 @@ public class GoogleChat {
|
|||
public static String translateIfNeeded(String source, Direction direction, boolean respectRegex) {
|
||||
if (shouldSkipOutright(direction)) return source;
|
||||
if (respectRegex && failsRegex(source, direction)) return source;
|
||||
// Ignore generics since this is apparently not something java supports
|
||||
@SuppressWarnings("rawtypes") TranslateService svc = GoogleChat.TRANSLATE_SERVICE;
|
||||
Language sourceLang = svc.parseLang(direction.source());
|
||||
Language targetLang = svc.parseLang(direction.target());
|
||||
try {
|
||||
// Ignore generics since this is apparently not something java supports
|
||||
@SuppressWarnings("rawtypes") TranslateService svc = GoogleChat.TRANSLATE_SERVICE;
|
||||
if (svc == null) throw new NullPointerException("Translate service uninitialized");
|
||||
Language sourceLang = svc.parseLang(direction.source());
|
||||
Language targetLang = svc.parseLang(direction.target());
|
||||
//noinspection unchecked
|
||||
return svc.translate(source, sourceLang, targetLang);
|
||||
} catch (TranslateException e) {
|
||||
} catch (Throwable e) {
|
||||
LOGGER.error("Could not translate text: " + source, e);
|
||||
return source;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,8 @@
|
|||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"libjf:config": ["io.gitlab.jfronny.googlechat.JFC_GoogleChatConfig"],
|
||||
"server": ["io.gitlab.jfronny.googlechat.server.GoogleChatServer"]
|
||||
"server": ["io.gitlab.jfronny.googlechat.server.GoogleChatServer"],
|
||||
"main": ["io.gitlab.jfronny.googlechat.GoogleChat"]
|
||||
},
|
||||
"mixins": [
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue