Updating the language of a Javafx application at runtime

Asked

Viewed 102 times

1

I have a combobox that contains the languages, while selecting it stores and I would like to already apply the language update.

But I wanted to do it dynamically. The problem is that I don’t know how to change the language.

This method is called when the combo option is selected

 languages.valueProperty().addListener(new ChangeListener<Locale>() {
            @Override
            public void changed(ObservableValue<? extends Locale> observable, Locale oldValue, Locale newValue) {
                LanguageEnum le = CommonService.fromLocale(newValue);
                try {
                    CommonService.setLanguage(le);
                    refreshRender(CommonService.getBundle());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

In this scenario I would render the open screens with the selected language, the intention was to see which Cene was visible, and change their language, however I would need to know the fxml that was loaded to load again.

I don’t know if it is this way, and I don’t know how to conclude, because I couldn’t find a method to recover the URL of the instance of Scene that was used, I don’t know if this is the best way, the goal is to change the language based on the language selected in the combo and already apply the translation on the screen. I started studying Javafx last week so I’m pretty lost.

   private void refreshRender(ResourceBundle bundle) {
        ObservableList<Stage> stages = StageHelper.getStages();
        for (Stage s : stages) {
            if (!s.isShowing()) {
                continue;
            }
            Scene scene = s.getScene();
            Parent root = scene.getRoot();
    FXMLLoader.load(getClass().getResource("xxxx.fxml"), bundle);
        }
    }
  • This Soen topic may resolve your question: https://stackoverflow.com/documentation/javafx/5434/internationalization-in-javafx#t=20170813200741721983

  • It is a solution, only if I understood correctly, in this case it will no longer use fxml started to create the elements directly via code using a Factory

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.