mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Don't use Locale class in MessageTranslator
The Minecraft Java client doesn't appear to use any specific locale function in its translating. Just passing a string saves computational time in parsing a locale that will be the same result every time. Should this be invalid behavior, we should instead cache the Locale class for a player so it doesn't have to be repeatedly calculated.
This commit is contained in:
parent
b65ba2cb52
commit
e1085270b8
2 changed files with 10 additions and 17 deletions
|
@ -37,14 +37,13 @@ import org.geysermc.connector.network.session.GeyserSession;
|
|||
import org.geysermc.connector.utils.LanguageUtils;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MessageTranslator {
|
||||
|
||||
// These are used for handling the translations of the messages
|
||||
private static final TranslatableComponentRenderer<Locale> RENDERER = TranslatableComponentRenderer.usingTranslationSource(new MinecraftTranslationRegistry());
|
||||
// Custom instead of TranslatableComponentRenderer#usingTranslationSource so we don't need to worry about finding a Locale class
|
||||
private static final TranslatableComponentRenderer<String> RENDERER = new MinecraftTranslationRegistry();
|
||||
|
||||
// Construct our own {@link GsonComponentSerializer} since we need to change a setting
|
||||
private static final GsonComponentSerializer GSON_SERIALIZER = GsonComponentSerializer.builder()
|
||||
|
@ -105,9 +104,8 @@ public class MessageTranslator {
|
|||
*/
|
||||
public static String convertMessage(Component message, String locale) {
|
||||
try {
|
||||
// Get a Locale from the given locale string
|
||||
Locale localeCode = Locale.forLanguageTag(locale.replace('_', '-'));
|
||||
message = RENDERER.render(message, localeCode);
|
||||
// Translate any components that require it
|
||||
message = RENDERER.render(message, locale);
|
||||
|
||||
String legacy = LegacyComponentSerializer.legacySection().serialize(message);
|
||||
|
||||
|
|
|
@ -25,8 +25,7 @@
|
|||
|
||||
package org.geysermc.connector.network.translators.chat;
|
||||
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.translation.Translator;
|
||||
import net.kyori.adventure.text.renderer.TranslatableComponentRenderer;
|
||||
import org.geysermc.connector.utils.LocaleUtils;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
@ -40,19 +39,14 @@ import java.util.regex.Pattern;
|
|||
* This class is used for mapping a translation key with the already loaded Java locale data
|
||||
* Used in MessageTranslator.java as part of the KyoriPowered/Adventure library
|
||||
*/
|
||||
public class MinecraftTranslationRegistry implements Translator {
|
||||
public class MinecraftTranslationRegistry extends TranslatableComponentRenderer<String> {
|
||||
private final Pattern stringReplacement = Pattern.compile("%s");
|
||||
private final Pattern positionalStringReplacement = Pattern.compile("%([0-9]+)\\$s");
|
||||
|
||||
@Override
|
||||
public @Nonnull Key name() {
|
||||
return Key.key("geyser", "minecraft_translations");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable MessageFormat translate(@Nonnull String key, @Nonnull Locale locale) {
|
||||
public @Nullable MessageFormat translate(@Nonnull String key, @Nonnull String locale) {
|
||||
// Get the locale string
|
||||
String localeString = LocaleUtils.getLocaleString(key, locale.toString());
|
||||
String localeString = LocaleUtils.getLocaleString(key, locale);
|
||||
|
||||
// Replace the `%s` with numbered inserts `{0}`
|
||||
Pattern p = stringReplacement;
|
||||
|
@ -75,6 +69,7 @@ public class MinecraftTranslationRegistry implements Translator {
|
|||
m.appendTail(sb);
|
||||
|
||||
// replace single quote instances which get lost in MessageFormat otherwise
|
||||
return new MessageFormat(sb.toString().replace("'", "''"), locale);
|
||||
// Locale shouldn't need to be specific - dates for example will not be handled
|
||||
return new MessageFormat(sb.toString().replace("'", "''"), Locale.ROOT);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue