From bc3fd2e274c9fa9705d230780545eb621aabdc41 Mon Sep 17 00:00:00 2001 From: Sten Laane <21343173+StenAL@users.noreply.github.com> Date: Fri, 20 Sep 2024 08:41:23 +0300 Subject: [PATCH] Client+Server: Use Locale enum in more places --- .../main/java/agolf/lobby/LobbyChatPanel.java | 3 +- .../java/com/aapeli/client/BadWordFilter.java | Bin 9831 -> 9884 bytes .../com/aapeli/client/LocalizationNode.java | 12 ++--- .../java/com/aapeli/client/TextManager.java | 20 ++++---- .../java/com/aapeli/multiuser/ChatBase.java | 4 +- .../java/com/aapeli/multiuser/Languages.java | 14 ++---- .../java/com/aapeli/multiuser/UserList.java | 9 +++- .../org/moparforia/server/game/Player.java | 45 +++++++++--------- .../packethandlers/golf/LanguageHandler.java | 3 +- .../java/org/moparforia/shared/Language.java | 3 +- 10 files changed, 59 insertions(+), 54 deletions(-) diff --git a/client/src/main/java/agolf/lobby/LobbyChatPanel.java b/client/src/main/java/agolf/lobby/LobbyChatPanel.java index c00362e7..ebc9193e 100644 --- a/client/src/main/java/agolf/lobby/LobbyChatPanel.java +++ b/client/src/main/java/agolf/lobby/LobbyChatPanel.java @@ -36,8 +36,7 @@ protected LobbyChatPanel(GameContainer gameContainer, int width, int height, int this.disableChatInput(2); } - this.setOutputToGlobal(Languages.getLanguageIdByString( - gameContainer.params.getChatLocale().toString())); + this.setOutputToGlobal(Languages.getLanguageId(gameContainer.params.getChatLocale())); this.addChatListener(this); } diff --git a/client/src/main/java/com/aapeli/client/BadWordFilter.java b/client/src/main/java/com/aapeli/client/BadWordFilter.java index bd164f1463135447be459941ee92a5d8941f31af..a9e663b5214df2e890c1ab04fee7616207098a73 100644 GIT binary patch delta 99 zcmaFvGskyAtZHU%L4Hw*LVi)YUT%ItVo_RtQD&lEaYkZMYKopuVqSV_VtVSvXcczd p{N%)(R1HnN^wbiVIFOfGSelqqtN|0zbMy4`^9=UbJdgdP8~|o`B%}ZU delta 55 zcmbQ^``l+j?8X))cICvp^wPxiR1HnN)WXuloMO-Py!@h6=fvVv4W+b9B~49*>dAr} LGMhiMuapA-M$;3q diff --git a/client/src/main/java/com/aapeli/client/LocalizationNode.java b/client/src/main/java/com/aapeli/client/LocalizationNode.java index 90e1d9a6..0d67800d 100644 --- a/client/src/main/java/com/aapeli/client/LocalizationNode.java +++ b/client/src/main/java/com/aapeli/client/LocalizationNode.java @@ -2,18 +2,18 @@ import com.aapeli.tools.Tools; import com.aapeli.tools.XmlUnit; +import org.moparforia.shared.Language; +import org.moparforia.shared.Locale; class LocalizationNode { - private String language; + private Language language; private String singular; private String plural; private String zero; - private final TextManager textManager; - protected LocalizationNode(TextManager textManager, String language, XmlUnit unit, boolean reversed) { - this.textManager = textManager; - this.language = language.substring(0, 2).toLowerCase(); + protected LocalizationNode(Locale locale, XmlUnit unit, boolean reversed) { + this.language = locale.getLanguage(); this.singular = unit.getChildValue("singular"); this.plural = unit.getChildValue("plural"); this.zero = unit.getChildValue("zero"); @@ -30,7 +30,7 @@ protected String getLocalization(int quantity) { return this.zero; } - if (this.plural != null && !this.language.equals("fr")) { + if (this.plural != null && !this.language.equals(Language.FRENCH)) { return this.plural; } } else if ((quantity < 0 || quantity > 1) && this.plural != null) { diff --git a/client/src/main/java/com/aapeli/client/TextManager.java b/client/src/main/java/com/aapeli/client/TextManager.java index 8d374d65..a3ab33ed 100644 --- a/client/src/main/java/com/aapeli/client/TextManager.java +++ b/client/src/main/java/com/aapeli/client/TextManager.java @@ -7,12 +7,13 @@ import java.util.Calendar; import java.util.Date; import java.util.Hashtable; +import org.moparforia.shared.Locale; public final class TextManager implements Runnable { private Parameters parameters; private Thread textLoaderThread; - private String language; + private Locale locale; private Hashtable gameTable; private Hashtable sharedTable; private String errorMessage; @@ -29,7 +30,7 @@ public TextManager(Parameters parameters, boolean debug) { public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boolean debug) { this(debug); this.parameters = parameters; - this.language = parameters.getTranslationLocale().toString(); + this.locale = parameters.getTranslationLocale(); if (loadTextsInSeparateThread) { this.textLoaderThread = new Thread(this); @@ -307,13 +308,13 @@ public void destroy() { } this.parameters = null; - this.language = null; + this.locale = null; this.errorMessage = null; } } - protected String getLanguage() { - return this.language; + protected Locale getLocale() { + return this.locale; } private String getGame(String key, String[] arguments) { @@ -525,17 +526,17 @@ protected String getSharedString(String key, int quantity) { } private void loadTexts(Applet applet) { - this.loadLanguageFiles(applet); + this.loadLocaleFiles(applet); } - private void loadLanguageFiles(Applet applet) { + private void loadLocaleFiles(Applet applet) { String codeBasePath = applet.getCodeBase().toString(); if (codeBasePath.endsWith("/")) { codeBasePath = codeBasePath.substring(0, codeBasePath.length() - 1); } int slashLocation = codeBasePath.lastIndexOf('/'); - String languageDirectoryPath = codeBasePath.substring(0, slashLocation + 1) + "l10n/" + this.language + "/"; + String languageDirectoryPath = codeBasePath.substring(0, slashLocation + 1) + "l10n/" + this.locale + "/"; String gameFilename = codeBasePath.substring(slashLocation + 1); this.gameTable = this.readTable(languageDirectoryPath + gameFilename + ".xml"); @@ -556,8 +557,7 @@ private Hashtable readTable(String fileUrl) { for (XmlUnit child : children) { table.put( child.getAttribute("key").toLowerCase(), - new LocalizationNode( - this, this.language, child, Tools.getBoolean(child.getAttribute("reverse")))); + new LocalizationNode(this.locale, child, Tools.getBoolean(child.getAttribute("reverse")))); } return table; diff --git a/client/src/main/java/com/aapeli/multiuser/ChatBase.java b/client/src/main/java/com/aapeli/multiuser/ChatBase.java index 531bf811..95c76614 100644 --- a/client/src/main/java/com/aapeli/multiuser/ChatBase.java +++ b/client/src/main/java/com/aapeli/multiuser/ChatBase.java @@ -452,13 +452,13 @@ public boolean useRoundButtons() { } } - public void setOutputToGlobal(int var1) { + public void setOutputToGlobal(int languageId) { Object var2 = this.synchronizedObject; synchronized (this.synchronizedObject) { if (this.gui_globaloutput == null) { Point var3 = this.chatTextArea.getLocation(); this.remove(this.chatTextArea); - this.gui_globaloutput = new GlobalTextArea(this, this.chatTextArea, var1); + this.gui_globaloutput = new GlobalTextArea(this, this.chatTextArea, languageId); this.gui_globaloutput.setLocation(var3.x, var3.y); this.add(this.gui_globaloutput); } diff --git a/client/src/main/java/com/aapeli/multiuser/Languages.java b/client/src/main/java/com/aapeli/multiuser/Languages.java index 27943ca0..4dbaecbb 100644 --- a/client/src/main/java/com/aapeli/multiuser/Languages.java +++ b/client/src/main/java/com/aapeli/multiuser/Languages.java @@ -3,6 +3,7 @@ import com.aapeli.client.ImageManager; import com.aapeli.client.TextManager; import java.awt.Image; +import org.moparforia.shared.Locale; public final class Languages { @@ -78,23 +79,18 @@ public Languages(TextManager textManager, ImageManager imageManager) { this.imageManager = imageManager; } - public static int getLanguageIdByString(String locale) { + public static int getLanguageId(Locale locale) { if (locale == null) { return LANGUAGE_UNKNOWN; } else { - int i = locale.indexOf('_'); - if (i > 0) { - locale = locale.substring(0, i); - } - - locale = locale.toLowerCase(); + String language = locale.getLanguage().toString(); for (int j = 1; j < languageData.length; ++j) { - if (locale.equals(languageData[j][0])) { + if (language.equals(languageData[j][0])) { return j; } - if (languageData[j][1] != null && locale.equals(languageData[j][1])) { + if (languageData[j][1] != null && language.equals(languageData[j][1])) { return j; } } diff --git a/client/src/main/java/com/aapeli/multiuser/UserList.java b/client/src/main/java/com/aapeli/multiuser/UserList.java index f9548828..016320b1 100644 --- a/client/src/main/java/com/aapeli/multiuser/UserList.java +++ b/client/src/main/java/com/aapeli/multiuser/UserList.java @@ -30,6 +30,7 @@ import java.util.Hashtable; import java.util.List; import java.util.StringTokenizer; +import org.moparforia.shared.Locale; public class UserList extends IPanel implements ComponentListener, ItemListener, ActionListener { @@ -466,7 +467,13 @@ public User addUser(String userData, boolean userIsLocal, int color) { boolean isNotAcceptingChallenges = elevation.indexOf('n') >= 0; User user = new User(username, userIsLocal, isRegistered, isVip, isSheriff, rating); user.setIsNotAcceptingChallenges(isNotAcceptingChallenges); - int language = Languages.getLanguageIdByString(locale); + + int language; + if (!locale.equals("-")) { + language = Languages.getLanguageId(Locale.fromString(locale)); + } else { + language = Languages.LANGUAGE_UNKNOWN; + } user.setLanguage(language); user.setLanguageFlag(this.languages.getFlag(language)); if (color >= 0) { diff --git a/server/src/main/java/org/moparforia/server/game/Player.java b/server/src/main/java/org/moparforia/server/game/Player.java index ea4dddfc..cea45a72 100644 --- a/server/src/main/java/org/moparforia/server/game/Player.java +++ b/server/src/main/java/org/moparforia/server/game/Player.java @@ -2,6 +2,7 @@ import io.netty.channel.Channel; import io.netty.util.AttributeKey; +import org.moparforia.shared.Locale; import org.moparforia.shared.Tools; public class Player { @@ -15,7 +16,7 @@ public class Player { private final int id; private String nick; - private String locale; + private Locale locale; private String profileUrl; private String avatarUrl; private String clan; @@ -36,27 +37,27 @@ public class Player { public Player(Channel channel, int id) { this.channel = channel; this.id = id; - ranking = 0; + this.ranking = 0; resetVals(); } public void resetVals() { - nick = "-"; - locale = "-"; - profileUrl = "-"; - avatarUrl = "-"; - clan = "-"; - accessLevel = ACCESSLEVEL_NORMAL; - ranking = 0; - emailVerified = false; - registered = false; - vip = false; - sheriff = accessLevel == ACCESSLEVEL_SHERIFF || accessLevel == ACCESSLEVEL_ADMIN; - notAcceptingChallenges = false; - isChatHidden = false; - hasSkipped = false; - lobby = null; - game = null; + this.nick = "-"; + this.locale = null; + this.profileUrl = "-"; + this.avatarUrl = "-"; + this.clan = "-"; + this.accessLevel = ACCESSLEVEL_NORMAL; + this.ranking = 0; + this.emailVerified = false; + this.registered = false; + this.vip = false; + this.sheriff = accessLevel == ACCESSLEVEL_SHERIFF || accessLevel == ACCESSLEVEL_ADMIN; + this.notAcceptingChallenges = false; + this.isChatHidden = false; + this.hasSkipped = false; + this.lobby = null; + this.game = null; } public Channel getChannel() { @@ -83,11 +84,11 @@ public String getNick() { return nick; } - public String getLocale() { + public Locale getLocale() { return locale; } - public void setLocale(String locale) { + public void setLocale(Locale locale) { this.locale = locale; } @@ -210,7 +211,7 @@ public void setGameType(GameType gameType) { public boolean equals(Object o) { if (o == null || !(o instanceof Player)) return false; Player p = (Player) o; - return nick.equals(p.nick) && ranking == p.ranking && locale.equals(p.locale); + return nick.equals(p.nick) && ranking == p.ranking && locale == p.locale; } public String toString() { @@ -220,7 +221,7 @@ public String toString() { "3:" + (nick != null ? nick : ""), tmp.equals("") ? "w" : tmp, ranking, - locale != null ? locale : "", + locale != null ? locale : "-", profileUrl != null ? profileUrl : "", avatarUrl != null ? avatarUrl : ""); } diff --git a/server/src/main/java/org/moparforia/server/net/packethandlers/golf/LanguageHandler.java b/server/src/main/java/org/moparforia/server/net/packethandlers/golf/LanguageHandler.java index e79ab56b..d4d7b40c 100644 --- a/server/src/main/java/org/moparforia/server/net/packethandlers/golf/LanguageHandler.java +++ b/server/src/main/java/org/moparforia/server/net/packethandlers/golf/LanguageHandler.java @@ -7,6 +7,7 @@ import org.moparforia.server.net.Packet; import org.moparforia.server.net.PacketHandler; import org.moparforia.server.net.PacketType; +import org.moparforia.shared.Locale; public class LanguageHandler implements PacketHandler { @@ -23,7 +24,7 @@ public Pattern getPattern() { @Override public boolean handle(Server server, Packet packet, Matcher message) { Player player = packet.getChannel().attr(Player.PLAYER_ATTRIBUTE_KEY).get(); - player.setLocale(message.group(1)); // todo: check if we axly support this locale + player.setLocale(Locale.fromString(message.group(1))); return true; } } diff --git a/shared/src/main/java/org/moparforia/shared/Language.java b/shared/src/main/java/org/moparforia/shared/Language.java index da8432d9..2d810ea7 100644 --- a/shared/src/main/java/org/moparforia/shared/Language.java +++ b/shared/src/main/java/org/moparforia/shared/Language.java @@ -3,7 +3,8 @@ public enum Language { ENGLISH("en"), FINNISH("fi"), - SWEDISH("sv"); + SWEDISH("sv"), + FRENCH("fr"); private final String languageCode;