AdamBien / afterburner.fx

The Opinionated Un-Framework For Java FX Applications
http://afterburner.adam-bien.com
278 stars 82 forks source link

set different ResourceBundle for different Views #64

Open flysoftsystems opened 8 years ago

flysoftsystems commented 8 years ago

Hi Adam,

I really appreciate your work with this small but very relevant DI plugin. I am working with internationalized strings both with Netbeans 8.1 and SceneBuilder. My app has many users and each user would want the Views display in his native language, but I don't know how to attach user-specific Locales to ResourceBundles which in turn can be injected into the Views.

With the standard javafx, I could change the language like this:

  private void loadView(Locale locale) {
    try {
      FXMLLoader fxmlLoader = new FXMLLoader();

      // Here, just the resource bundles name is mentioned. You can add support for more languages
      // by adding more properties-files with language-specific endings like
      // "E_13_Internationalization_fr.properties".
      fxmlLoader.setResources(ResourceBundle.getBundle("E_13_Internationalization", locale));

      Pane pane = (BorderPane) fxmlLoader.load(this.getClass().getResource("/E_13_Internationalization.fxml").openStream());
      borderPane.setCenter(pane);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

Is there an option to set resources within the View or any other solution would be greatly appreciated!

Thank you for your time, Cheng

mavek87 commented 8 years ago

Hello, I can't understand how can I set the locale of resourceBundle used by the views, and if it's possible to change it at runtime. Can you suggest me a way to do it pleese?

Thanks in advance for your help, Matteo

flysoftsystems commented 8 years ago

Hi Matteo,

until now I haven't gotten a reply from Adem Bien. But Peter Rogge who is responsible for the Netbeans AfterburnerFX plugin gave me a good hint how to implement this. You have to understand that currently AfterburnerFX doesn't support customized Locale inserts during the creation of a view. Instead the view fetches the Locale from Locale.getDefault() automatically during instantiation. Consequently you need to Locale.setDefault() before the new View () statement. Locale.setDefault(customLocale); MyView view = new MyView(); Locale.setDefault(standardLocale);

Moreover with FXML loading of resourceBundles switching resourceBundle on the flyis not possible. If you want your Scene to be shown with a different Locale then you have to recreate the view based on the new Locale like this:

public void showMyView() {
        // first initiation
        if (myStage == null) {

            myStage = new Stage();

            Locale.setDefault(userLocale);
            view = new MyView();
            Locale.setDefault(standardLocale);

            Scene scene = new Scene(view.getView());
            myStage.setScene(scene);

            // after first time init checking whether Locale has changed
        } else {
            if (view.getRealPresenter().getLocale() != view.getResourceBundle().getLocale()) {

                System.out.println("Locale has changed");
                System.out.println("Old Locale is: " + view.getResourceBundle().getLocale());
                System.out.println("New Locale is: " + view.getRealPresenter().getLocale());

                // recreating view after Locale has changed
                Locale.setDefault(view.getRealPresenter().getLocale());
                view = new MyView();
                Locale.setDefault(standardLocale);

                Scene scene = new Scene(view.getView());

                myStage.setScene(scene);
            }
        }

        myStage.showAndWait();
    }
ridi-mz commented 8 years ago

Hi, I'm having trouble to implement the internationalization system in an efficient way with the afterburner. My application has multiple views, and i want to put

language_en.properties

and

language_it.properties

files in Other sources Package. Anyone can provide an exemple ?

flysoftsystems commented 8 years ago

hi there. For afterburnerFX one has to keep View, Presenter and all its language property files within the same package. Since afterburnerFX runs as "Convention over Configuration", one has to follow its naming conventions. For example:

So if you want to create a new view called "home", then you need to create a package called "home", presenter called "HomePresenter", view called "HomeView", fxml called home.fxml etc. In case your IDE is Netbeans you can make use of an excellent plugin by Peter Rogge called Netbeans Afterburnerfx plugin, to be found here: link; it will help you create these files automatically once you.

Be advised that all files related to transfer must be located within the transfer package! Cheers, Mate