Sit/src/main/java/one/oth3r/sit/file/LangReader.java

124 lines
5.1 KiB
Java
Raw Normal View History

2024-07-11 13:43:54 -05:00
package one.oth3r.sit.file;
2023-07-20 20:17:52 -05:00
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
2023-07-20 20:17:52 -05:00
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
2025-05-03 15:41:16 -05:00
import one.oth3r.otterlib.chat.CTxT;
2024-07-11 13:43:54 -05:00
import one.oth3r.sit.Sit;
import one.oth3r.sit.utl.Data;
2023-07-20 20:17:52 -05:00
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
2023-07-20 20:17:52 -05:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LangReader {
2025-05-03 15:41:16 -05:00
private static final Map<String, String> defaultLangMap = new HashMap<>();
2023-07-20 20:17:52 -05:00
private static final Map<String, String> languageMap = new HashMap<>();
2025-05-03 15:41:16 -05:00
2023-07-20 20:17:52 -05:00
private final String translationKey;
2025-05-03 15:41:16 -05:00
2023-07-20 20:17:52 -05:00
private final Object[] placeholders;
2025-05-03 15:41:16 -05:00
2023-07-20 20:17:52 -05:00
public LangReader(String translationKey, Object... placeholders) {
this.translationKey = translationKey;
this.placeholders = placeholders;
}
2025-05-03 15:41:16 -05:00
public CTxT getTxT() {
2023-07-20 20:17:52 -05:00
String translated = getLanguageValue(translationKey);
if (placeholders != null && placeholders.length > 0) {
//removed all double \\ and replaces with \
translated = translated.replaceAll("\\\\\\\\", "\\\\");
String regex = "%\\d*\\$?[dfs]";
Matcher anyMatch = Pattern.compile(regex).matcher(translated);
Matcher endMatch = Pattern.compile(regex+"$").matcher(translated);
2025-05-03 15:41:16 -05:00
// Arraylist with all the %(#$)[dfs]
2023-07-20 20:17:52 -05:00
ArrayList<String> matches = new ArrayList<>();
while (anyMatch.find()) {
String match = anyMatch.group();
matches.add(match);
}
//SPLITS the text at each regex and remove the regex
String[] parts = translated.split(regex);
//if the last element of the array ends with regex, remove it and add an empty string to the end of the array
if (endMatch.find()) {
String[] newParts = Arrays.copyOf(parts, parts.length + 1);
newParts[parts.length] = "";
parts = newParts;
}
//if there are placeholders specified, and the split is more than 1, it will replace %(dfs) with the placeholder objects
if (parts.length > 1) {
2025-05-03 15:41:16 -05:00
CTxT txt = new CTxT("");
2023-07-20 20:17:52 -05:00
int i = 0;
for (String match : matches) {
int get = i;
//if the match is numbered, change GET to the number it wants
if (match.contains("$")) {
match = match.substring(1,match.indexOf('$'));
get = Integer.parseInt(match)-1;
}
if (parts.length != i) txt.append(parts[i]);
//convert the obj into txt
2025-05-03 15:41:16 -05:00
txt.append(getTxTFromObj(placeholders[get]));
2023-07-20 20:17:52 -05:00
i++;
}
if (parts.length != i) txt.append(parts[i]);
2025-05-03 15:41:16 -05:00
return new CTxT(txt);
2023-07-20 20:17:52 -05:00
}
}
2025-05-03 15:41:16 -05:00
return new CTxT(translated);
2023-07-20 20:17:52 -05:00
}
2025-05-03 15:41:16 -05:00
private CTxT getTxTFromObj(Object obj) {
if (obj instanceof CTxT) return (((CTxT) obj));
else if (obj instanceof Text) return new CTxT((MutableText) obj);
else return new CTxT(String.valueOf(obj));
}
2023-07-20 20:17:52 -05:00
public static LangReader of(String translationKey, Object... placeholders) {
return new LangReader(translationKey, placeholders);
}
2024-07-11 13:43:54 -05:00
2023-07-20 20:17:52 -05:00
public static void loadLanguageFile() {
2025-05-03 15:41:16 -05:00
Type tToken = new TypeToken<Map<String, String>>(){}.getType();
2023-07-20 20:17:52 -05:00
try {
2025-05-03 15:41:16 -05:00
// load the config language
Reader selectionReader = new InputStreamReader(getInputStream(false), StandardCharsets.UTF_8);
languageMap.putAll(new Gson().fromJson(selectionReader, tToken));
// load the default language as well (fallback)
Reader defaultReader = new InputStreamReader(getInputStream(true), StandardCharsets.UTF_8);
defaultLangMap.putAll(new Gson().fromJson(defaultReader, tToken));
} catch (Exception e) {
2025-06-16 15:07:37 -05:00
Data.LOGGER.error("ERROR WITH LANGUAGE FILE - PLEASE REPORT WITH THE ERROR LOG");
Data.LOGGER.error(e.getMessage());
2025-05-03 15:41:16 -05:00
}
}
2024-07-11 13:43:54 -05:00
2025-05-03 15:41:16 -05:00
private static InputStream getInputStream(boolean english) {
ClassLoader classLoader = Sit.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("assets/sit-oth3r/lang/"+FileData.getServerConfig().getLang()+".json");
// make null if english
if (english) inputStream = null;
2024-07-11 13:43:54 -05:00
2025-05-03 15:41:16 -05:00
// if it cant read (null), try again, but with the english file
if (inputStream == null) inputStream = classLoader.getResourceAsStream("assets/sit-oth3r/lang/"+new ServerConfig().getLang()+".json");
2024-07-11 13:43:54 -05:00
2025-05-03 15:41:16 -05:00
// if null after that, throw an exception
if (inputStream == null) throw new IllegalArgumentException("CANT LOAD THE LANGUAGE FILE. SIT! WILL BREAK.");
return inputStream;
2023-07-20 20:17:52 -05:00
}
2025-05-03 15:41:16 -05:00
2023-07-20 20:17:52 -05:00
public static String getLanguageValue(String key) {
2025-05-03 15:41:16 -05:00
return languageMap.getOrDefault(key, defaultLangMap.getOrDefault(key, key));
2023-07-20 20:17:52 -05:00
}
}