Missed changes

This commit is contained in:
rtm516 2020-04-05 02:37:39 +01:00
parent 7f1fb3d43c
commit b5ce83bbe2
2 changed files with 43 additions and 16 deletions

View file

@ -36,27 +36,43 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import org.geysermc.connector.GeyserConnector;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
public class MessageUtils { public class MessageUtils {
private static final HashMap<String, String> LANG_MAPPINGS = new HashMap<>(); private static final HashMap<String, HashMap<String, String>> LOCALE_MAPPINGS = new HashMap<>();
static { static {
/* Load the language mappings */ /* Load the language mappings */
InputStream stream = Toolbox.getResource("mappings/lang.json"); InputStream languagesStream = Toolbox.getResource("mappings/locales.json");
JsonNode locales;
try {
locales = Toolbox.JSON_MAPPER.readTree(languagesStream);
} catch (Exception e) {
throw new AssertionError("Unable to load Java locale list", e);
}
for (JsonNode localeNode : locales.get("locales")) {
String currentLocale = localeNode.asText();
InputStream stream = Toolbox.getResource("mappings/lang/" + currentLocale + ".json");
JsonNode lang; JsonNode lang;
try { try {
lang = Toolbox.JSON_MAPPER.readTree(stream); lang = Toolbox.JSON_MAPPER.readTree(stream);
} catch (Exception e) { } catch (Exception e) {
throw new AssertionError("Unable to load Java lang mappings", e); throw new AssertionError("Unable to load Java lang map for " + currentLocale, e);
} }
Iterator<Map.Entry<String, JsonNode>> langIterator = lang.fields(); Iterator<Map.Entry<String, JsonNode>> langIterator = lang.fields();
HashMap<String, String> langMap = new HashMap<>();
while (langIterator.hasNext()) { while (langIterator.hasNext()) {
Map.Entry<String, JsonNode> entry = langIterator.next(); Map.Entry<String, JsonNode> entry = langIterator.next();
LANG_MAPPINGS.put(entry.getKey(), entry.getValue().asText()); langMap.put(entry.getKey(), entry.getValue().asText());
}
LOCALE_MAPPINGS.put(currentLocale.toLowerCase(), langMap);
} }
} }
@ -94,7 +110,7 @@ public class MessageUtils {
+ "%" + message.getTranslationKey(); + "%" + message.getTranslationKey();
} }
public static String getBedrockMessageWithTranslate(Message message, boolean convertLangStrings) { public static String getBedrockMessageWithTranslate(Message message, String locale) {
JsonParser parser = new JsonParser(); JsonParser parser = new JsonParser();
if (isMessage(message.getText())) { if (isMessage(message.getText())) {
JsonObject object = parser.parse(message.getText()).getAsJsonObject(); JsonObject object = parser.parse(message.getText()).getAsJsonObject();
@ -102,8 +118,8 @@ public class MessageUtils {
} }
String messageText = message.getText(); String messageText = message.getText();
if (convertLangStrings) { if (locale != null) {
messageText = getLangConversion(messageText); messageText = getLocaleString(messageText, locale);
} }
StringBuilder builder = new StringBuilder(messageText); StringBuilder builder = new StringBuilder(messageText);
@ -117,12 +133,23 @@ public class MessageUtils {
return builder.toString(); return builder.toString();
} }
private static String getLangConversion(String messageText) { public static String getBedrockMessageWithTranslate(Message message) {
return LANG_MAPPINGS.getOrDefault(messageText, messageText); return getBedrockMessageWithTranslate(message, null);
}
private static String getLocaleString(String messageText, String locale) {
HashMap<String, String> localeStrings = LOCALE_MAPPINGS.get(locale.toLowerCase());
if (localeStrings == null)
localeStrings = LOCALE_MAPPINGS.get("en_us");
String newLocaleString = localeStrings.getOrDefault(messageText, messageText);
GeyserConnector.getInstance().getLogger().info("Converting '" + messageText + "' -> '" + newLocaleString + "'");
return newLocaleString;
} }
public static String getBedrockMessage(Message message) { public static String getBedrockMessage(Message message) {
return getBedrockMessageWithTranslate(message, false); return getBedrockMessageWithTranslate(message);
} }
private static String getColor(ChatColor color) { private static String getColor(ChatColor color) {

View file

@ -40,7 +40,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.translators.item.ItemEntry; import org.geysermc.connector.network.translators.item.ItemEntry;
import java.io.InputStream; import java.io.*;
import java.util.*; import java.util.*;
public class Toolbox { public class Toolbox {