top.hendrixshen.magiclib.language.impl.MagicLanguageManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of magiclib-1_14_4 Show documentation
Show all versions of magiclib-1_14_4 Show documentation
To beat magic with magic :(
package top.hendrixshen.magiclib.language.impl;
import com.google.common.collect.Lists;
import lombok.Getter;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.class_2960;
import net.minecraft.class_310;
import net.minecraft.class_3298;
import net.minecraft.class_3300;
import net.minecraft.class_3304;
import net.minecraft.class_4013;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.MagicLibReference;
import top.hendrixshen.magiclib.impl.config.ConfigEntrypoint;
import top.hendrixshen.magiclib.util.MiscUtil;
//#if MC <= 11802
import java.io.FileNotFoundException;
//#endif
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class MagicLanguageManager implements class_4013 {
public static final String DEFAULT_CODE = "en_us";
public static final MagicLanguageManager INSTANCE = new MagicLanguageManager();
public ConcurrentHashMap defaultLanguage = new ConcurrentHashMap<>();
public ConcurrentHashMap> language = new ConcurrentHashMap<>();
ArrayList fallbackLanguageList = Lists.newArrayList(DEFAULT_CODE);
@Getter
private String currentCode = DEFAULT_CODE;
@NotNull
private class_3300 resourceManager;
private MagicLanguageManager() {
this.resourceManager = new MagicLanguageResourceManager();
this.reload();
}
public void setCurrentCode(String currentCode) {
if (!Objects.equals(this.currentCode, currentCode)) {
this.currentCode = currentCode;
this.reload();
}
}
@Environment(EnvType.CLIENT)
public void initClient() {
this.resourceManager = class_310.method_1551().method_1478();
this.fallbackLanguageList = Lists.newArrayList(ConfigEntrypoint.getFallbackLanguageListFromConfig());
((class_3304) class_310.method_1551().method_1478()).method_14477(INSTANCE);
this.reload();
}
private void initLanguage(String code, ConcurrentHashMap language) {
String languagePath = String.format("lang/%s.json", code);
Set nameSpaces;
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
// 1.15 的 server 缺少 ResourceManager.getNamespaces
nameSpaces = ((MagicLanguageResourceManager) resourceManager).method_14487();
} else {
nameSpaces = resourceManager.method_14487();
}
for (String namespace : nameSpaces) {
class_2960 resourceLocation = new class_2960(namespace, languagePath);
try {
for (class_3298 resource : resourceManager.method_14489(resourceLocation)) {
try {
InputStream inputStream = resource.method_14482();
try {
MiscUtil.loadStringMapFromJson(inputStream, language::put);
} catch (Throwable e) {
if (inputStream != null) {
inputStream.close();
}
MagicLibReference.getLogger().warn("Failed to parse translations from {} ({})", resource, e);
continue;
}
// from minecraft code
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
MagicLibReference.getLogger().warn("Failed to load translations from {} ({})", resource, e);
}
}
//#if MC <= 11802
} catch (FileNotFoundException ignored) {
// ignore..
//#endif
} catch (Exception e) {
MagicLibReference.getLogger().warn("Failed to load translations from {}:{} ({})", namespace, languagePath, e);
}
}
}
public void reload() {
this.defaultLanguage.clear();
this.language.clear();
ArrayList codes = new ArrayList<>(fallbackLanguageList);
codes.remove(currentCode);
codes.add(0, currentCode);
if (!codes.contains(DEFAULT_CODE)) {
codes.add(DEFAULT_CODE);
}
for (int i = codes.size() - 1; i >= 0; --i) {
String code = codes.get(i);
ConcurrentHashMap currentLanguage = new ConcurrentHashMap<>();
initLanguage(code, currentLanguage);
this.language.put(code, currentLanguage);
this.defaultLanguage.putAll(currentLanguage);
}
}
@Override
public void method_14491(@NotNull class_3300 resourceManager) {
reload();
}
public String get(String key, Object... objects) {
String translateValue = this.defaultLanguage.getOrDefault(key, key);
try {
return String.format(translateValue, objects);
} catch (IllegalFormatException var4) {
return "Format error: " + translateValue;
}
}
public String get(String key) {
return this.defaultLanguage.getOrDefault(key, key);
}
public String getByCode(String code, String key) {
ConcurrentHashMap currentLanguage = this.language.getOrDefault(code, null);
if (currentLanguage == null) {
currentLanguage = new ConcurrentHashMap<>();
this.initLanguage(code, currentLanguage);
this.language.put(code, currentLanguage);
}
return currentLanguage.getOrDefault(key, key);
}
public String getByCode(String code, String key, Object... objects) {
ConcurrentHashMap currentLanguage = this.language.getOrDefault(code, null);
if (currentLanguage == null) {
currentLanguage = new ConcurrentHashMap<>();
this.initLanguage(code, currentLanguage);
this.language.put(code, currentLanguage);
}
String translateValue = currentLanguage.getOrDefault(key, key);
try {
return String.format(translateValue, objects);
} catch (IllegalFormatException var4) {
return "Format error: " + translateValue;
}
}
public boolean exists(String key) {
return this.defaultLanguage.containsKey(key);
}
public boolean exists(String code, String key) {
ConcurrentHashMap currentLanguage = this.language.getOrDefault(code, null);
if (currentLanguage == null) {
currentLanguage = new ConcurrentHashMap<>();
this.initLanguage(code, currentLanguage);
this.language.put(code, currentLanguage);
}
return currentLanguage.containsKey(key);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy