Skip to content

resource bundles i18n

Manuel Mauky edited this page Jun 12, 2018 · 5 revisions

Internationalization and ResourceBundles are part of every big application. MvvmFX supports developers with the handling of resourceBundles.

Global ResourceBundles

You can define a global ResourceBundle that is used for all views in the whole application. This is done with the static method MvvmFX.setGlobalResourceBundle(ResourceBundle bundle).

public class MyApp extends Application {

    public void start(Stage stage) throws Exception {
        ResourceBundle bundle = ResourceBundle.getBundle("myapp");

        MvvmFX.setGlobalResourceBundle(bundle);

        Parent root = FluentViewLoader.fxmlView(MyView.class).load().getView();
        stage.setScene(new Scene(root));
        stage.show();
    }
}

Provide ResourceBundle when loading a view.

The fluent API of the FluentViewLoader provides the possibility to use a resourceBundle for the view that is loaded.

FluentViewLoader.fxmlView(MyView.class).resourceBundle(bundle).load();

This bundle will be merged with the global resourceBundle (if any) so that a view has access to both the global resources and the per-view resources. Durring the merge the per-view resourceBundle has a higher priority then the global one. This means that if there is a value with the key "mykey" in the global and in the per-view bundle, the value from the global bundle will be lost and only the value from the per-view bundle will be available.

Use a specific ResourceBundle for an included FXML file.

With JavaFX it is possible to define a ResourceBundle when including an FXML file in another FXML file (using <fx:include ...>tag). In principle this is also possible with mvvmFX. However, for the mvvmFX-framework there is no way to interfere the loading-process of this resourceBundle and therefore there is no support for merging the resourceBundle with the global resourceBundle.

<fx:include source="../menu/MenuView.fxml" resources="menu"/>

Additionally, it's not possible to inject such a resourceBundle into the View or ViewModel via @InjectResourceBundle (see the next chapter for more details). However, you can still use the normal JavaFX naming-convention to inject the ResourceBundle into the View class by including a field for the ResourceBundle with the name "resources".

Injection of ResourceBundles

Like with standard JavaFX it is possible to inject the ResourceBundle in the "controller" class (in mvvmFX terms the "view" class or "codeBehind"). This is possible with mvvmFX too.

Additionally you can inject the ResourceBundle into your ViewModel. To do this you need to use the annotation @InjectResourceBundle.