将语言从英语更改为荷兰语 [英] changing language from english to dutch

查看:87
本文介绍了将语言从英语更改为荷兰语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在刷新屏幕(java和javaFX)时将App的语言从英语更改为荷兰语.有人知道从哪里开始,或者是否有可以在应用程序中更改语言的功能?

I would like to change the language of my App from english to dutch when I refresh my screen (java and javaFX). Does anyone know where to start or wether there is a function to change the language within an app?

推荐答案

您应该使用

You should put all the strings in properties files, using the naming mechanism described in the ResourceBundle documentation.

然后您可以创建一个 ObjectProperty< Locale> 来表示当前语言环境(即语言),然后根据该语言环境将UI中的所有字符串绑定到适当的值.您可能想要一个单独的类来处理此问题:这是一个简单的示例.

You can then create an ObjectProperty<Locale> to represent the current locale (i.e. language), and bind all the strings in your UI to the appropriate value based on this locale. You probably want a separate class to handle this: here's a simple example.

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

这是一个使用此示例的简单示例:

Here's a quick example using this:

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

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

The two properties files (in the resources directory) are

greetings.properties:

greetings.properties:

greeting=Hello

和greetings_nl.properties:

and greetings_nl.properties:

greeting=Hallo

这篇关于将语言从英语更改为荷兰语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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