本地化JavaFx控件 [英] Localizing JavaFx Controls

查看:105
本文介绍了本地化JavaFx控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的语言为JavaFx控件添加一个ResourceBundle但是我没有这样做。

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

我试图在类路径中添加controls_fi_FI.properties。希望它能工作,但不行。

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

然后我查看了JavaFx的ControlResources类,它负责本地化控件,我注意到了bundle的基本名称是com\sun\javafx\scene\control\skin\resources\controls。

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?

推荐答案

我只能让这个通过一个相当大的黑客,甚至是远程便携。但是,也许它会给你足够的工作来创建一个可行的解决方案。 (这适用于Java 8。)

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.)

我创建了一个包 com.sun.javafx.scene.control.skin.resources ,并用单行创建一个controls_fi_FI.properties文件

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);
    }
}

运行此应用程序大多数方式无法正常工作:当 System.out.println(...)生成正确的结果时,进度指示器中显示的文本是错误的。

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.

但是,如果我在jar文件中捆绑 com / sun / javafx / control / skin / resources / controls_fi_FI.properties ,并将该jar文件移动到我的JDK安装的 jre / lib / ext 子目录(即与 jfxrt.jar 相同的位置),它根据需要运行(至少在我的简单测试中;正如我所说,我没有声称这是任何类型的强大解决方案)。

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).

问题似乎是扩展类加载器正在加载 ControlsResources ,因此使用相同的类加载器来加载资源包。

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...

这篇关于本地化JavaFx控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆