Skip to content

Commit

Permalink
Client+Server: Use Locale enum in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
StenAL committed Nov 7, 2024
1 parent 41b34fa commit bc3fd2e
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 54 deletions.
3 changes: 1 addition & 2 deletions client/src/main/java/agolf/lobby/LobbyChatPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Binary file modified client/src/main/java/com/aapeli/client/BadWordFilter.java
Binary file not shown.
12 changes: 6 additions & 6 deletions client/src/main/java/com/aapeli/client/LocalizationNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions client/src/main/java/com/aapeli/client/TextManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, LocalizationNode> gameTable;
private Hashtable<String, LocalizationNode> sharedTable;
private String errorMessage;
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand All @@ -556,8 +557,7 @@ private Hashtable<String, LocalizationNode> 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;
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/com/aapeli/multiuser/ChatBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 5 additions & 9 deletions client/src/main/java/com/aapeli/multiuser/Languages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
}
Expand Down
9 changes: 8 additions & 1 deletion client/src/main/java/com/aapeli/multiuser/UserList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 23 additions & 22 deletions server/src/main/java/org/moparforia/server/game/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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 : "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
}
3 changes: 2 additions & 1 deletion shared/src/main/java/org/moparforia/shared/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
public enum Language {
ENGLISH("en"),
FINNISH("fi"),
SWEDISH("sv");
SWEDISH("sv"),
FRENCH("fr");

private final String languageCode;

Expand Down

0 comments on commit bc3fd2e

Please sign in to comment.