Here is my StartApp.java
, entry point of my application.
public class StartApp extends Application {
private Locale locale = new Locale("en");
public Locale getLocale(){
return locale;
}
public void setLocale(Locale locale){
this.locale = locale;
}
@Override
public void start(Stage primaryStage) throws Exception{
ResourceBundle bundle = ResourceBundle.getBundle("resources.bundles/MyBundle", locale);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/LoginView.fxml"), bundle);
Parent root = fxmlLoader.load();
primaryStage.setTitle("Title");
primaryStage.setScene(new Scene(root, 750, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) throws SQLException {
launch(args);
}
Then on LoginController.java
I create instance of StartApp and set onActions for 2 buttons
StartApp startApp = new StartApp();
@Override
public void initialize(URL location, ResourceBundle resources) {
bundle = resources;
plBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
startApp.setLocale(new Locale("pl"));
changeLanguage(event);
} catch (Exception e) {
e.printStackTrace();
}
}
});
enBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
startApp.setLocale(new Locale("en"));
changeLanguage(event);
} catch (Exception e) {
e.printStackTrace();
}
}
});
here is my changeLanguage
method, which refresh current window and changes its language
public void changeLanguage(ActionEvent event) throws Exception{
((Node)event.getSource()).getScene().getWindow().hide();
Stage primaryStage = new Stage();
ResourceBundle bundle = ResourceBundle.getBundle("resources.bundles/MyBundle", startApp.getLocale());
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/LoginView.fxml"), bundle);
Parent root = fxmlLoader.load();
primaryStage.setTitle("Title2");
primaryStage.setScene(new Scene(root, 750, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
And till now everything works fine, it changes language once I click buttons. But what I want to do now is to open new window(stage) with choosen language, but unfortunatelly, it always open new scene with language set on StartApp.
Here is method in LoginController
than opens new stage.
public void register(ActionEvent event) throws Exception{
((Node)event.getSource()).getScene().getWindow().hide();
Stage primaryStage = new Stage();
ResourceBundle bundle = ResourceBundle.getBundle("resources.bundles/MyBundle", startApp.getLocale());
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/RegisterView.fxml"), bundle);
Parent root = fxmlLoader.load();
primaryStage.setTitle("Title2");
primaryStage.setScene(new Scene(root, 750, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
Btw. Iv tried just extending StartApp to LoginController, making locale public, etc, everytime it ends up the same. When I created
Locale newLocale = null;
in LoginController
, and then tried to assign values to it once I click buttons defined in initialize
, I got nullpointerexception.
I don't know how to change all text by one command. To keep it simlpe i just rename each element separately.
Example:
i18n/messages.properties
i18n/messages_ukr.properties
i18n/messages_rus.properties
Singleton Context.java which will contain current locale:
Util class MessageUtil.java will return message in current locale (seted in Context):
I use SceneBuilder tool for creating *.fxml files. For example screen controller for one of this files that contain Label and ChoiceBox for language change might looks like following: