Language of JavaFX color picker

2020-06-19 08:24发布

问题:

Is there a way to change language of ColorPicker's texts such as "Custom Color...", "Current Color", "New Color", "Hue", "Saturation", "Brightness", "Opacity", "Save", "Use", "Cancel"?

回答1:

EDIT: Below answer is for those who need some more exotic language. If you use one of those: de, es, fr, it, ja, ko, pt, sv, zh @sergey-grinev provided sufficient answer.


I came up with two solutions. Both rely on properties file. You can create your own based on examples found in com/sun/javafx/scene/control/skin/resources/ in jxfrt.jar provided with JRE.

All examples will use polish Locale (new Locale("pl", "PL")) which is not built-in.


Solution 1

Create JAR file with following structure (change suffix accordingly)

com/sun/javafx/scene/control/skin/resources/controls_pl_PL.properties

and place it in

<path_to_JVM>/lib/ext

That's it.

I'm not sure what the license says about placing custom files in com.sun.* packages, so here's another solution.

Solution 2

Create properties file like above, but you can name it whatever and place it wherever you want. Let's say it will be

path/to/my/resources/polish.properties

Create two classes - ResourceBundle.Control and ResourceBundleControlProvider (read more) like this.

public class CustomLocaleFxResourceBundleControl extends ResourceBundle.Control {
    static final String FX_BASE_NAME = "com/sun/javafx/scene/control/skin/resources/controls";
    private static final Locale MY_LOCALE = new Locale("pl", "PL");

    @Override
    public String toBundleName(String baseName, Locale locale) {
        if (FX_BASE_NAME.equals(baseName) && MY_LOCALE.equals(locale))
            return "path/to/my/resources/polish"; // without extension

        return super.toBundleName(baseName, locale);
    }
}
public class CustomLocaleFxResourceBundleControlProvider implements ResourceBundleControlProvider {
    private static final ResourceBundle.Control MY_RESOURCE_BUNDLE_CONTROL = new CustomLocaleFxResourceBundleControl();

    public ResourceBundle.Control getControl(String baseName) {
        if (CustomLocaleFxResourceBundleControl.FX_BASE_NAME.equals(baseName))
            return MY_RESOURCE_BUNDLE_CONTROL;

        return null;
    }
}

Compile those classes and put them in JAR file along with your resource and META-INF folder. META-INF folder should have following structure

META-INF/services/java.util.spi.ResourceBundleControlProvider

java.util.spi.ResourceBundleControlProvider is a text file which only line is path to ResourceBundleControlProvider class. In our case it's just

CustomLocaleFxResourceBundleControlProvider

Complete JAR put in

<path_to_JVM>/lib/ext


回答2:

The easiest way is to use one of predefined locales, e.g. add next line before creating ColorPicker:

Locale.setDefault(Locale.FRENCH);

You'll see next UI: