Mirror von
https://github.com/GeyserMC/Geyser.git
synchronisiert 2024-09-08 20:43:04 +02: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.
Dieser Commit ist enthalten in:
Ursprung
b65ba2cb52
Commit
e1085270b8
@ -37,14 +37,13 @@ import org.geysermc.connector.network.session.GeyserSession;
|
|||||||
import org.geysermc.connector.utils.LanguageUtils;
|
import org.geysermc.connector.utils.LanguageUtils;
|
||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class MessageTranslator {
|
public class MessageTranslator {
|
||||||
|
|
||||||
// These are used for handling the translations of the messages
|
// 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
|
// Construct our own {@link GsonComponentSerializer} since we need to change a setting
|
||||||
private static final GsonComponentSerializer GSON_SERIALIZER = GsonComponentSerializer.builder()
|
private static final GsonComponentSerializer GSON_SERIALIZER = GsonComponentSerializer.builder()
|
||||||
@ -105,9 +104,8 @@ public class MessageTranslator {
|
|||||||
*/
|
*/
|
||||||
public static String convertMessage(Component message, String locale) {
|
public static String convertMessage(Component message, String locale) {
|
||||||
try {
|
try {
|
||||||
// Get a Locale from the given locale string
|
// Translate any components that require it
|
||||||
Locale localeCode = Locale.forLanguageTag(locale.replace('_', '-'));
|
message = RENDERER.render(message, locale);
|
||||||
message = RENDERER.render(message, localeCode);
|
|
||||||
|
|
||||||
String legacy = LegacyComponentSerializer.legacySection().serialize(message);
|
String legacy = LegacyComponentSerializer.legacySection().serialize(message);
|
||||||
|
|
||||||
|
@ -25,8 +25,7 @@
|
|||||||
|
|
||||||
package org.geysermc.connector.network.translators.chat;
|
package org.geysermc.connector.network.translators.chat;
|
||||||
|
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.text.renderer.TranslatableComponentRenderer;
|
||||||
import net.kyori.adventure.translation.Translator;
|
|
||||||
import org.geysermc.connector.utils.LocaleUtils;
|
import org.geysermc.connector.utils.LocaleUtils;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
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
|
* 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
|
* 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 stringReplacement = Pattern.compile("%s");
|
||||||
private final Pattern positionalStringReplacement = Pattern.compile("%([0-9]+)\\$s");
|
private final Pattern positionalStringReplacement = Pattern.compile("%([0-9]+)\\$s");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nonnull Key name() {
|
public @Nullable MessageFormat translate(@Nonnull String key, @Nonnull String locale) {
|
||||||
return Key.key("geyser", "minecraft_translations");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable MessageFormat translate(@Nonnull String key, @Nonnull Locale locale) {
|
|
||||||
// Get the locale string
|
// Get the locale string
|
||||||
String localeString = LocaleUtils.getLocaleString(key, locale.toString());
|
String localeString = LocaleUtils.getLocaleString(key, locale);
|
||||||
|
|
||||||
// Replace the `%s` with numbered inserts `{0}`
|
// Replace the `%s` with numbered inserts `{0}`
|
||||||
Pattern p = stringReplacement;
|
Pattern p = stringReplacement;
|
||||||
@ -75,6 +69,7 @@ public class MinecraftTranslationRegistry implements Translator {
|
|||||||
m.appendTail(sb);
|
m.appendTail(sb);
|
||||||
|
|
||||||
// replace single quote instances which get lost in MessageFormat otherwise
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren