Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localizing JavaFx Controls

I am trying add a ResourceBundle for my language for the JavaFx controls but am failing to do so.

I tried to add controls_fi_FI.properties in the classpath.. and hoped it would work, but no.

So then I looked at JavaFx's ControlResources class, which is responsible for localizing the controls, and I noticed that the basename of the bundle is "com\sun\javafx\scene\control\skin\resources\controls".

How can I add my properties-file to this bundle? I find it strange that there is practically no information anywhere on how to do this?

like image 639
user2499946 Avatar asked Sep 11 '14 15:09

user2499946


1 Answers

I can only get this to work via a pretty massive hack, that isn't even remotely portable. However, maybe it will give you enough to work from to create a viable solution. (This works under Java 8.)

I created a package com.sun.javafx.scene.control.skin.resources, and created a controls_fi_FI.properties file in it with the single line

ProgressIndicator.doneString=Valmis

I created a test app with the following:

import java.util.Locale;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
            System.out.println(ResourceBundle.getBundle("com/sun/javafx/scene/control/skin/resources/controls").getString("ProgressIndicator.doneString"));

            BorderPane root = new BorderPane();
            ProgressIndicator progressIndicator = new ProgressIndicator(1);
            root.setCenter(progressIndicator);
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
    }

    public static void main(String[] args) {
        Locale.setDefault(new Locale("fi", "FI"));
        launch(args);
    }
}

Running this app most ways doesn't work as desired: while the System.out.println(...) produces the correct result, the text displayed in the progress indicator is wrong.

However, if I bundle com/sun/javafx/control/skin/resources/controls_fi_FI.properties in a jar file, and move that jar file to the jre/lib/ext subdirectory of my JDK installation (ie. the same location as jfxrt.jar), it runs as required (at least in my simple test; as I said, I make no claims for this to be any kind of robust solution).

The issue appears to be that ControlsResources is being loaded by the extension class loader, and so is using the same class loader to load the resource bundle.

With better knowledge of class loaders than I have, you might be able to mold this into a reasonable solution...

like image 112
James_D Avatar answered Sep 22 '22 19:09

James_D