added localization options

//todo add all the strings
This commit is contained in:
Ticho Hidding
2025-09-25 14:38:06 +02:00
parent 7431d1b03f
commit 2c4719f6d4
8 changed files with 62 additions and 97 deletions

View File

@@ -1,5 +1,6 @@
package org.toop;
import org.toop.app.gui.LocalServerSelector;
import org.toop.framework.networking.NetworkingClientManager;
import org.toop.framework.networking.NetworkingInitializationException;
@@ -7,6 +8,7 @@ import org.toop.framework.networking.NetworkingInitializationException;
public class Main {
public static void main(String[] args) {
initSystems();
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
}
private static void initSystems() throws NetworkingInitializationException {

View File

@@ -1,15 +1,23 @@
package org.toop.app.gui;
import org.toop.events.WindowEvents;
import org.toop.framework.eventbus.EventFlow;
import org.toop.local.AppContext;
import javax.swing.*;
import java.util.Locale;
import java.util.ResourceBundle;
public class LocalServerSelector {
private JPanel panel1;
private JButton serverButton;
private JButton localButton;
private final JFrame frame;
Locale locale = AppContext.getLocale();
ResourceBundle resourceBundle = ResourceBundle.getBundle("Localization", locale);
public LocalServerSelector() {
frame = new JFrame("Server Selector");
frame = new JFrame(resourceBundle.getString("windowTitleServerSelector"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel1);
frame.setSize(1920, 1080);
@@ -17,9 +25,15 @@ public class LocalServerSelector {
frame.setVisible(true);
serverButton.addActionListener(e -> onServerClicked());
serverButton.setText(resourceBundle.getString("buttonSelectServer"));
localButton.addActionListener(e -> onLocalClicked());
localButton.setText(resourceBundle.getString("buttonSelectLocal"));
new EventFlow().listen(WindowEvents.LanguageChanged.class, this::changeLanguage);
}
private void changeLanguage(WindowEvents.LanguageChanged event) {
locale = AppContext.getLocale();
resourceBundle = ResourceBundle.getBundle("Localization", locale);
}
private void onServerClicked() {
frame.dispose();
new RemoteGameSelector();

View File

@@ -22,4 +22,7 @@ public class WindowEvents extends EventsBase {
/** Triggers when the mouse is released within the window. */
public record OnMouseRelease(int button) implements EventWithoutSnowflake {}
/** Triggers when the language is changed. */
public record LanguageChanged() implements EventWithoutSnowflake {}
}

View File

@@ -0,0 +1,14 @@
package org.toop.local;
import java.util.Locale;
public class AppContext {
private static Locale currentLocale = Locale.getDefault();
public static void setCurrentLocale(Locale locale) {
currentLocale = locale;
}
public static Locale getLocale() {
return currentLocale;
}
}

View File

@@ -2,11 +2,16 @@ package org.toop.tictactoe.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.app.gui.LocalGameSelector;
import org.toop.app.gui.RemoteGameSelector;
import org.toop.events.WindowEvents;
import org.toop.framework.eventbus.EventFlow;
import org.toop.local.AppContext;
import org.toop.tictactoe.LocalTicTacToe;
import org.toop.game.GameBase;
@@ -26,6 +31,8 @@ public class UIGameBoard {
private LocalTicTacToe localTicTacToe;
private boolean gameOver = false;
Locale locale = AppContext.getLocale();
ResourceBundle resourceBundle = ResourceBundle.getBundle("Localization", locale);
public UIGameBoard(LocalTicTacToe lttt, Object parent) {
if (!(parent == null)) {
@@ -43,7 +50,7 @@ public class UIGameBoard {
tttPanel = new JPanel(new BorderLayout());
// Back button
backToMainMenuButton = new JButton("Back to Main Menu");
backToMainMenuButton = new JButton(resourceBundle.getString("buttonBackToMainMenu"));
tttPanel.add(backToMainMenuButton, BorderLayout.SOUTH);
backToMainMenuButton.addActionListener(
_ -> {
@@ -65,7 +72,11 @@ public class UIGameBoard {
// cells[moveIndex].setText(String.valueOf(symbol));
// });
// });
new EventFlow().listen(WindowEvents.LanguageChanged.class, this::changeLanguage);
}
private void changeLanguage(WindowEvents.LanguageChanged event) {
locale = AppContext.getLocale();
resourceBundle = ResourceBundle.getBundle("Localization", locale);
}
private JPanel createGridPanel(int sizeX, int sizeY) {

View File

@@ -0,0 +1,9 @@
# Window titles
windowTitleServerSelector=Server Selector
# Buttons on the server selector screen
buttonSelectServer=Connect to a server
buttonSelectLocal=Play a game on this computer
# Buttons on the UI gameboard
buttonBackToMainMenu=Go back to the main menu

View File

@@ -0,0 +1,2 @@
# Window titles
windowTitleServerSelector=Selecteer een server