Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set different ResourceBundle for different Views #64

Open
flysoftsystems opened this issue Jun 4, 2016 · 4 comments
Open

set different ResourceBundle for different Views #64

flysoftsystems opened this issue Jun 4, 2016 · 4 comments

Comments

@flysoftsystems
Copy link

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
Copy link

mavek87 commented Jun 22, 2016

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
Copy link
Author

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
Copy link

ridi-mz commented Aug 10, 2016

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
Copy link
Author

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:

  • package-name: transfer
  • presenter-name: TransferPresenter.java
  • view-name: TransferView.java
  • fxml-name: transfer.fxml
  • language-properties: transfer_fr.properties
  • language-properties: transfer_en.properties
  • language-properties: transfer_de.properties

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants