javafx 中的对话框国际化
dialog internationalization in javafx
我在我的代码中发现了一个问题,因为它根据 os 的语言翻译了一些单词(在本例中是一个按钮)。我已经搜索了解决方案,但没有找到适合我的情况。据我所知,捆绑包用于翻译字符串。
这是我的问题:
我的问题是,它没有取消,而是写入 "Annuler",法语单词。
这是对话框的代码:
printerSet.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
ChoiceDialog<String> dialog = new ChoiceDialog<>(
"Dummy Printer", choices);
dialog.setTitle("Choice Dialog");
dialog.setHeaderText(null);
dialog.setContentText("Choose the printer you want to use:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String opt = result.get();
System.out.println("Your choice: " + opt);
printerLabel.setText("Selected Printer: " + opt);
}
printButton.setDisable(true);
name.setText("");
code.setText("");
description.setText("");
availability.setText("");
}
});
有人知道解决办法吗?
尝试在启动时提供以下 JVM 参数:
java -Duser.language=en -Duser.country=US ...
您可以手动添加按钮:
MVCE:
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class MCVE extends Application {
@Override
public void start(Stage stage) {
ChoiceDialog<String> dialog = new ChoiceDialog<>(
"Dummy Printer");
dialog.setTitle("Choice Dialog");
dialog.setHeaderText(null);
dialog.setContentText("Choose the printer you want to use:");
// Remove the default buttons and then add your custom ones.
dialog.getDialogPane().getButtonTypes().clear();
dialog.getDialogPane().getButtonTypes().add(
new ButtonType("OK", ButtonData.OK_DONE));
dialog.getDialogPane().getButtonTypes().add(
new ButtonType("Cancel", ButtonData.CANCEL_CLOSE));
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String opt = result.get();
System.out.println("Your choice: " + opt);
}
}
public static void main(String[] args) {
launch();
}
}
这也可以在运行时通过在 Application
class.
的 main
方法中使用 Locale.setDefault(locale)
来实现
例如:
public class App extends Application {
public static void main(String[] args) {
Locale.setDefault(Locale.ENGLISH);
try {
launch(args);
} catch (Throwable e) {
// Handle error
}
}
}
在调用 Application.launch()
后再次调用 Locale.setDefault(locale)
,对对话框按钮文本没有影响。
对于问题和问题中的问题
controlsfx 向导中的变音符号
见https://bitbucket.org/controlsfx/controlsfx/issues/769/encoding-problem-all-german-umlauts-are
我正在使用以下方法:
更改语言环境后,我在向导窗格上调用了 refreshI18n(),为此我使用派生的 WizardPane。 refreshI18n() 将调用 fixButtons() 并根据设置的语言环境重新设置按钮文本。
主要问题是找到控件并重置文本,例如对于按钮
/**
* https://bitbucket.org/controlsfx/controlsfx/issues/769/encoding-problem-all-german-umlauts-are
*
* @param wizardPane
*/
protected void fixButtons() {
ButtonType buttonTypes[] = { ButtonType.NEXT, ButtonType.PREVIOUS,
ButtonType.CANCEL, ButtonType.FINISH };
for (ButtonType buttonType : buttonTypes) {
Button button = findButton(buttonType);
if (button != null) {
button.setText(buttonType.getText());
}
}
}
/**
* get the Button for the given buttonType
* @return the button
*/
public Button findButton(ButtonType buttonType) {
for (Node node : getChildren()) {
if (node instanceof ButtonBar) {
ButtonBar buttonBar = (ButtonBar) node;
ObservableList<Node> buttons = buttonBar.getButtons();
for (Node buttonNode : buttons) {
Button button = (Button) buttonNode;
@SuppressWarnings("unchecked")
ObjectProperty<ButtonData> prop = (ObjectProperty<ButtonData>) button
.getProperties().get("javafx.scene.control.ButtonBar.ButtonData");
ButtonData buttonData = prop.getValue();
if (buttonData.equals(buttonType.getButtonData())) {
return button;
}
}
}
}
return null;
}
我在我的代码中发现了一个问题,因为它根据 os 的语言翻译了一些单词(在本例中是一个按钮)。我已经搜索了解决方案,但没有找到适合我的情况。据我所知,捆绑包用于翻译字符串。
这是我的问题:
我的问题是,它没有取消,而是写入 "Annuler",法语单词。
这是对话框的代码:
printerSet.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
ChoiceDialog<String> dialog = new ChoiceDialog<>(
"Dummy Printer", choices);
dialog.setTitle("Choice Dialog");
dialog.setHeaderText(null);
dialog.setContentText("Choose the printer you want to use:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String opt = result.get();
System.out.println("Your choice: " + opt);
printerLabel.setText("Selected Printer: " + opt);
}
printButton.setDisable(true);
name.setText("");
code.setText("");
description.setText("");
availability.setText("");
}
});
有人知道解决办法吗?
尝试在启动时提供以下 JVM 参数:
java -Duser.language=en -Duser.country=US ...
您可以手动添加按钮:
MVCE:
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class MCVE extends Application {
@Override
public void start(Stage stage) {
ChoiceDialog<String> dialog = new ChoiceDialog<>(
"Dummy Printer");
dialog.setTitle("Choice Dialog");
dialog.setHeaderText(null);
dialog.setContentText("Choose the printer you want to use:");
// Remove the default buttons and then add your custom ones.
dialog.getDialogPane().getButtonTypes().clear();
dialog.getDialogPane().getButtonTypes().add(
new ButtonType("OK", ButtonData.OK_DONE));
dialog.getDialogPane().getButtonTypes().add(
new ButtonType("Cancel", ButtonData.CANCEL_CLOSE));
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String opt = result.get();
System.out.println("Your choice: " + opt);
}
}
public static void main(String[] args) {
launch();
}
}
这也可以在运行时通过在 Application
class.
main
方法中使用 Locale.setDefault(locale)
来实现
例如:
public class App extends Application {
public static void main(String[] args) {
Locale.setDefault(Locale.ENGLISH);
try {
launch(args);
} catch (Throwable e) {
// Handle error
}
}
}
在调用 Application.launch()
后再次调用 Locale.setDefault(locale)
,对对话框按钮文本没有影响。
对于问题和问题中的问题 controlsfx 向导中的变音符号
见https://bitbucket.org/controlsfx/controlsfx/issues/769/encoding-problem-all-german-umlauts-are
我正在使用以下方法: 更改语言环境后,我在向导窗格上调用了 refreshI18n(),为此我使用派生的 WizardPane。 refreshI18n() 将调用 fixButtons() 并根据设置的语言环境重新设置按钮文本。
主要问题是找到控件并重置文本,例如对于按钮
/**
* https://bitbucket.org/controlsfx/controlsfx/issues/769/encoding-problem-all-german-umlauts-are
*
* @param wizardPane
*/
protected void fixButtons() {
ButtonType buttonTypes[] = { ButtonType.NEXT, ButtonType.PREVIOUS,
ButtonType.CANCEL, ButtonType.FINISH };
for (ButtonType buttonType : buttonTypes) {
Button button = findButton(buttonType);
if (button != null) {
button.setText(buttonType.getText());
}
}
}
/**
* get the Button for the given buttonType
* @return the button
*/
public Button findButton(ButtonType buttonType) {
for (Node node : getChildren()) {
if (node instanceof ButtonBar) {
ButtonBar buttonBar = (ButtonBar) node;
ObservableList<Node> buttons = buttonBar.getButtons();
for (Node buttonNode : buttons) {
Button button = (Button) buttonNode;
@SuppressWarnings("unchecked")
ObjectProperty<ButtonData> prop = (ObjectProperty<ButtonData>) button
.getProperties().get("javafx.scene.control.ButtonBar.ButtonData");
ButtonData buttonData = prop.getValue();
if (buttonData.equals(buttonType.getButtonData())) {
return button;
}
}
}
}
return null;
}