mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Added translation of extra message parts
This commit is contained in:
parent
70a71b2ee8
commit
bc67675d16
3 changed files with 14 additions and 24 deletions
|
@ -60,25 +60,20 @@ public class JavaChatTranslator extends PacketTranslator<ServerChatPacket> {
|
|||
break;
|
||||
}
|
||||
|
||||
String locale = session.getClientData().getLanguageCode();
|
||||
|
||||
if (packet.getMessage() instanceof TranslationMessage) {
|
||||
textPacket.setType(TextPacket.Type.TRANSLATION);
|
||||
textPacket.setNeedsTranslation(true);
|
||||
|
||||
String locale = session.getClientData().getLanguageCode();
|
||||
|
||||
List<String> paramsTranslated = MessageUtils.getTranslationParams(((TranslationMessage) packet.getMessage()).getTranslationParams(), locale);
|
||||
textPacket.setParameters(paramsTranslated);
|
||||
|
||||
textPacket.setMessage(MessageUtils.insertParams(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale), paramsTranslated));
|
||||
textPacket.setMessage(MessageUtils.insertParams(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale, false), paramsTranslated));
|
||||
} else {
|
||||
textPacket.setNeedsTranslation(false);
|
||||
|
||||
// This make every message get translated which fixes alot of formatting issues
|
||||
// but also causes players to be able to send translation strings as messages
|
||||
// if thats all they send
|
||||
// textPacket.setMessage(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), session.getClientData().getLanguageCode()));
|
||||
|
||||
textPacket.setMessage(MessageUtils.getBedrockMessage(packet.getMessage()));
|
||||
textPacket.setMessage(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale, false));
|
||||
}
|
||||
|
||||
session.getUpstream().sendPacket(textPacket);
|
||||
|
|
|
@ -42,6 +42,8 @@ public class LocaleUtils {
|
|||
|
||||
private static final Map<String, Asset> ASSET_MAP = new HashMap<>();
|
||||
|
||||
private static final String DEFAULT_LOCALE = (GeyserConnector.getInstance().getConfig().getDefaultLocale() != null ? GeyserConnector.getInstance().getConfig().getDefaultLocale() : "en_us");
|
||||
|
||||
static {
|
||||
// Create the locales folder
|
||||
File localesFolder = new File("locales/");
|
||||
|
@ -49,7 +51,7 @@ public class LocaleUtils {
|
|||
|
||||
// Download the latest asset list and cache it
|
||||
generateAssetCache();
|
||||
downloadAndLoadLocale(GeyserConnector.getInstance().getConfig().getDefaultLocale());
|
||||
downloadAndLoadLocale(DEFAULT_LOCALE);
|
||||
}
|
||||
|
||||
private static void generateAssetCache() {
|
||||
|
@ -169,7 +171,7 @@ public class LocaleUtils {
|
|||
public static String getLocaleString(String messageText, String locale) {
|
||||
Map<String, String> localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(locale.toLowerCase());
|
||||
if (localeStrings == null)
|
||||
localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(GeyserConnector.getInstance().getConfig().getDefaultLocale());
|
||||
localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(DEFAULT_LOCALE);
|
||||
|
||||
return localeStrings.getOrDefault(messageText, messageText);
|
||||
}
|
||||
|
|
|
@ -71,11 +71,7 @@ public class MessageUtils {
|
|||
} else {
|
||||
String builder = getFormat(message.getStyle().getFormats()) +
|
||||
getColor(message.getStyle().getColor());
|
||||
if (locale != null) {
|
||||
builder += getTranslatedBedrockMessage(message, locale);
|
||||
}else {
|
||||
builder += getBedrockMessage(message);
|
||||
}
|
||||
builder += getTranslatedBedrockMessage(message, locale, false);
|
||||
strings.add(builder);
|
||||
}
|
||||
}
|
||||
|
@ -92,7 +88,7 @@ public class MessageUtils {
|
|||
+ "%" + message.getTranslationKey();
|
||||
}
|
||||
|
||||
public static String getTranslatedBedrockMessage(Message message, String locale) {
|
||||
public static String getTranslatedBedrockMessage(Message message, String locale, boolean shouldTranslate) {
|
||||
JsonParser parser = new JsonParser();
|
||||
if (isMessage(message.getText())) {
|
||||
JsonObject object = parser.parse(message.getText()).getAsJsonObject();
|
||||
|
@ -100,7 +96,7 @@ public class MessageUtils {
|
|||
}
|
||||
|
||||
String messageText = message.getText();
|
||||
if (locale != null) {
|
||||
if (locale != null && shouldTranslate) {
|
||||
messageText = LocaleUtils.getLocaleString(messageText, locale);
|
||||
}
|
||||
|
||||
|
@ -109,18 +105,15 @@ public class MessageUtils {
|
|||
builder.append(getFormat(msg.getStyle().getFormats()));
|
||||
builder.append(getColor(msg.getStyle().getColor()));
|
||||
if (!(msg.getText() == null)) {
|
||||
builder.append(getTranslatedBedrockMessage(msg, locale));
|
||||
boolean isTranslationMessage = (msg instanceof TranslationMessage);
|
||||
builder.append(getTranslatedBedrockMessage(msg, locale, isTranslationMessage));
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static String getTranslatedBedrockMessage(Message message) {
|
||||
return getTranslatedBedrockMessage(message, null);
|
||||
}
|
||||
|
||||
public static String getBedrockMessage(Message message) {
|
||||
return getTranslatedBedrockMessage(message);
|
||||
return getTranslatedBedrockMessage(message, null, false);
|
||||
}
|
||||
|
||||
public static String insertParams(String message, List<String> params) {
|
||||
|
|
Loading…
Reference in a new issue