All locale should work now

This commit is contained in:
Oskar Rasieński 2025-12-04 01:37:32 +01:00
commit ebd896cbee
6 changed files with 53 additions and 14 deletions

View file

@ -53,6 +53,6 @@ public class Main extends Application {
var addStep = FXML.load(AddStepsCtrl.class, "client", "scenes", "AddSteps.fxml");
var mainCtrl = INJECTOR.getInstance(MainCtrl.class);
mainCtrl.initialize(primaryStage, addName, foodpal, addIngredient, addStep);
mainCtrl.setup(primaryStage, addName, foodpal, addIngredient, addStep);
}
}

View file

@ -15,6 +15,10 @@
*/
package client.scenes;
import client.utils.LocaleAware;
import client.utils.LocaleManager;
import jakarta.inject.Inject;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
@ -22,28 +26,42 @@ import javafx.util.Pair;
import java.io.IOException;
public class MainCtrl {
public class MainCtrl implements LocaleAware {
private final LocaleManager localeManager;
@FXML
private Stage primaryStage;
@FXML
private AddNameCtrl addNameCtrl;
private Scene addName;
@FXML
private FoodpalApplicationCtrl foodpalCtrl;
private Scene foodpal;
@FXML
private AddIngredientCtrl addIngredientCtrl;
private Scene addIngredient;
@FXML
private AddStepsCtrl addStepsCtrl;
private Scene addStep;
public void initialize(Stage primaryStage,
Pair<AddNameCtrl, Parent> addName,
Pair<FoodpalApplicationCtrl, Parent> foodpal,
Pair<AddIngredientCtrl, Parent> addIngredient,
Pair<AddStepsCtrl, Parent> addStep
) throws IOException, InterruptedException {
private String addNameTitle = "Naming recipes (default)";
private String addIngredientTitle = "To add ingredients (default)";
private String addStepTitle = "To add preparation steps (default)";
@Inject
public MainCtrl(LocaleManager localeManager) {
this.localeManager = localeManager;
}
public void setup(Stage primaryStage,
Pair<AddNameCtrl, Parent> addName,
Pair<FoodpalApplicationCtrl, Parent> foodpal,
Pair<AddIngredientCtrl, Parent> addIngredient,
Pair<AddStepsCtrl, Parent> addStep) throws IOException, InterruptedException {
this.primaryStage = primaryStage;
@ -59,14 +77,26 @@ public class MainCtrl {
this.addStepsCtrl = addStep.getKey();
this.addStep = new Scene(addStep.getValue());
showFoodpal();
primaryStage.show();
initialize(); // Initialize LocaleManager stuff manually since MainCtrl isn't loaded from FXML.
}
@Override
public void updateText() {
addNameTitle = getLocaleString("add.recipe.title");
addStepTitle = getLocaleString("add.step.title");
addIngredientTitle = getLocaleString("add.ingredient.title");
}
@Override
public LocaleManager getLocaleManager() {
return localeManager;
}
public void showAddName() {
primaryStage.setTitle("Naming recipes");
primaryStage.setTitle(addNameTitle);
primaryStage.setScene(addName);
addName.setOnKeyPressed(e -> {
try {
@ -84,7 +114,7 @@ public class MainCtrl {
}
public void showAddIngredient() {
primaryStage.setTitle("To add ingredients");
primaryStage.setTitle(addIngredientTitle);
primaryStage.setScene(addIngredient);
addIngredient.setOnKeyPressed(e -> {
try {
@ -97,7 +127,7 @@ public class MainCtrl {
}
public void showAddSteps() {
primaryStage.setTitle("To add preparation steps");
primaryStage.setTitle(addStepTitle);
primaryStage.setScene(addStep);
addStep.setOnKeyPressed(e -> {
try {

View file

@ -27,6 +27,10 @@ public interface LocaleAware extends Initializable {
initializeComponents();
}
default void initialize() {
initialize(null, null);
}
default String getLocaleString(String key) {
return getLocaleManager().getBundle().getString(key);
}

View file

@ -25,18 +25,22 @@ public class LocaleManager {
currentBundle.set(bundle);
}
@SuppressWarnings("unused")
public void setLocale(Locale locale) {
currentLocale.set(locale);
}
@SuppressWarnings("unused")
public Locale getLocale() {
return currentLocale.get();
}
@SuppressWarnings("unused")
public ResourceBundle getBundle() {
return currentBundle.get();
}
@SuppressWarnings("unused")
public ObjectProperty<ResourceBundle> getBundleProperty() {
return currentBundle;
}

View file

@ -1,6 +1,6 @@
add.ingredient.title=Dodaj składnik
add.recipe.title=Utwórz przepis
add.step.title=Dodaj instrukcje
add.step.title=Dodaj instrukcję
add.ingredient.label=Składnik
add.recipe.label=Nazwa przepisu

View file

@ -15,6 +15,7 @@
*/
package client.scenes;
import client.utils.LocaleManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -24,7 +25,7 @@ public class MainCtrlTest {
@BeforeEach
public void setup() {
sut = new MainCtrl();
sut = new MainCtrl(new LocaleManager());
}
@Test