如何在FXML文档中实现JavaFX的语言支持?

时间:2021-02-28 17:05:06

How can I have different languages for a view in a FXML document to support many countries?

如何在FXML文档中使用不同的语言来支持许多国家/地区?

1 个解决方案

#1


21  

Use ResourceBundles to store the locale-dependent text, and access the data in the bundle using "%resourceKey".

使用ResourceBundles存储与语言环境相关的文本,并使用“%resourceKey”访问包中的数据。

Specifically, create text files for each language you want to support and place them in the classpath. The Javadocs for ResourceBundle have the details on the naming scheme, but you should have a default bundle defined by BaseName.properties and bundles for other languages and variants defined by BaseName_xx.properties. For example (with the resources directory in the root of the classpath):

具体来说,为要支持的每种语言创建文本文件,并将它们放在类路径中。 ResourceBundle的Javadocs有关于命名方案的详细信息,但您应该有BaseName.properties定义的默认包以及BaseName_xx.properties定义的其他语言和变体的包。例如(使用类路径根目录中的resources目录):

resources/UIResources.properties:

资源/ UIResources.properties:

greeting = Hello

resources/UIResources_fr.properties:

资源/ UIResources_fr.properties:

greeting = Bonjour

Then in your FXML file you can do

然后在你的FXML文件中就可以了

<Label text = "%greeting" />

To pass the ResourceBundle to the FXMLLoader do:

要将ResourceBundle传递给FXMLLoader,请执行以下操作:

ResourceBundle bundle = ResourceBundle.getBundle("resources.UIResources");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/path/to/FXML.fxml"), bundle);
Parent root = loader.load();

This code will load the resource bundle corresponding to the default locale (typically the locale you have set at the OS level), falling back on the default if it can't find a corresponding bundle. If you want to force it to use a particular bundle, you can do

此代码将加载与默认语言环境相对应的资源包(通常是您在操作系统级别设置的语言环境),如果找不到相应的包,则返回默认语言环境。如果你想强制它使用特定的捆绑,你可以这样做

ResourceBundle bundle = ResourceBundle.getBundle("/resources/UIResources", new Locale("fr"));

Finally, if you need access to the resource bundle in the FXML controller, you can inject it into a field of type ResourceBundle and name resources:

最后,如果需要访问FXML控制器中的资源包,可以将其注入ResourceBundle类型的字段并命名资源:

public class MyController {

    @FXML
    private ResourceBundle resources ;

    // ...
}

#1


21  

Use ResourceBundles to store the locale-dependent text, and access the data in the bundle using "%resourceKey".

使用ResourceBundles存储与语言环境相关的文本,并使用“%resourceKey”访问包中的数据。

Specifically, create text files for each language you want to support and place them in the classpath. The Javadocs for ResourceBundle have the details on the naming scheme, but you should have a default bundle defined by BaseName.properties and bundles for other languages and variants defined by BaseName_xx.properties. For example (with the resources directory in the root of the classpath):

具体来说,为要支持的每种语言创建文本文件,并将它们放在类路径中。 ResourceBundle的Javadocs有关于命名方案的详细信息,但您应该有BaseName.properties定义的默认包以及BaseName_xx.properties定义的其他语言和变体的包。例如(使用类路径根目录中的resources目录):

resources/UIResources.properties:

资源/ UIResources.properties:

greeting = Hello

resources/UIResources_fr.properties:

资源/ UIResources_fr.properties:

greeting = Bonjour

Then in your FXML file you can do

然后在你的FXML文件中就可以了

<Label text = "%greeting" />

To pass the ResourceBundle to the FXMLLoader do:

要将ResourceBundle传递给FXMLLoader,请执行以下操作:

ResourceBundle bundle = ResourceBundle.getBundle("resources.UIResources");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/path/to/FXML.fxml"), bundle);
Parent root = loader.load();

This code will load the resource bundle corresponding to the default locale (typically the locale you have set at the OS level), falling back on the default if it can't find a corresponding bundle. If you want to force it to use a particular bundle, you can do

此代码将加载与默认语言环境相对应的资源包(通常是您在操作系统级别设置的语言环境),如果找不到相应的包,则返回默认语言环境。如果你想强制它使用特定的捆绑,你可以这样做

ResourceBundle bundle = ResourceBundle.getBundle("/resources/UIResources", new Locale("fr"));

Finally, if you need access to the resource bundle in the FXML controller, you can inject it into a field of type ResourceBundle and name resources:

最后,如果需要访问FXML控制器中的资源包,可以将其注入ResourceBundle类型的字段并命名资源:

public class MyController {

    @FXML
    private ResourceBundle resources ;

    // ...
}