mirror of
https://github.com/Oth3r/Sit.git
synced 2025-09-19 16:03:22 +02:00
switch to CTxT
This commit is contained in:
parent
e8947f2a24
commit
c3b7bb02fd
6 changed files with 79 additions and 50 deletions
|
@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
|||
import com.google.gson.reflect.TypeToken;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.Text;
|
||||
import one.oth3r.otterlib.chat.CTxT;
|
||||
import one.oth3r.sit.Sit;
|
||||
import one.oth3r.sit.utl.Data;
|
||||
|
||||
|
@ -20,14 +21,19 @@ import java.util.regex.Matcher;
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
public class LangReader {
|
||||
private static final Map<String, String> defaultLangMap = new HashMap<>();
|
||||
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() {
|
||||
|
||||
public CTxT getTxT() {
|
||||
String translated = getLanguageValue(translationKey);
|
||||
if (placeholders != null && placeholders.length > 0) {
|
||||
//removed all double \\ and replaces with \
|
||||
|
@ -35,7 +41,8 @@ public class LangReader {
|
|||
String regex = "%\\d*\\$?[dfs]";
|
||||
Matcher anyMatch = Pattern.compile(regex).matcher(translated);
|
||||
Matcher endMatch = Pattern.compile(regex+"$").matcher(translated);
|
||||
//Arraylist with all the %(#$)[dfs]
|
||||
|
||||
// Arraylist with all the %(#$)[dfs]
|
||||
ArrayList<String> matches = new ArrayList<>();
|
||||
while (anyMatch.find()) {
|
||||
String match = anyMatch.group();
|
||||
|
@ -51,7 +58,7 @@ public class LangReader {
|
|||
}
|
||||
//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();
|
||||
CTxT txt = new CTxT("");
|
||||
int i = 0;
|
||||
for (String match : matches) {
|
||||
int get = i;
|
||||
|
@ -62,47 +69,56 @@ public class LangReader {
|
|||
}
|
||||
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));
|
||||
txt.append(getTxTFromObj(placeholders[get]));
|
||||
i++;
|
||||
}
|
||||
if (parts.length != i) txt.append(parts[i]);
|
||||
return txt;
|
||||
return new CTxT(txt);
|
||||
}
|
||||
}
|
||||
return Text.empty().append(translated);
|
||||
return new CTxT(translated);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
public static LangReader of(String translationKey, Object... placeholders) {
|
||||
return new LangReader(translationKey, placeholders);
|
||||
}
|
||||
|
||||
public static void loadLanguageFile() {
|
||||
ClassLoader classLoader = Sit.class.getClassLoader();
|
||||
Type tToken = new TypeToken<Map<String, String>>(){}.getType();
|
||||
try {
|
||||
InputStream inputStream = classLoader.getResourceAsStream("assets/sit-oth3r/lang/" + FileData.getServerConfig().getLang() +".json");
|
||||
|
||||
// if the input stream is null, the language file wasn't found
|
||||
if (inputStream == null) {
|
||||
// try loading the default language file
|
||||
inputStream = classLoader.getResourceAsStream("assets/sit-oth3r/lang/" + new ServerConfig().getLang() +".json");
|
||||
Data.LOGGER.error("COULDN'T LOAD THE LANGUAGE FILE. RESETTING TO en_us.");
|
||||
}
|
||||
|
||||
// if the input stream is still null, throw an exception
|
||||
if (inputStream == null) throw new IllegalArgumentException("UNABLE TO LOAD THE ENGLISH LANGUAGE FILE.");
|
||||
|
||||
Type type = new TypeToken<Map<String, String>>(){}.getType();
|
||||
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
||||
languageMap.putAll(new Gson().fromJson(reader, type));
|
||||
|
||||
// close the input stream
|
||||
inputStream.close();
|
||||
// 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) {
|
||||
Data.LOGGER.error(e.getMessage());
|
||||
Data.LOGGER.info("ERROR WITH LANGUAGE FILE - PLEASE REPORT WITH THE ERROR LOG");
|
||||
Data.LOGGER.info(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// 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");
|
||||
|
||||
// if null after that, throw an exception
|
||||
if (inputStream == null) throw new IllegalArgumentException("CANT LOAD THE LANGUAGE FILE. SIT! WILL BREAK.");
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
public static String getLanguageValue(String key) {
|
||||
return languageMap.getOrDefault(key, key);
|
||||
return languageMap.getOrDefault(key, defaultLangMap.getOrDefault(key, key));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue