改变语言从英语到荷兰语

时间:2014-11-28 13:23:34

标签: java javafx

当我刷新屏幕(java和javaFX)时,我想将我的应用程序的语言从英语更改为荷兰语。有谁知道从哪里开始或者有更改应用程序中的语言的功能?

1 个答案:

答案 0 :(得分:1)

您应该使用ResourceBundle documentation中描述的命名机制将所有字符串放在属性文件中。

然后,您可以创建ObjectProperty<Locale>来表示当前区域设置(即语言),并根据此区域设置将UI中的所有字符串绑定到适当的值。你可能想要一个单独的类来处理这个:这是一个简单的例子。

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

import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

public class LocalizedBinding {

    // property representing the current locale:
    private final ObjectProperty<Locale> locale ;

    // private property to hold the resource bundle:
    private final ObjectProperty<ResourceBundle> bundle ;

    public LocalizedBinding(String bundleName, Locale locale) {

        this.locale = new SimpleObjectProperty<>(locale);
        this.bundle = new SimpleObjectProperty<>();

        // update resource bundle whenever locale changes:
        bundle.bind(Bindings.createObjectBinding(() -> {
                Locale l = this.locale.get();
                if (l == null) {
                    return null ;
                } else {
                    ResourceBundle resources = ResourceBundle.getBundle(bundleName, l);
                    return resources;
                }
            },          
            this.locale));
    }   

    // creates a StringBinding whose value is obtained from the current
    // resource bundle using the provided key. The binding will automatically
    // update if the locale changes:

    public StringBinding createStringBinding(String key) {
        return new StringBinding() {

            {
                bind(bundle);
            }

            @Override
            protected String computeValue() {
                ResourceBundle resources = bundle.get();
                if (resources == null) {
                    return key ;
                } else {
                    return resources.getString(key);
                }
            }

        };
    }

    // Property accessors for locale:

    public final ObjectProperty<Locale> localeProperty() {
        return this.locale;
    }

    public final java.util.Locale getLocale() {
        return this.localeProperty().get();
    }

    public final void setLocale(final java.util.Locale locale) {
        this.localeProperty().set(locale);
    }
}

以下是使用此功能的简单示例:

import java.util.Locale;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class SwitchableLanguageTest extends Application {

    @Override
    public void start(Stage primaryStage) {

        // Combo box for language selection:
        ComboBox<Locale> combo = new ComboBox<>();
        combo.getItems().addAll(Locale.getDefault(), new Locale("nl"));

        // display each language in the actual language:
        combo.setCellFactory(lv -> createListCell());
        combo.setButtonCell(createListCell());

        Label greetingLabel = new Label();

        // Create a localizedBinding object for the bundle resources/greetings 
        LocalizedBinding localizedBinding = new LocalizedBinding(
                "resources/greetings", Locale.getDefault());

        // update the localizedBinding's locale when the combo box value changes:
        localizedBinding.localeProperty().bind(combo.valueProperty());

        // bind the label's text:
        greetingLabel.textProperty().bind(
                localizedBinding.createStringBinding("greeting"));

        combo.getSelectionModel().select(Locale.getDefault());

        BorderPane root = new BorderPane(greetingLabel, combo, null, null, null);
        BorderPane.setAlignment(combo, Pos.CENTER);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private ListCell<Locale> createListCell() {
        return new ListCell<Locale>() {
            @Override
            public void updateItem(Locale locale, boolean empty) {
                super.updateItem(locale, empty);
                if (empty) {
                    setText("");
                } else {
                    setText(locale.getDisplayLanguage(locale));
                }
            }
        };
    }

    public static void main(String[] args) {
        launch(args);
    }
}

两个属性文件(在资源目录中)是

greetings.properties:

greeting=Hello

和 greetings_nl.properties:

greeting=Hallo