gpt4 book ai didi

java - 将语言从英语更改为荷兰语

转载 作者:行者123 更新时间:2023-12-01 12:12:42 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

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

问候语属性:

greeting=Hello

和greetings_nl.properties:

greeting=Hallo

关于java - 将语言从英语更改为荷兰语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27189661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com