Added base locale functionality. Works in AddNameCtrl for now.
This commit is contained in:
parent
fc13f7b847
commit
6e9ac2b055
10 changed files with 136 additions and 8 deletions
|
|
@ -16,6 +16,7 @@
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
import client.scenes.FoodpalApplicationCtrl;
|
import client.scenes.FoodpalApplicationCtrl;
|
||||||
|
import client.utils.LocaleManager;
|
||||||
import com.google.inject.Binder;
|
import com.google.inject.Binder;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
import com.google.inject.Scopes;
|
import com.google.inject.Scopes;
|
||||||
|
|
@ -30,5 +31,7 @@ public class MyModule implements Module {
|
||||||
binder.bind(MainCtrl.class).in(Scopes.SINGLETON);
|
binder.bind(MainCtrl.class).in(Scopes.SINGLETON);
|
||||||
binder.bind(AddNameCtrl.class).in(Scopes.SINGLETON);
|
binder.bind(AddNameCtrl.class).in(Scopes.SINGLETON);
|
||||||
binder.bind(FoodpalApplicationCtrl.class).in(Scopes.SINGLETON);
|
binder.bind(FoodpalApplicationCtrl.class).in(Scopes.SINGLETON);
|
||||||
|
|
||||||
|
binder.bind(LocaleManager.class).in(Scopes.SINGLETON);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,28 +15,50 @@
|
||||||
*/
|
*/
|
||||||
package client.scenes;
|
package client.scenes;
|
||||||
|
|
||||||
|
import client.utils.LocaleAware;
|
||||||
|
import client.utils.LocaleManager;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.ServerUtils;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
import jakarta.ws.rs.WebApplicationException;
|
import jakarta.ws.rs.WebApplicationException;
|
||||||
import javafx.scene.control.Alert;
|
import javafx.scene.control.Alert;
|
||||||
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.input.KeyEvent;
|
import javafx.scene.input.KeyEvent;
|
||||||
import javafx.stage.Modality;
|
import javafx.stage.Modality;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class AddNameCtrl {
|
public class AddNameCtrl implements LocaleAware {
|
||||||
|
|
||||||
private final ServerUtils server;
|
private final ServerUtils server;
|
||||||
private final MainCtrl mainCtrl;
|
private final MainCtrl mainCtrl;
|
||||||
public TextField recipeName;
|
|
||||||
|
|
||||||
|
private final LocaleManager localeManager;
|
||||||
|
|
||||||
|
public TextField recipeName;
|
||||||
|
public Label recipeNameLabel;
|
||||||
|
public Button cancelButton;
|
||||||
|
public Button okButton;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public AddNameCtrl(ServerUtils server, MainCtrl mainCtrl) {
|
public AddNameCtrl(ServerUtils server, MainCtrl mainCtrl, LocaleManager localeManager) {
|
||||||
this.mainCtrl = mainCtrl;
|
this.mainCtrl = mainCtrl;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
|
this.localeManager = localeManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateText() {
|
||||||
|
recipeNameLabel.setText(getLocaleString("add.recipe.label"));
|
||||||
|
okButton.setText(getLocaleString("add.recipe.button.ok"));
|
||||||
|
cancelButton.setText(getLocaleString("add.recipe.button.cancel"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocaleManager getLocaleManager() {
|
||||||
|
return localeManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancel() throws IOException, InterruptedException {
|
public void cancel() throws IOException, InterruptedException {
|
||||||
|
|
@ -78,4 +100,5 @@ public class AddNameCtrl {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -3,7 +3,9 @@ package client.scenes;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import client.utils.LocaleManager;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.ServerUtils;
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
|
|
||||||
|
|
@ -17,6 +19,7 @@ import javafx.scene.control.ListView;
|
||||||
public class FoodpalApplicationCtrl {
|
public class FoodpalApplicationCtrl {
|
||||||
private final MainCtrl mainCtrl;
|
private final MainCtrl mainCtrl;
|
||||||
private final ServerUtils server;
|
private final ServerUtils server;
|
||||||
|
private final LocaleManager localeManager;
|
||||||
|
|
||||||
|
|
||||||
// all of these aren't used with only my part of the code
|
// all of these aren't used with only my part of the code
|
||||||
|
|
@ -72,9 +75,10 @@ public class FoodpalApplicationCtrl {
|
||||||
private Button addPreparationStepButton;
|
private Button addPreparationStepButton;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FoodpalApplicationCtrl(MainCtrl mainCtrl, ServerUtils server) {
|
public FoodpalApplicationCtrl(MainCtrl mainCtrl, ServerUtils server, LocaleManager localeManager) {
|
||||||
this.mainCtrl = mainCtrl;
|
this.mainCtrl = mainCtrl;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
|
this.localeManager = localeManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
|
|
@ -192,16 +196,20 @@ public class FoodpalApplicationCtrl {
|
||||||
@FXML
|
@FXML
|
||||||
private void switchToEnglish() {
|
private void switchToEnglish() {
|
||||||
System.out.println("Switch language to EN");
|
System.out.println("Switch language to EN");
|
||||||
|
localeManager.setLocale(Locale.of("en"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void switchToDutch() {
|
private void switchToDutch() {
|
||||||
System.out.println("Switch language to NL");
|
System.out.println("Switch language to NL");
|
||||||
|
localeManager.setLocale(Locale.of("nl"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void switchToPolish() {
|
private void switchToPolish() {
|
||||||
System.out.println("Switch language to PL");
|
System.out.println("Switch language to PL");
|
||||||
|
localeManager.setLocale(Locale.of("pl"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
38
client/src/main/java/client/utils/LocaleAware.java
Normal file
38
client/src/main/java/client/utils/LocaleAware.java
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
package client.utils;
|
||||||
|
|
||||||
|
import javafx.fxml.Initializable;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public interface LocaleAware extends Initializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates all text when locale changes.
|
||||||
|
* Must be implemented by each controller.
|
||||||
|
*/
|
||||||
|
void updateText();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the injected LocaleManager.
|
||||||
|
* Must be implemented by each controller.
|
||||||
|
*/
|
||||||
|
LocaleManager getLocaleManager();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default void initialize(URL location, ResourceBundle resources) {
|
||||||
|
updateText();
|
||||||
|
|
||||||
|
getLocaleManager().getBundleProperty().addListener((_, _, _) -> updateText());
|
||||||
|
|
||||||
|
initializeComponents();
|
||||||
|
}
|
||||||
|
|
||||||
|
default String getLocaleString(String key) {
|
||||||
|
return getLocaleManager().getBundle().getString(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional function for initialization of components. Runs after localization.
|
||||||
|
*/
|
||||||
|
default void initializeComponents() {}
|
||||||
|
}
|
||||||
40
client/src/main/java/client/utils/LocaleManager.java
Normal file
40
client/src/main/java/client/utils/LocaleManager.java
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
package client.utils;
|
||||||
|
|
||||||
|
import javafx.beans.property.ObjectProperty;
|
||||||
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public class LocaleManager {
|
||||||
|
private final ObjectProperty<Locale> currentLocale = new SimpleObjectProperty<>(Locale.ENGLISH);
|
||||||
|
private final ObjectProperty<ResourceBundle> currentBundle = new SimpleObjectProperty<>();
|
||||||
|
|
||||||
|
private static final String RESOURCE_BUNDLE_PATH = "locale/lang";
|
||||||
|
|
||||||
|
public LocaleManager() {
|
||||||
|
// TODO: Set currentLocale to config value instead of EN default.
|
||||||
|
updateBundle();
|
||||||
|
currentLocale.addListener((_, _, _) -> updateBundle());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateBundle() {
|
||||||
|
ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_PATH, currentLocale.get());
|
||||||
|
currentBundle.set(bundle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocale(Locale locale) {
|
||||||
|
currentLocale.set(locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Locale getLocale() {
|
||||||
|
return currentLocale.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResourceBundle getBundle() {
|
||||||
|
return currentBundle.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectProperty<ResourceBundle> getBundleProperty() {
|
||||||
|
return currentBundle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,9 +7,9 @@
|
||||||
|
|
||||||
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.AddNameCtrl">
|
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.AddNameCtrl">
|
||||||
<children>
|
<children>
|
||||||
<Button layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
|
<Button fx:id="okButton" layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
|
||||||
<Button layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
|
<Button fx:id="cancelButton" layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
|
||||||
<TextField fx:id="recipeName" layoutX="109.0" layoutY="24.0" prefHeight="18.0" prefWidth="292.0" />
|
<TextField fx:id="recipeName" layoutX="109.0" layoutY="24.0" prefHeight="18.0" prefWidth="292.0" />
|
||||||
<Label layoutX="28.0" layoutY="29.0" text="Recipe Name" />
|
<Label fx:id="recipeNameLabel" layoutX="28.0" layoutY="29.0" text="Recipe Name" />
|
||||||
</children>
|
</children>
|
||||||
</AnchorPane>
|
</AnchorPane>
|
||||||
4
client/src/main/resources/locale/lang.properties
Normal file
4
client/src/main/resources/locale/lang.properties
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
add.recipe.title=Create recipe
|
||||||
|
add.recipe.label=Recipe Name
|
||||||
|
add.recipe.button.ok=Ok
|
||||||
|
add.recipe.button.cancel=Cancel
|
||||||
4
client/src/main/resources/locale/lang_en.properties
Normal file
4
client/src/main/resources/locale/lang_en.properties
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
add.recipe.title=Create recipe
|
||||||
|
add.recipe.label=Recipe Name
|
||||||
|
add.recipe.button.ok=Ok
|
||||||
|
add.recipe.button.cancel=Cancel
|
||||||
4
client/src/main/resources/locale/lang_nl.properties
Normal file
4
client/src/main/resources/locale/lang_nl.properties
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
add.recipe.title=Recept aanmaken
|
||||||
|
add.recipe.label=Receptnaam
|
||||||
|
add.recipe.button.ok=Ok
|
||||||
|
add.recipe.button.cancel=Annuleren
|
||||||
4
client/src/main/resources/locale/lang_pl.properties
Normal file
4
client/src/main/resources/locale/lang_pl.properties
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
add.recipe.title=Utwórz przepis
|
||||||
|
add.recipe.label=Nazwa przepisu
|
||||||
|
add.recipe.button.ok=Ok
|
||||||
|
add.recipe.button.cancel=Anuluj
|
||||||
Loading…
Add table
Add a link
Reference in a new issue