2024-07-11 13:43:54 -05:00
|
|
|
package one.oth3r.sit.file;
|
2023-07-20 20:17:52 -05:00
|
|
|
|
2023-11-26 00:17:01 -06: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;
|
2024-07-11 13:43:54 -05:00
|
|
|
import one.oth3r.sit.Sit;
|
2024-07-23 13:49:43 -05:00
|
|
|
import one.oth3r.sit.utl.Data;
|
2023-07-20 20:17:52 -05:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
2023-11-26 00:17:01 -06:00
|
|
|
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 {
|
|
|
|
private static final Map<String, String> languageMap = new HashMap<>();
|
|
|
|
private final String translationKey;
|
|
|
|
private final Object[] placeholders;
|
|
|
|
public LangReader(String translationKey, Object... placeholders) {
|
|
|
|
this.translationKey = translationKey;
|
|
|
|
this.placeholders = placeholders;
|
|
|
|
}
|
|
|
|
public MutableText getTxT() {
|
|
|
|
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);
|
|
|
|
//Arraylist with all the %(#$)[dfs]
|
|
|
|
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) {
|
|
|
|
MutableText txt = Text.empty();
|
|
|
|
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
|
|
|
|
Object obj = placeholders[get];
|
|
|
|
if (obj instanceof Text) txt.append((Text) obj);
|
|
|
|
else txt.append(String.valueOf(obj));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (parts.length != i) txt.append(parts[i]);
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
}
|
2023-10-24 18:22:30 -05:00
|
|
|
return Text.empty().append(translated);
|
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() {
|
2024-07-11 13:43:54 -05:00
|
|
|
ClassLoader classLoader = Sit.class.getClassLoader();
|
2023-07-20 20:17:52 -05:00
|
|
|
try {
|
2024-07-23 12:27:01 -05:00
|
|
|
InputStream inputStream = classLoader.getResourceAsStream("assets/sit/lang/"+ FileData.getServerConfig().getLang() +".json");
|
2024-07-11 13:43:54 -05:00
|
|
|
|
|
|
|
// if the input stream is null, the language file wasn't found
|
2023-07-20 20:17:52 -05:00
|
|
|
if (inputStream == null) {
|
2024-07-11 13:43:54 -05:00
|
|
|
// try loading the default language file
|
|
|
|
inputStream = classLoader.getResourceAsStream("assets/sit/lang/"+ new ServerConfig().getLang() +".json");
|
|
|
|
//todo error message and reset back to EN_US
|
2023-07-20 20:17:52 -05:00
|
|
|
}
|
2024-07-11 13:43:54 -05:00
|
|
|
|
|
|
|
// if the input stream is still null, throw an exception
|
|
|
|
if (inputStream == null) throw new IllegalArgumentException("UNABLE TO LOAD THE LANGUAGE FILE.");
|
|
|
|
|
2023-11-26 00:17:01 -06:00
|
|
|
Type type = new TypeToken<Map<String, String>>(){}.getType();
|
|
|
|
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
|
|
|
languageMap.putAll(new Gson().fromJson(reader, type));
|
2024-07-11 13:43:54 -05:00
|
|
|
|
|
|
|
// close the input stream
|
|
|
|
inputStream.close();
|
2023-07-20 20:17:52 -05:00
|
|
|
} catch (Exception e) {
|
2024-07-23 13:49:43 -05:00
|
|
|
Data.LOGGER.error(e.getMessage());
|
2023-07-20 20:17:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public static String getLanguageValue(String key) {
|
|
|
|
return languageMap.getOrDefault(key, key);
|
|
|
|
}
|
|
|
|
}
|