Skip to content

Commit 3d8e909

Browse files
authored
Custom localization for each player (#537)
1 parent adcf428 commit 3d8e909

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

api/src/main/java/com/velocitypowered/api/proxy/Player.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.velocitypowered.api.util.GameProfile;
2020
import com.velocitypowered.api.util.ModInfo;
2121
import java.util.List;
22+
import java.util.Locale;
2223
import java.util.Optional;
2324
import java.util.UUID;
2425
import java.util.function.UnaryOperator;
@@ -45,6 +46,22 @@ public interface Player extends CommandSource, Identified, InboundConnection,
4546
*/
4647
String getUsername();
4748

49+
/**
50+
* Returns the locale the proxy will use to send messages translated via the Adventure global translator.
51+
* By default, the value of {@link PlayerSettings#getLocale()} is used.
52+
*
53+
* <p>This can be {@code null} when the client has not yet connected to any server.</p>
54+
*
55+
* @return the locale.
56+
*/
57+
@Nullable Locale getEffectiveLocale();
58+
59+
/**
60+
* Change the locale the proxy will be translating its messages to.
61+
*
62+
* @param locale the locale to translate to
63+
*/
64+
void setEffectiveLocale(Locale locale);
4865

4966
/**
5067
* Returns the player's UUID.

proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.velocitypowered.api.util.GameProfile;
4949
import com.velocitypowered.api.util.ModInfo;
5050
import com.velocitypowered.proxy.VelocityServer;
51-
import com.velocitypowered.proxy.config.VelocityConfiguration;
5251
import com.velocitypowered.proxy.connection.MinecraftConnection;
5352
import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation;
5453
import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
@@ -79,7 +78,6 @@
7978
import java.util.Collections;
8079
import java.util.List;
8180
import java.util.Locale;
82-
import java.util.Objects;
8381
import java.util.Optional;
8482
import java.util.Queue;
8583
import java.util.UUID;
@@ -97,8 +95,6 @@
9795
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
9896
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
9997
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
100-
import net.kyori.adventure.title.Title;
101-
import net.kyori.adventure.title.Title.Times;
10298
import net.kyori.adventure.translation.GlobalTranslator;
10399
import org.apache.logging.log4j.LogManager;
104100
import org.apache.logging.log4j.Logger;
@@ -149,6 +145,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
149145
.withStatic(PermissionChecker.POINTER, getPermissionChecker())
150146
.build();
151147
private @Nullable String clientBrand;
148+
private @Nullable Locale effectiveLocale;
152149

153150
ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection,
154151
@Nullable InetSocketAddress virtualHost, boolean onlineMode) {
@@ -178,6 +175,19 @@ public String getUsername() {
178175
return profile.getName();
179176
}
180177

178+
@Override
179+
public Locale getEffectiveLocale() {
180+
if (effectiveLocale == null && settings != null) {
181+
return settings.getLocale();
182+
}
183+
return effectiveLocale;
184+
}
185+
186+
@Override
187+
public void setEffectiveLocale(Locale locale) {
188+
effectiveLocale = locale;
189+
}
190+
181191
@Override
182192
public UUID getUniqueId() {
183193
return profile.getId();
@@ -276,7 +286,7 @@ public ProtocolVersion getProtocolVersion() {
276286
*/
277287
public Component translateMessage(Component message) {
278288
Locale locale = ClosestLocaleMatcher.INSTANCE
279-
.lookupClosest(this.settings == null ? Locale.getDefault() : this.settings.getLocale());
289+
.lookupClosest(getEffectiveLocale() == null ? Locale.getDefault() : getEffectiveLocale());
280290
return GlobalTranslator.render(message, locale);
281291
}
282292

0 commit comments

Comments
 (0)