fix: use new default translater cache entries on existing translations
This commit is contained in:
parent
a614dc95c6
commit
dfea11cafd
|
@ -10,6 +10,7 @@ public class Cfg {
|
|||
@Entry public static String targetLanguage = "en";
|
||||
@Entry public static ProgressMode renderProgress = ProgressMode.None;
|
||||
@Entry public static boolean forceRegenerate = false;
|
||||
@Entry public static boolean useDefaultCache = true;
|
||||
|
||||
static {
|
||||
JFC_Cfg.ensureInitialized();
|
||||
|
|
|
@ -4,6 +4,7 @@ import io.gitlab.jfronny.libjf.config.api.v2.ConfigHolder;
|
|||
import io.gitlab.jfronny.translater.Cfg;
|
||||
import io.gitlab.jfronny.translater.Translater;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
|
@ -12,6 +13,7 @@ import java.util.Properties;
|
|||
|
||||
public class CachingTransformer implements Transformer {
|
||||
private static final Path CACHE_FILE = FabricLoader.getInstance().getConfigDir().resolve(Translater.MOD_ID + ".cache");
|
||||
private final @Nullable Properties defaultCache;
|
||||
private final Properties cache = new Properties();
|
||||
private final Transformer transformer;
|
||||
|
||||
|
@ -21,6 +23,7 @@ public class CachingTransformer implements Transformer {
|
|||
return null;
|
||||
//Transform and cache if not present
|
||||
if (!cache.containsKey(str)) {
|
||||
if (defaultCache != null && defaultCache.containsKey(str)) return (String) defaultCache.get(str);
|
||||
String transformed = transformer.transform(str);
|
||||
if (transformed == null) {
|
||||
// The transformer failed
|
||||
|
@ -47,28 +50,27 @@ public class CachingTransformer implements Transformer {
|
|||
} catch (IOException e) {
|
||||
Translater.LOGGER.error("Could not load translater cache", e);
|
||||
}
|
||||
} else {
|
||||
//Save default cache if parameters are default
|
||||
if (!Cfg.breakFully && Cfg.rounds == 5) {
|
||||
Translater.LOGGER.info("Initializing default cache");
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inS = classLoader.getResourceAsStream("namecache.ini");
|
||||
if (inS != null) {
|
||||
try {
|
||||
cache.load(inS);
|
||||
inS.close();
|
||||
save();
|
||||
} catch (IOException e) {
|
||||
Translater.LOGGER.error("Could not initialize default translater cache");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//Save default cache if parameters are default
|
||||
if (!Cfg.breakFully && Cfg.rounds == 5 && "en".equals(Cfg.targetLanguage) && !Cfg.forceRegenerate) {
|
||||
Translater.LOGGER.info("Initializing default cache");
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Properties p = new Properties();
|
||||
try (InputStream inS = classLoader.getResourceAsStream("namecache.ini")) {
|
||||
if (inS == null) p = null;
|
||||
else p.load(inS);
|
||||
} catch (IOException e) {
|
||||
p = null;
|
||||
Translater.LOGGER.error("Could not initialize default translater cache", e);
|
||||
}
|
||||
defaultCache = p;
|
||||
} else defaultCache = null;
|
||||
save();
|
||||
}
|
||||
|
||||
private void save() {
|
||||
if (cache.isEmpty()) return;
|
||||
try (OutputStream outS = Files.newOutputStream(CACHE_FILE)) {
|
||||
cache.store(outS, "---Lang---");
|
||||
} catch (IOException e) {
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
"translater.jfconfig.renderProgress.tooltip": "Significantly slows down the loading time but gives a visual of the progress. Values: Full, Console, None",
|
||||
"translater.jfconfig.forceRegenerate": "Force Regenerate",
|
||||
"translater.jfconfig.forceRegenerate.tooltip": "Use this if something is broken. This initiates the regeneration of the cache",
|
||||
"translater.jfconfig.useDefaultCache": "Use default Cache",
|
||||
"translater.jfconfig.useDefaultCache.tooltip": "Use pre-generated translations shipped with the mod if the config permits to save time",
|
||||
"translater.jfconfig.enum.ProgressMode.Full": "Full",
|
||||
"translater.jfconfig.enum.ProgressMode.Gui": "Gui",
|
||||
"translater.jfconfig.enum.ProgressMode.Console": "Console",
|
||||
|
|
Loading…
Reference in New Issue