From 6d5054895f31b590da601d563bd9794f99d3d900 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 1/8] Client: Rename locale and language parameters These were previously swapped. A locale is a language + region, e.g. en_us or fi_fi whereas a language is just the first part of a locale. --- .../src/main/java/agolf/GolfConnection.java | 2 +- .../main/java/agolf/lobby/LobbyChatPanel.java | 2 +- .../java/com/aapeli/client/Parameters.java | 82 +++++++++---------- .../java/com/aapeli/client/TextManager.java | 4 +- .../main/java/org/moparforia/client/Game.java | 7 +- 5 files changed, 45 insertions(+), 52 deletions(-) diff --git a/client/src/main/java/agolf/GolfConnection.java b/client/src/main/java/agolf/GolfConnection.java index f16691fa..c42ca526 100644 --- a/client/src/main/java/agolf/GolfConnection.java +++ b/client/src/main/java/agolf/GolfConnection.java @@ -159,7 +159,7 @@ private void handlePacket(String cmd) { this.socketConnection.closeConnection(); } else if (args[0].equals("versok")) { - this.writeData("language\t" + this.gameContainer.params.getChatLang()); + this.writeData("language\t" + this.gameContainer.params.getChatLocale()); String var4 = this.gameContainer.params.getSessionLocale(); if (var4 != null) { this.writeData("sessionlocale\t" + var4); diff --git a/client/src/main/java/agolf/lobby/LobbyChatPanel.java b/client/src/main/java/agolf/lobby/LobbyChatPanel.java index 5384ee9f..3eabf599 100644 --- a/client/src/main/java/agolf/lobby/LobbyChatPanel.java +++ b/client/src/main/java/agolf/lobby/LobbyChatPanel.java @@ -36,7 +36,7 @@ protected LobbyChatPanel(GameContainer gameContainer, int width, int height, int this.disableChatInput(2); } - this.setOutputToGlobal(Languages.getLanguageIdByString(gameContainer.params.getChatLang())); + this.setOutputToGlobal(Languages.getLanguageIdByString(gameContainer.params.getChatLocale())); this.addChatListener(this); } diff --git a/client/src/main/java/com/aapeli/client/Parameters.java b/client/src/main/java/com/aapeli/client/Parameters.java index 49bce241..3e76d145 100644 --- a/client/src/main/java/com/aapeli/client/Parameters.java +++ b/client/src/main/java/com/aapeli/client/Parameters.java @@ -21,9 +21,9 @@ public final class Parameters { private String codeBaseHost; private String documentBaseHost; private String serverIp; - private String locale; - private String translationLanguage; - private String chatLang; + private String language; + private String translationLocale; + private String chatLocale; private String siteName; private String username; private String sessionLocale; @@ -112,24 +112,24 @@ public int getServerPort() { return this.serverPort; } - public String getLocale() { - return this.locale; + public String language() { + return this.language; } - public String getTranslationLang() { - return this.translationLanguage; + public String getTranslationLocale() { + return this.translationLocale; } public String getUsername() { return this.username; } - public String getChatLang() { - return this.chatLang != null ? this.chatLang : this.translationLanguage; + public String getChatLocale() { + return this.chatLocale != null ? this.chatLocale : this.translationLocale; } - public String getLang() { - return this.getChatLang(); + public String getLocale() { + return this.getChatLocale(); } public String getSiteName() { @@ -392,9 +392,9 @@ public AApplet getAApplet() { public void destroy() { this.serverIp = null; - this.locale = null; - this.translationLanguage = null; - this.chatLang = null; + this.language = null; + this.translationLocale = null; + this.chatLocale = null; this.siteName = null; this.sessionLocale = null; this.session = null; @@ -449,9 +449,9 @@ protected String getSubgame() { private void init() { this.serverIp = this.getParamServer(); this.serverPort = this.getParamPort(); - this.locale = this.getParamLocale(); - this.translationLanguage = this.getParamLanguage(); - this.chatLang = this.getParamChatLanguage(); + this.language = this.getParamLanguage(); + this.translationLocale = this.getParamLocale(); + this.chatLocale = this.getParamChatLocale(); this.siteName = this.getParamSiteName(); this.sessionLocale = this.getParameter("sessionlocale"); this.session = this.getParameter("session"); @@ -514,12 +514,12 @@ private int getParamPort() { } } - private String getParamLocale() { - String locale; + private String getParamLanguage() { + String language; try { - locale = this.getParameter("locale"); - if (locale != null) { - return locale; + language = this.getParameter("language"); + if (language != null) { + return language; } } catch (Exception e) { } @@ -531,9 +531,9 @@ private String getParamLocale() { } else { if (this.codeBaseHost.endsWith(".playforia.com")) { try { - locale = this.codeBaseHost.substring(0, this.codeBaseHost.indexOf(46)); - if (locale.length() > 0 && !locale.equals("www")) { - return locale; + language = this.codeBaseHost.substring(0, this.codeBaseHost.indexOf(46)); + if (language.length() > 0 && !language.equals("www")) { + return language; } } catch (Exception e) { } @@ -541,9 +541,9 @@ private String getParamLocale() { if (this.codeBaseHost.contains("playray")) { try { - locale = this.codeBaseHost.substring(this.codeBaseHost.lastIndexOf(46) + 1); - if (locale.length() > 0) { - return locale; + language = this.codeBaseHost.substring(this.codeBaseHost.lastIndexOf(46) + 1); + if (language.length() > 0) { + return language; } } catch (Exception e) { } @@ -553,33 +553,29 @@ private String getParamLocale() { } } - private String getParamLanguage() { + private String getParamLocale() { try { - String language = this.getParameter("lang"); - if (language != null) { - return language; + String locale = this.getParameter("locale"); + if (locale != null) { + return locale; } - language = this.getParameter("language"); - if (language != null) { - return language; - } } catch (Exception e) { } return null; } - private String getParamChatLanguage() { + private String getParamChatLocale() { try { - String chatLanguage = this.getParameter("chatlang"); - if (chatLanguage != null) { - return chatLanguage; + String chatLocale = this.getParameter("chatlocale"); + if (chatLocale != null) { + return chatLocale; } - chatLanguage = this.getParameter("serverlang"); - if (chatLanguage != null) { - return chatLanguage; + chatLocale = this.getParameter("serverlocale"); + if (chatLocale != null) { + return chatLocale; } } catch (Exception e) { } diff --git a/client/src/main/java/com/aapeli/client/TextManager.java b/client/src/main/java/com/aapeli/client/TextManager.java index 56d1aa52..c56a9eb5 100644 --- a/client/src/main/java/com/aapeli/client/TextManager.java +++ b/client/src/main/java/com/aapeli/client/TextManager.java @@ -48,12 +48,12 @@ public TextManager(Parameters parameters, boolean debug) { public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boolean debug) { this(debug); this.parameters = parameters; - String language = parameters.getTranslationLang(); + String language = parameters.getTranslationLocale(); if (language != null) { this.language = language; this.useLanguageFiles = true; } else { - this.language = parameters.getLocale(); + this.language = parameters.language(); this.useLanguageFiles = false; } diff --git a/client/src/main/java/org/moparforia/client/Game.java b/client/src/main/java/org/moparforia/client/Game.java index 5415fa7a..febfec2a 100644 --- a/client/src/main/java/org/moparforia/client/Game.java +++ b/client/src/main/java/org/moparforia/client/Game.java @@ -52,11 +52,8 @@ public Stub(String server, String lang, String username, int port, boolean verbo params.put("server", server + ":" + port); - // params.put("locale", "en"); - // params.put("lang", en_US); - - params.put("locale", lang.substring(0, 2)); // use first part of en_US, fi_FI or sv_SE - params.put("lang", lang); + params.put("language", lang.substring(0, 2)); // use first part of en_US, fi_FI or sv_SE + params.put("locale", lang); params.put("sitename", "playray"); params.put("quitpage", "http://www.playforia.com/games/"); params.put("regremindshowtime", "3,8,15,25,50,100,1000"); From ed428f729f56f0fb3fbe8132e23eee24aba51df5 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 2/8] Client: Rename Language enum to Locale This is what it actually represents. --- .../main/java/org/moparforia/client/Game.java | 17 ++++++++++++----- .../java/org/moparforia/client/Launcher.java | 18 +++++++++--------- .../org/moparforia/client/LauncherCLITest.java | 6 +++--- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/client/src/main/java/org/moparforia/client/Game.java b/client/src/main/java/org/moparforia/client/Game.java index febfec2a..9b1741c3 100644 --- a/client/src/main/java/org/moparforia/client/Game.java +++ b/client/src/main/java/org/moparforia/client/Game.java @@ -14,10 +14,16 @@ public class Game { private static final int HEIGHT = 525; public Game( - JFrame frame, String server, int port, String lang, String username, boolean verbose, boolean norandom) { + JFrame frame, + String server, + int port, + Launcher.Locale locale, + String username, + boolean verbose, + boolean norandom) { Applet game = new AGolf(); - game.setStub(new Stub(server, lang, username, port, verbose, norandom)); + game.setStub(new Stub(server, locale, username, port, verbose, norandom)); game.setSize(WIDTH, HEIGHT); game.init(); game.start(); @@ -32,7 +38,8 @@ class Stub implements AppletStub { private final Map params; private String server; - public Stub(String server, String lang, String username, int port, boolean verbose, boolean norandom) { + public Stub( + String server, Launcher.Locale locale, String username, int port, boolean verbose, boolean norandom) { if (server.indexOf(':') == -1) { // is ipv4 this.server = server; } else { // is ipv6 @@ -52,8 +59,8 @@ public Stub(String server, String lang, String username, int port, boolean verbo params.put("server", server + ":" + port); - params.put("language", lang.substring(0, 2)); // use first part of en_US, fi_FI or sv_SE - params.put("locale", lang); + params.put("language", locale.toString().substring(0, 2)); // use first part of en_US, fi_FI or sv_SE + params.put("locale", locale.toString()); params.put("sitename", "playray"); params.put("quitpage", "http://www.playforia.com/games/"); params.put("regremindshowtime", "3,8,15,25,50,100,1000"); diff --git a/client/src/main/java/org/moparforia/client/Launcher.java b/client/src/main/java/org/moparforia/client/Launcher.java index fb10b642..03a87f67 100644 --- a/client/src/main/java/org/moparforia/client/Launcher.java +++ b/client/src/main/java/org/moparforia/client/Launcher.java @@ -45,7 +45,7 @@ public class Launcher implements Callable { names = {"--lang", "-l"}, description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}", defaultValue = "en_us") - private Language lang; + private Locale locale; @CommandLine.Option( names = {"--username", "-u"}, @@ -149,7 +149,7 @@ public Integer call() throws Exception { } } - launchGame(frame, hostname, port, lang, username, verbose, norandom); + launchGame(frame, hostname, port, locale, username, verbose, norandom); return null; } @@ -165,11 +165,11 @@ public Game launchGame( JFrame frame, String hostname, int port, - Language lang, + Locale locale, String username, boolean verbose, boolean norandom) { - return new Game(frame, hostname, port, lang.toString(), username, verbose, norandom); + return new Game(frame, hostname, port, locale, username, verbose, norandom); } public Image loadIcon() throws IOException { @@ -184,20 +184,20 @@ public void setPort(int port) { this.port = port; } - enum Language { + public enum Locale { EN_US("en_US"), FI_FI("fi_FI"), SV_SE("sv_SE"); - private final String custom_name; + private final String name; - Language(String name) { - this.custom_name = name; + Locale(String name) { + this.name = name; } @Override public String toString() { - return this.custom_name; + return this.name; } } } diff --git a/client/src/test/java/org/moparforia/client/LauncherCLITest.java b/client/src/test/java/org/moparforia/client/LauncherCLITest.java index 7215ef72..d9176250 100644 --- a/client/src/test/java/org/moparforia/client/LauncherCLITest.java +++ b/client/src/test/java/org/moparforia/client/LauncherCLITest.java @@ -94,7 +94,7 @@ void testValidLang() { any(), eq(Launcher.DEFAULT_SERVER), eq(Launcher.DEFAULT_PORT), - eq(Launcher.Language.EN_US), + eq(Launcher.Locale.EN_US), any(), anyBoolean(), anyBoolean()); @@ -105,7 +105,7 @@ void testValidLang() { any(), eq(Launcher.DEFAULT_SERVER), eq(Launcher.DEFAULT_PORT), - eq(Launcher.Language.FI_FI), + eq(Launcher.Locale.FI_FI), any(), anyBoolean(), anyBoolean()); @@ -182,7 +182,7 @@ void testDefaultValues() { any(), eq(Launcher.DEFAULT_SERVER), eq(Launcher.DEFAULT_PORT), - eq(Launcher.Language.EN_US), + eq(Launcher.Locale.EN_US), eq(null), eq(false), eq(false)); From e0e7590016ccb4f2f07d177a09a5e9a1d983aa9a 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 3/8] Client: Add and use Language enum This complements the Locale enum and helps avoid string parsing each time the language is desired. --- .../main/java/org/moparforia/client/Game.java | 2 +- .../java/org/moparforia/client/Launcher.java | 37 +++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/client/src/main/java/org/moparforia/client/Game.java b/client/src/main/java/org/moparforia/client/Game.java index 9b1741c3..7525eb14 100644 --- a/client/src/main/java/org/moparforia/client/Game.java +++ b/client/src/main/java/org/moparforia/client/Game.java @@ -59,7 +59,7 @@ public Stub( params.put("server", server + ":" + port); - params.put("language", locale.toString().substring(0, 2)); // use first part of en_US, fi_FI or sv_SE + params.put("language", locale.getLanguage().toString()); params.put("locale", locale.toString()); params.put("sitename", "playray"); params.put("quitpage", "http://www.playforia.com/games/"); diff --git a/client/src/main/java/org/moparforia/client/Launcher.java b/client/src/main/java/org/moparforia/client/Launcher.java index 03a87f67..fbdea5c9 100644 --- a/client/src/main/java/org/moparforia/client/Launcher.java +++ b/client/src/main/java/org/moparforia/client/Launcher.java @@ -185,19 +185,42 @@ public void setPort(int port) { } public enum Locale { - EN_US("en_US"), - FI_FI("fi_FI"), - SV_SE("sv_SE"); + EN_US("en_US", Language.ENGLISH), + FI_FI("fi_FI", Language.FINNISH), + SV_SE("sv_SE", Language.SWEDISH); - private final String name; + private final String localeCode; + private final Language language; - Locale(String name) { - this.name = name; + Locale(String localeCode, Language language) { + this.localeCode = localeCode; + this.language = language; + } + + public Language getLanguage() { + return this.language; + } + + @Override + public String toString() { + return this.localeCode; + } + } + + public enum Language { + ENGLISH("en"), + FINNISH("fi"), + SWEDISH("sv"); + + private final String languageCode; + + Language(String languageCode) { + this.languageCode = languageCode; } @Override public String toString() { - return this.name; + return this.languageCode; } } } From d7a8bd573cc5eafc056f2b471dac5580a8104ac0 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 4/8] Client+Shared: Move Language and Locale enums to shared package These will be used in both the server and client so they need to be moved to a place where they're available in both. --- .../main/java/org/moparforia/client/Game.java | 12 ++---- .../java/org/moparforia/client/Launcher.java | 41 +------------------ .../moparforia/client/LauncherCLITest.java | 7 ++-- .../java/org/moparforia/shared/Language.java | 18 ++++++++ .../java/org/moparforia/shared/Locale.java | 24 +++++++++++ 5 files changed, 50 insertions(+), 52 deletions(-) create mode 100644 shared/src/main/java/org/moparforia/shared/Language.java create mode 100644 shared/src/main/java/org/moparforia/shared/Locale.java diff --git a/client/src/main/java/org/moparforia/client/Game.java b/client/src/main/java/org/moparforia/client/Game.java index 7525eb14..d1d9b2ac 100644 --- a/client/src/main/java/org/moparforia/client/Game.java +++ b/client/src/main/java/org/moparforia/client/Game.java @@ -8,19 +8,14 @@ import java.util.HashMap; import java.util.Map; import javax.swing.JFrame; +import org.moparforia.shared.Locale; public class Game { private static final int WIDTH = 735; private static final int HEIGHT = 525; public Game( - JFrame frame, - String server, - int port, - Launcher.Locale locale, - String username, - boolean verbose, - boolean norandom) { + JFrame frame, String server, int port, Locale locale, String username, boolean verbose, boolean norandom) { Applet game = new AGolf(); game.setStub(new Stub(server, locale, username, port, verbose, norandom)); @@ -38,8 +33,7 @@ class Stub implements AppletStub { private final Map params; private String server; - public Stub( - String server, Launcher.Locale locale, String username, int port, boolean verbose, boolean norandom) { + public Stub(String server, Locale locale, String username, int port, boolean verbose, boolean norandom) { if (server.indexOf(':') == -1) { // is ipv4 this.server = server; } else { // is ipv6 diff --git a/client/src/main/java/org/moparforia/client/Launcher.java b/client/src/main/java/org/moparforia/client/Launcher.java index fbdea5c9..ec07a0e4 100644 --- a/client/src/main/java/org/moparforia/client/Launcher.java +++ b/client/src/main/java/org/moparforia/client/Launcher.java @@ -15,6 +15,7 @@ import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; +import org.moparforia.shared.Locale; import org.moparforia.shared.ManifestVersionProvider; import picocli.CommandLine; @@ -183,44 +184,4 @@ public void setHostname(String hostname) { public void setPort(int port) { this.port = port; } - - public enum Locale { - EN_US("en_US", Language.ENGLISH), - FI_FI("fi_FI", Language.FINNISH), - SV_SE("sv_SE", Language.SWEDISH); - - private final String localeCode; - private final Language language; - - Locale(String localeCode, Language language) { - this.localeCode = localeCode; - this.language = language; - } - - public Language getLanguage() { - return this.language; - } - - @Override - public String toString() { - return this.localeCode; - } - } - - public enum Language { - ENGLISH("en"), - FINNISH("fi"), - SWEDISH("sv"); - - private final String languageCode; - - Language(String languageCode) { - this.languageCode = languageCode; - } - - @Override - public String toString() { - return this.languageCode; - } - } } diff --git a/client/src/test/java/org/moparforia/client/LauncherCLITest.java b/client/src/test/java/org/moparforia/client/LauncherCLITest.java index d9176250..206227d0 100644 --- a/client/src/test/java/org/moparforia/client/LauncherCLITest.java +++ b/client/src/test/java/org/moparforia/client/LauncherCLITest.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.quality.Strictness; +import org.moparforia.shared.Locale; import picocli.CommandLine; /** @@ -94,7 +95,7 @@ void testValidLang() { any(), eq(Launcher.DEFAULT_SERVER), eq(Launcher.DEFAULT_PORT), - eq(Launcher.Locale.EN_US), + eq(Locale.EN_US), any(), anyBoolean(), anyBoolean()); @@ -105,7 +106,7 @@ void testValidLang() { any(), eq(Launcher.DEFAULT_SERVER), eq(Launcher.DEFAULT_PORT), - eq(Launcher.Locale.FI_FI), + eq(Locale.FI_FI), any(), anyBoolean(), anyBoolean()); @@ -182,7 +183,7 @@ void testDefaultValues() { any(), eq(Launcher.DEFAULT_SERVER), eq(Launcher.DEFAULT_PORT), - eq(Launcher.Locale.EN_US), + eq(Locale.EN_US), eq(null), eq(false), eq(false)); diff --git a/shared/src/main/java/org/moparforia/shared/Language.java b/shared/src/main/java/org/moparforia/shared/Language.java new file mode 100644 index 00000000..f82a012d --- /dev/null +++ b/shared/src/main/java/org/moparforia/shared/Language.java @@ -0,0 +1,18 @@ +package org.moparforia.shared; + +public enum Language { + ENGLISH("en"), + FINNISH("fi"), + SWEDISH("sv"); + + private final String languageCode; + + Language(String languageCode) { + this.languageCode = languageCode; + } + + @Override + public String toString() { + return this.languageCode; + } +} diff --git a/shared/src/main/java/org/moparforia/shared/Locale.java b/shared/src/main/java/org/moparforia/shared/Locale.java new file mode 100644 index 00000000..eafc7659 --- /dev/null +++ b/shared/src/main/java/org/moparforia/shared/Locale.java @@ -0,0 +1,24 @@ +package org.moparforia.shared; + +public enum Locale { + EN_US("en_US", Language.ENGLISH), + FI_FI("fi_FI", Language.FINNISH), + SV_SE("sv_SE", Language.SWEDISH); + + private final String localeCode; + private final Language language; + + Locale(String localeCode, Language language) { + this.localeCode = localeCode; + this.language = language; + } + + public Language getLanguage() { + return this.language; + } + + @Override + public String toString() { + return this.localeCode; + } +} From b6777a1facf9614ee937e166c64556b1327a3964 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 5/8] Client: Add strong types to language-related parameters These are still converted to strings by consumers, but it's a first step towards using these strong types everywhere. --- .../main/java/agolf/lobby/LobbyChatPanel.java | 3 +- .../java/com/aapeli/client/Parameters.java | 41 ++++++++++--------- .../java/com/aapeli/client/TextManager.java | 4 +- .../java/org/moparforia/shared/Language.java | 9 ++++ .../java/org/moparforia/shared/Locale.java | 9 ++++ 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/client/src/main/java/agolf/lobby/LobbyChatPanel.java b/client/src/main/java/agolf/lobby/LobbyChatPanel.java index 3eabf599..c00362e7 100644 --- a/client/src/main/java/agolf/lobby/LobbyChatPanel.java +++ b/client/src/main/java/agolf/lobby/LobbyChatPanel.java @@ -36,7 +36,8 @@ protected LobbyChatPanel(GameContainer gameContainer, int width, int height, int this.disableChatInput(2); } - this.setOutputToGlobal(Languages.getLanguageIdByString(gameContainer.params.getChatLocale())); + this.setOutputToGlobal(Languages.getLanguageIdByString( + gameContainer.params.getChatLocale().toString())); this.addChatListener(this); } diff --git a/client/src/main/java/com/aapeli/client/Parameters.java b/client/src/main/java/com/aapeli/client/Parameters.java index 3e76d145..b62a04d0 100644 --- a/client/src/main/java/com/aapeli/client/Parameters.java +++ b/client/src/main/java/com/aapeli/client/Parameters.java @@ -7,12 +7,13 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.StringTokenizer; +import org.moparforia.shared.Language; +import org.moparforia.shared.Locale; public final class Parameters { private static final String LOCALHOST = "127.0.0.1"; // private static final String aString1416 = "192.168.1.23"; - private static final String ENGLISH_LANGUAGE = "en"; private static final String PLAYFORIA_SITE_NAME = "playforia"; private static final String PLAYFORIA_QUIT_PAGE = "http://www.playforia.com/"; private static final String QUIT_TARGET = "_top"; @@ -21,9 +22,9 @@ public final class Parameters { private String codeBaseHost; private String documentBaseHost; private String serverIp; - private String language; - private String translationLocale; - private String chatLocale; + private Language language; + private Locale translationLocale; + private Locale chatLocale; private String siteName; private String username; private String sessionLocale; @@ -112,11 +113,11 @@ public int getServerPort() { return this.serverPort; } - public String language() { + public Language language() { return this.language; } - public String getTranslationLocale() { + public Locale getTranslationLocale() { return this.translationLocale; } @@ -124,11 +125,11 @@ public String getUsername() { return this.username; } - public String getChatLocale() { + public Locale getChatLocale() { return this.chatLocale != null ? this.chatLocale : this.translationLocale; } - public String getLocale() { + public Locale getLocale() { return this.getChatLocale(); } @@ -514,26 +515,26 @@ private int getParamPort() { } } - private String getParamLanguage() { + private Language getParamLanguage() { String language; try { language = this.getParameter("language"); if (language != null) { - return language; + return Language.fromString(language); } } catch (Exception e) { } if (this.codeBaseHost.endsWith("aapeli.com")) { - return "fi"; + return Language.FINNISH; } else if (this.codeBaseHost.endsWith("playray.com")) { - return ENGLISH_LANGUAGE; + return Language.ENGLISH; } else { if (this.codeBaseHost.endsWith(".playforia.com")) { try { language = this.codeBaseHost.substring(0, this.codeBaseHost.indexOf(46)); if (language.length() > 0 && !language.equals("www")) { - return language; + return Language.fromString(language); } } catch (Exception e) { } @@ -543,21 +544,21 @@ private String getParamLanguage() { try { language = this.codeBaseHost.substring(this.codeBaseHost.lastIndexOf(46) + 1); if (language.length() > 0) { - return language; + return Language.fromString(language); } } catch (Exception e) { } } - return ENGLISH_LANGUAGE; + return Language.ENGLISH; } } - private String getParamLocale() { + private Locale getParamLocale() { try { String locale = this.getParameter("locale"); if (locale != null) { - return locale; + return Locale.fromString(locale); } } catch (Exception e) { @@ -566,16 +567,16 @@ private String getParamLocale() { return null; } - private String getParamChatLocale() { + private Locale getParamChatLocale() { try { String chatLocale = this.getParameter("chatlocale"); if (chatLocale != null) { - return chatLocale; + return Locale.fromString(chatLocale); } chatLocale = this.getParameter("serverlocale"); if (chatLocale != null) { - return chatLocale; + return Locale.fromString(chatLocale); } } catch (Exception e) { } diff --git a/client/src/main/java/com/aapeli/client/TextManager.java b/client/src/main/java/com/aapeli/client/TextManager.java index c56a9eb5..1479a879 100644 --- a/client/src/main/java/com/aapeli/client/TextManager.java +++ b/client/src/main/java/com/aapeli/client/TextManager.java @@ -48,12 +48,12 @@ public TextManager(Parameters parameters, boolean debug) { public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boolean debug) { this(debug); this.parameters = parameters; - String language = parameters.getTranslationLocale(); + String language = parameters.getTranslationLocale().toString(); if (language != null) { this.language = language; this.useLanguageFiles = true; } else { - this.language = parameters.language(); + this.language = parameters.language().toString(); this.useLanguageFiles = false; } diff --git a/shared/src/main/java/org/moparforia/shared/Language.java b/shared/src/main/java/org/moparforia/shared/Language.java index f82a012d..da8432d9 100644 --- a/shared/src/main/java/org/moparforia/shared/Language.java +++ b/shared/src/main/java/org/moparforia/shared/Language.java @@ -15,4 +15,13 @@ public enum Language { public String toString() { return this.languageCode; } + + public static Language fromString(String code) { + for (Language l : Language.values()) { + if (l.languageCode.equalsIgnoreCase(code)) { + return l; + } + } + throw new IllegalArgumentException("No language with code " + code + " found"); + } } diff --git a/shared/src/main/java/org/moparforia/shared/Locale.java b/shared/src/main/java/org/moparforia/shared/Locale.java index eafc7659..81463fd9 100644 --- a/shared/src/main/java/org/moparforia/shared/Locale.java +++ b/shared/src/main/java/org/moparforia/shared/Locale.java @@ -21,4 +21,13 @@ public Language getLanguage() { public String toString() { return this.localeCode; } + + public static Locale fromString(String code) { + for (Locale l : Locale.values()) { + if (l.localeCode.equalsIgnoreCase(code)) { + return l; + } + } + throw new IllegalArgumentException("No locale with code " + code + " found"); + } } From 41b34fa6f6a1855dbdd5add7b137313148591432 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 6/8] Client: Remove unused code path for loading .loc localization files The repository has no .loc localization files so there's no point in keeping the code to load them around as it just makes TextManager harder to comprehend. Also, this enables simplifying the code path that is actually taken in loadLanguageFiles. --- .../java/com/aapeli/client/TextManager.java | 196 ++---------------- 1 file changed, 19 insertions(+), 177 deletions(-) diff --git a/client/src/main/java/com/aapeli/client/TextManager.java b/client/src/main/java/com/aapeli/client/TextManager.java index 1479a879..8d374d65 100644 --- a/client/src/main/java/com/aapeli/client/TextManager.java +++ b/client/src/main/java/com/aapeli/client/TextManager.java @@ -4,13 +4,6 @@ import com.aapeli.tools.Tools; import com.aapeli.tools.XmlUnit; import java.applet.Applet; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; import java.util.Calendar; import java.util.Date; import java.util.Hashtable; @@ -20,23 +13,11 @@ public final class TextManager implements Runnable { private Parameters parameters; private Thread textLoaderThread; private String language; - private Hashtable gameTable; - private Hashtable sharedTable; + private Hashtable gameTable; + private Hashtable sharedTable; private String errorMessage; - private boolean useLanguageFiles; // true == use language xml files, false == use locale .loc files private boolean debug; - public TextManager(Applet applet, String var2) { - this(applet, var2, false); - } - - public TextManager(Applet applet, String var2, boolean debug) { - this(debug); - this.language = var2; - this.useLanguageFiles = false; - this.loadTexts(applet); - } - public TextManager(Parameters parameters) { this(parameters, false, false); } @@ -48,14 +29,7 @@ public TextManager(Parameters parameters, boolean debug) { public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boolean debug) { this(debug); this.parameters = parameters; - String language = parameters.getTranslationLocale().toString(); - if (language != null) { - this.language = language; - this.useLanguageFiles = true; - } else { - this.language = parameters.language().toString(); - this.useLanguageFiles = false; - } + this.language = parameters.getTranslationLocale().toString(); if (loadTextsInSeparateThread) { this.textLoaderThread = new Thread(this); @@ -67,8 +41,8 @@ public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boo private TextManager(boolean debug) { this.debug = debug; - this.gameTable = new Hashtable(); - this.sharedTable = new Hashtable(); + this.gameTable = new Hashtable<>(); + this.sharedTable = new Hashtable<>(); this.errorMessage = null; this.textLoaderThread = null; } @@ -540,168 +514,36 @@ private String method1728(long var1, int var3) { protected String getText(String key, int quantity) { key = key.toLowerCase(); - if (this.useLanguageFiles) { - LocalizationNode localizationNode = (LocalizationNode) this.gameTable.get(key); - return localizationNode == null ? null : localizationNode.getLocalization(quantity); - } else { - return (String) this.gameTable.get(key); - } + LocalizationNode localizationNode = this.gameTable.get(key); + return localizationNode == null ? null : localizationNode.getLocalization(quantity); } protected String getSharedString(String key, int quantity) { key = key.toLowerCase(); - if (this.useLanguageFiles) { - LocalizationNode localizationNode = (LocalizationNode) this.sharedTable.get(key); - return localizationNode == null ? null : localizationNode.getLocalization(quantity); - } else { - return (String) this.sharedTable.get(key); - } + LocalizationNode localizationNode = this.sharedTable.get(key); + return localizationNode == null ? null : localizationNode.getLocalization(quantity); } private void loadTexts(Applet applet) { - if (this.useLanguageFiles) { - this.loadLanguageFiles(applet); - } else { - this.loadLocaleFiles(applet); - } - } - - private void loadLocaleFiles(Applet applet) { - URL codeBase = applet.getCodeBase(); - this.gameTable = this.loadLocalizationTable(codeBase); - - try { - if (FileUtil.isFileUrl(codeBase)) { - codeBase = new URL(codeBase, FileUtil.RESOURCE_DIR); - } else { - codeBase = new URL(codeBase, "../Shared/"); - } - } catch (MalformedURLException e) { - } - - this.sharedTable = this.loadLocalizationTable(codeBase); - } - - private Hashtable loadLocalizationTable(URL baseUrl) { - Hashtable localizationTable = new Hashtable<>(); - BufferedReader reader = null; - String languageFileName = this.language + ".loc"; - - try { - URL localeUrl = new URL(baseUrl, "locale/"); - localeUrl = new URL(localeUrl, languageFileName); - InputStream inputStream = localeUrl.openStream(); - - InputStreamReader inputStreamReader; - try { - inputStreamReader = new InputStreamReader(inputStream, "Cp1252"); - } catch (UnsupportedEncodingException e) { - inputStreamReader = new InputStreamReader(inputStream); - } - - reader = new BufferedReader(inputStreamReader); - - String line; - while ((line = reader.readLine()) != null) { - line = line.trim(); - if (line.length() > 0 && line.charAt(0) != '#') { - int equalSignLocation = line.indexOf('='); - if (equalSignLocation <= 0) { - if (this.debug) { - System.out.println( - "Missing '='-character in \"" + this.language + "\"-locale file: \"" + line + "\""); - Thread.dumpStack(); - } - } else { - String key = line.substring(0, equalSignLocation).trim(); - if (key.length() == 0) { - if (this.debug) { - System.out.println( - "Empty key in \"" + this.language + "\"-locale file: \"" + line + "\""); - Thread.dumpStack(); - } - } else { - localizationTable.put( - key.toLowerCase(), - line.substring(equalSignLocation + 1).trim()); - } - } - } - } - } catch (FileNotFoundException e) { - if (this.debug) { - System.out.println("Missing localization file \"" + languageFileName + "\""); - } - - this.errorMessage = "Texts for '" + this.language + "' not available"; - localizationTable = null; - } catch (Exception e) { - if (this.debug) { - e.printStackTrace(); - } - - this.errorMessage = e.toString(); - localizationTable = null; - } - - try { - reader.close(); - } catch (Exception e) { - } - - return localizationTable; + this.loadLanguageFiles(applet); } private void loadLanguageFiles(Applet applet) { - URL codeBase = applet.getCodeBase(); - String var5 = null; - int slashLocation = this.language.indexOf('/'); - if (slashLocation > 0) { - var5 = this.language.substring(slashLocation + 1); - this.language = this.language.substring(0, slashLocation); + String codeBasePath = applet.getCodeBase().toString(); + if (codeBasePath.endsWith("/")) { + codeBasePath = codeBasePath.substring(0, codeBasePath.length() - 1); } - String languageDirectory; - String gameFilename; - String var7; - int var8; - if (FileUtil.isFileUrl(codeBase)) { - var7 = codeBase.toString(); - var8 = var7.indexOf(':', var7.indexOf(':') + 1) + 2; - int var9 = var7.indexOf('/', var8); - - try { - URL var10 = new URL(codeBase, FileUtil.LANGUAGE_DIR); - var10 = new URL(var10, this.language + "/"); - languageDirectory = var10.toExternalForm(); - } catch (MalformedURLException e) { - languageDirectory = "file:" + FileUtil.LANGUAGE_DIR + this.language + "/"; - } - - gameFilename = var7.substring(var8, var9); - } else { - var7 = codeBase.toString(); - var8 = var7.length(); - if (var7.charAt(var8 - 1) == '/') { - var7 = var7.substring(0, var8 - 1); - --var8; - } - - int slashLocation2 = var7.lastIndexOf('/'); - languageDirectory = var7.substring(0, slashLocation2 + 1) + "l10n/" + this.language + "/"; - gameFilename = var7.substring(slashLocation2 + 1); - } - - if (var5 != null) { - gameFilename = var5; - } + int slashLocation = codeBasePath.lastIndexOf('/'); + String languageDirectoryPath = codeBasePath.substring(0, slashLocation + 1) + "l10n/" + this.language + "/"; + String gameFilename = codeBasePath.substring(slashLocation + 1); - this.gameTable = this.readTable(languageDirectory + gameFilename + ".xml"); - this.sharedTable = this.readTable(languageDirectory + "Shared.xml"); + this.gameTable = this.readTable(languageDirectoryPath + gameFilename + ".xml"); + this.sharedTable = this.readTable(languageDirectoryPath + "Shared.xml"); } private Hashtable readTable(String fileUrl) { - EncodedXmlReader reader = new EncodedXmlReader(fileUrl, /*this.aBoolean1519*/ true); + EncodedXmlReader reader = new EncodedXmlReader(fileUrl, true); XmlUnit unit = reader.readXmlUnit(); if (unit == null) { System.out.println("Failed to read localization file '" + fileUrl + "'"); 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 7/8] 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; From 47c062096e9b6be42e68351c25773abeb015b346 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 8/8] Client: Add language selector to login screen Now users can play the game in a different language without using CLI flags. --- client/src/main/java/agolf/GameApplet.java | 7 ++ .../main/java/agolf/TrackTestLoginPanel.java | 65 +++++++++++++------ .../java/com/aapeli/client/TextManager.java | 5 ++ 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/client/src/main/java/agolf/GameApplet.java b/client/src/main/java/agolf/GameApplet.java index 73065a72..34bfdcec 100644 --- a/client/src/main/java/agolf/GameApplet.java +++ b/client/src/main/java/agolf/GameApplet.java @@ -14,6 +14,7 @@ import java.awt.Font; import java.awt.Image; import org.moparforia.client.Launcher; +import org.moparforia.shared.Locale; public class GameApplet extends AApplet { @@ -242,6 +243,12 @@ protected void trackTestLogin(String username, String password) { this.gameContainer.connection.writeData("ttlogin\t" + username + "\t" + password); } + protected void trackTestLogin(String username, String password, Locale locale) { + this.textManager.setLocale(locale, this); + this.gameContainer.connection.writeData("language\t" + locale); + this.trackTestLogin(username, password); + } + public boolean isEmailVerified() { return this.syncUnknownBool.get(); } diff --git a/client/src/main/java/agolf/TrackTestLoginPanel.java b/client/src/main/java/agolf/TrackTestLoginPanel.java index 17a704ae..f82c77fd 100644 --- a/client/src/main/java/agolf/TrackTestLoginPanel.java +++ b/client/src/main/java/agolf/TrackTestLoginPanel.java @@ -1,5 +1,6 @@ package agolf; +import com.aapeli.colorgui.Choicer; import com.aapeli.multiuser.UsernameValidator; import java.awt.Button; import java.awt.Color; @@ -9,27 +10,30 @@ import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import org.moparforia.shared.Locale; -class TrackTestLoginPanel extends Panel implements ActionListener, KeyListener { +class TrackTestLoginPanel extends Panel implements ActionListener, KeyListener, ItemListener { private GameApplet gameApplet; private int width; private int height; + private Locale locale; private TextField textFieldName; private TextField textFieldPassword; private Button buttonOk; private Label labelError; private Label labelName; - private Label labelName2; - private Label labelPassword; - private Label labelPassword2; + private Choicer languageChoicer; protected TrackTestLoginPanel(GameApplet gameApplet, int width, int height) { this.gameApplet = gameApplet; this.width = width; this.height = height; + this.locale = gameApplet.param.getLocale(); this.setSize(width, height); this.create(); } @@ -39,8 +43,8 @@ public void addNotify() { this.repaint(); } - public void paint(Graphics var1) { - this.update(var1); + public void paint(Graphics g) { + this.update(g); } public void update(Graphics g) { @@ -52,7 +56,7 @@ public void actionPerformed(ActionEvent evt) { String username = this.textFieldName.getText().trim(); String password = this.textFieldPassword.getText().trim(); // String password = ''; - this.gameApplet.trackTestLogin(username, password); + this.gameApplet.trackTestLogin(username, password, locale); } public void keyPressed(KeyEvent evt) {} @@ -72,7 +76,23 @@ private void create() { this.textFieldPassword.setBackground(Color.white); this.textFieldPassword.setForeground(Color.black); textFieldPassword.setEchoChar('*'); - // this.add(this.textFieldPassword); //Don't show this field + + this.languageChoicer = new Choicer(); + this.languageChoicer.setBounds(this.width / 2 - 75, this.height / 2 - 10, 150, 25); + this.languageChoicer.addItem("English"); + this.languageChoicer.addItem("Finnish"); + this.languageChoicer.addItem("Swedish"); + this.languageChoicer.setBackground(Color.white); + this.languageChoicer.setForeground(Color.black); + this.languageChoicer.addItemListener(this); + int selectedLanguageIndex = + switch (locale) { + case EN_US -> 0; + case FI_FI -> 1; + case SV_SE -> 2; + }; + this.languageChoicer.setSelectedIndex(selectedLanguageIndex); + this.add(this.languageChoicer); this.buttonOk = new Button("OK"); this.buttonOk.setBounds(this.width / 2 - 75, this.height / 2 + 50, 75, 25); @@ -87,18 +107,6 @@ private void create() { labelName = new Label("Nickname:"); labelName.setBounds(width / 2 - 200, height / 2 - 60, 75, 25); add(labelName); - // No more labels needed - // labelName2 = new Label(""); - // labelName2.setBounds(width / 2 + 80, height / 2 - 60, 75, 25); - // labelName2.setForeground(Color.red); - // add(labelName2); - // labelPassword = new Label(""); - // labelPassword.setBounds(width / 2 - 150, height / 2 - 10, 75, 25); - // add(labelPassword); - // labelPassword2 = new Label(""); - // labelPassword2.setBounds(width / 2 + 80, height / 2 - 10, 75, 25); - // labelPassword2.setForeground(Color.red); - // add(labelPassword2); } public void keyTyped(KeyEvent e) {} @@ -117,4 +125,21 @@ public void keyReleased(KeyEvent e) { buttonOk.setEnabled(false); } } + + @Override + public void itemStateChanged(ItemEvent e) { + if (e.getSource() == this.languageChoicer) { + switch (this.languageChoicer.getSelectedIndex()) { + case 0: + this.locale = Locale.EN_US; + break; + case 1: + this.locale = Locale.FI_FI; + break; + case 2: + this.locale = Locale.SV_SE; + break; + } + } + } } diff --git a/client/src/main/java/com/aapeli/client/TextManager.java b/client/src/main/java/com/aapeli/client/TextManager.java index a3ab33ed..ad54d833 100644 --- a/client/src/main/java/com/aapeli/client/TextManager.java +++ b/client/src/main/java/com/aapeli/client/TextManager.java @@ -60,6 +60,11 @@ public void run() { } } + public void setLocale(Locale locale, Applet applet) { + this.locale = locale; + this.loadTexts(applet); + } + public String getGame(String key) { return this.getGame(key, (String[]) null); }