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/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/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/agolf/lobby/LobbyChatPanel.java b/client/src/main/java/agolf/lobby/LobbyChatPanel.java index 5384ee9f..ebc9193e 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.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 bd164f14..a9e663b5 100644 Binary files a/client/src/main/java/com/aapeli/client/BadWordFilter.java and b/client/src/main/java/com/aapeli/client/BadWordFilter.java differ 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/Parameters.java b/client/src/main/java/com/aapeli/client/Parameters.java index 49bce241..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 locale; - private String translationLanguage; - private String chatLang; + private Language language; + private Locale translationLocale; + private Locale chatLocale; private String siteName; private String username; private String sessionLocale; @@ -112,24 +113,24 @@ public int getServerPort() { return this.serverPort; } - public String getLocale() { - return this.locale; + public Language language() { + return this.language; } - public String getTranslationLang() { - return this.translationLanguage; + public Locale getTranslationLocale() { + return this.translationLocale; } public String getUsername() { return this.username; } - public String getChatLang() { - return this.chatLang != null ? this.chatLang : this.translationLanguage; + public Locale getChatLocale() { + return this.chatLocale != null ? this.chatLocale : this.translationLocale; } - public String getLang() { - return this.getChatLang(); + public Locale getLocale() { + return this.getChatLocale(); } public String getSiteName() { @@ -392,9 +393,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 +450,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,26 +515,26 @@ private int getParamPort() { } } - private String getParamLocale() { - String locale; + private Language getParamLanguage() { + String language; try { - locale = this.getParameter("locale"); - if (locale != null) { - return locale; + language = this.getParameter("language"); + if (language != null) { + 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 { - 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.fromString(language); } } catch (Exception e) { } @@ -541,45 +542,41 @@ 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.fromString(language); } } catch (Exception e) { } } - return ENGLISH_LANGUAGE; + return Language.ENGLISH; } } - private String getParamLanguage() { + private Locale getParamLocale() { try { - String language = this.getParameter("lang"); - if (language != null) { - return language; + String locale = this.getParameter("locale"); + if (locale != null) { + return Locale.fromString(locale); } - language = this.getParameter("language"); - if (language != null) { - return language; - } } catch (Exception e) { } return null; } - private String getParamChatLanguage() { + private Locale getParamChatLocale() { try { - String chatLanguage = this.getParameter("chatlang"); - if (chatLanguage != null) { - return chatLanguage; + String chatLocale = this.getParameter("chatlocale"); + if (chatLocale != null) { + return Locale.fromString(chatLocale); } - chatLanguage = this.getParameter("serverlang"); - if (chatLanguage != null) { - return chatLanguage; + chatLocale = this.getParameter("serverlocale"); + if (chatLocale != null) { + 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 56d1aa52..ad54d833 100644 --- a/client/src/main/java/com/aapeli/client/TextManager.java +++ b/client/src/main/java/com/aapeli/client/TextManager.java @@ -4,39 +4,21 @@ 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; +import org.moparforia.shared.Locale; public final class TextManager implements Runnable { private Parameters parameters; private Thread textLoaderThread; - private String language; - private Hashtable gameTable; - private Hashtable sharedTable; + private Locale locale; + 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 +30,7 @@ public TextManager(Parameters parameters, boolean debug) { public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boolean debug) { this(debug); this.parameters = parameters; - String language = parameters.getTranslationLang(); - if (language != null) { - this.language = language; - this.useLanguageFiles = true; - } else { - this.language = parameters.getLocale(); - this.useLanguageFiles = false; - } + this.locale = parameters.getTranslationLocale(); if (loadTextsInSeparateThread) { this.textLoaderThread = new Thread(this); @@ -67,8 +42,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; } @@ -85,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); } @@ -333,13 +313,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) { @@ -540,168 +520,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); - } + 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) { + String codeBasePath = applet.getCodeBase().toString(); + if (codeBasePath.endsWith("/")) { + codeBasePath = codeBasePath.substring(0, codeBasePath.length() - 1); } - 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; - } - - 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 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.locale + "/"; + 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 + "'"); @@ -714,8 +562,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/client/src/main/java/org/moparforia/client/Game.java b/client/src/main/java/org/moparforia/client/Game.java index 5415fa7a..d1d9b2ac 100644 --- a/client/src/main/java/org/moparforia/client/Game.java +++ b/client/src/main/java/org/moparforia/client/Game.java @@ -8,16 +8,17 @@ 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, String lang, 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, 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 +33,7 @@ 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, Locale locale, String username, int port, boolean verbose, boolean norandom) { if (server.indexOf(':') == -1) { // is ipv4 this.server = server; } else { // is ipv6 @@ -52,11 +53,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", locale.getLanguage().toString()); + 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..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; @@ -45,7 +46,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 +150,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 +166,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 { @@ -183,21 +184,4 @@ public void setHostname(String hostname) { public void setPort(int port) { this.port = port; } - - enum Language { - EN_US("en_US"), - FI_FI("fi_FI"), - SV_SE("sv_SE"); - - private final String custom_name; - - Language(String name) { - this.custom_name = name; - } - - @Override - public String toString() { - return this.custom_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..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.Language.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.Language.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.Language.EN_US), + eq(Locale.EN_US), eq(null), eq(false), eq(false)); 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 new file mode 100644 index 00000000..2d810ea7 --- /dev/null +++ b/shared/src/main/java/org/moparforia/shared/Language.java @@ -0,0 +1,28 @@ +package org.moparforia.shared; + +public enum Language { + ENGLISH("en"), + FINNISH("fi"), + SWEDISH("sv"), + FRENCH("fr"); + + private final String languageCode; + + Language(String languageCode) { + this.languageCode = languageCode; + } + + @Override + 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 new file mode 100644 index 00000000..81463fd9 --- /dev/null +++ b/shared/src/main/java/org/moparforia/shared/Locale.java @@ -0,0 +1,33 @@ +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; + } + + 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"); + } +}