3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

Use the "closest" locale from Velocity

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-06-27 21:15:17 -04:00
Ursprung c6ef84eb7f
Commit 254e8f1a1e
5 geänderte Dateien mit 48 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -55,6 +55,7 @@ import com.velocitypowered.proxy.protocol.util.GameProfileSerializer;
import com.velocitypowered.proxy.scheduler.VelocityScheduler;
import com.velocitypowered.proxy.server.ServerMap;
import com.velocitypowered.proxy.util.AddressUtil;
import com.velocitypowered.proxy.util.ClosestLocaleMatcher;
import com.velocitypowered.proxy.util.EncryptionUtils;
import com.velocitypowered.proxy.util.FileSystemUtils;
import com.velocitypowered.proxy.util.VelocityChannelRegistrar;
@ -272,6 +273,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
translationRegistry.registerAll(locale,
ResourceBundle.getBundle("com/velocitypowered/proxy/l10n/messages",
locale, UTF8ResourceBundleControl.get()), false);
ClosestLocaleMatcher.INSTANCE.registerKnown(locale);
});
} catch (IOException e) {
logger.error("Encountered an I/O error whilst loading translations", e);

Datei anzeigen

@ -68,6 +68,7 @@ import com.velocitypowered.proxy.protocol.packet.title.GenericTitlePacket;
import com.velocitypowered.proxy.server.VelocityRegisteredServer;
import com.velocitypowered.proxy.tablist.VelocityTabList;
import com.velocitypowered.proxy.tablist.VelocityTabListLegacy;
import com.velocitypowered.proxy.util.ClosestLocaleMatcher;
import com.velocitypowered.proxy.util.DurationUtils;
import com.velocitypowered.proxy.util.collect.CappedSet;
import io.netty.buffer.ByteBufUtil;
@ -268,7 +269,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
}
public Component translateMessage(Component message) {
Locale locale = this.settings == null ? Locale.getDefault() : this.settings.getLocale();
Locale locale = ClosestLocaleMatcher.INSTANCE
.lookupClosest(this.settings == null ? Locale.getDefault() : this.settings.getLocale());
return GlobalTranslator.render(message, locale);
}

Datei anzeigen

@ -23,6 +23,7 @@ import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation;
import com.velocitypowered.proxy.protocol.packet.Disconnect;
import com.velocitypowered.proxy.protocol.packet.Handshake;
import com.velocitypowered.proxy.util.ClosestLocaleMatcher;
import java.net.InetSocketAddress;
import java.util.Locale;
import java.util.Optional;
@ -78,7 +79,8 @@ public final class InitialInboundConnection implements InboundConnection,
* @param reason the reason for disconnecting
*/
public void disconnect(Component reason) {
Component translated = GlobalTranslator.render(reason, Locale.getDefault());
Component translated = GlobalTranslator.render(reason, ClosestLocaleMatcher.INSTANCE
.lookupClosest(Locale.getDefault()));
logger.info("{} has disconnected: {}", this,
LegacyComponentSerializer.legacySection().serialize(translated));
@ -90,7 +92,8 @@ public final class InitialInboundConnection implements InboundConnection,
* @param reason the reason for disconnecting
*/
public void disconnectQuietly(Component reason) {
Component translated = GlobalTranslator.render(reason, Locale.getDefault());
Component translated = GlobalTranslator.render(reason, ClosestLocaleMatcher.INSTANCE
.lookupClosest(Locale.getDefault()));
connection.closeWith(Disconnect.create(translated, getProtocolVersion()));
}
}

Datei anzeigen

@ -24,6 +24,7 @@ import com.velocitypowered.api.permission.PermissionFunction;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.ConsoleCommandSource;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.util.ClosestLocaleMatcher;
import java.util.List;
import java.util.Locale;
import net.kyori.adventure.identity.Identity;
@ -59,7 +60,8 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons
@Override
public void sendMessage(@NonNull Identity identity, @NonNull Component message) {
Component translated = GlobalTranslator.render(message, Locale.getDefault());
Component translated = GlobalTranslator.render(message, ClosestLocaleMatcher.INSTANCE
.lookupClosest(Locale.getDefault()));
logger.info(LegacyComponentSerializer.legacySection().serialize(translated));
}

Datei anzeigen

@ -0,0 +1,35 @@
package com.velocitypowered.proxy.util;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ClosestLocaleMatcher {
public static final ClosestLocaleMatcher INSTANCE = new ClosestLocaleMatcher();
private final Map<String, Locale> byLanguage;
private final LoadingCache<Locale, Locale> closest;
private ClosestLocaleMatcher() {
this.byLanguage = new ConcurrentHashMap<>();
this.closest = Caffeine.newBuilder()
.build(sublocale -> {
final String tag = sublocale.getLanguage();
return byLanguage.getOrDefault(tag, sublocale);
});
}
public void registerKnown(final Locale locale) {
if (locale.getLanguage().equals(new Locale("zh").getLanguage())) {
return;
}
this.byLanguage.put(locale.getLanguage(), locale);
}
public Locale lookupClosest(final Locale locale) {
return closest.get(locale);
}
}