更改 JavaFX 8 DatePicker 中的语言 [英] Changing the language in JavaFX 8 DatePicker

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

问题描述

将 DatePicker 添加到我的应用时,我得到以下信息:

When adding a DatePicker to my app I get the following:

我认为这是因为我在计算机上使用希伯来语.如何将 DatePicker 的语言更改为英语?

I assume this is because I use Hebrew on my computer. How can I change the language of the DatePicker to be English?

推荐答案

您可以为 Java 虚拟机调用的实例定义默认语言环境:

You can define the default locale for your instance of the Java Virtual Machine calling:

Locale.setDefault(Locale.ENGLISH);

或者如果你找不到locale,你需要,在预制的常量中,你可以在官方支持的语言环境 并像这样创建您的自定义"语言环境:

Or if you can't find the locale, you need, in the pre made constants, you can look up the country code in the list of officially supported locales and create your "custom" locale like this:

Locale.setDefault(Locale("cs")) //locale for Czech language

关于 start 方法.如果您还想为文本编辑器实现自定义格式化程序,您也应该向格式化程序添加语言环境.

on the start method. If you also want to implement a custom formatter for the text editor, you should add locale to the formatter too.

这只是一个例子:

private final DateTimeFormatter formatter = 
        DateTimeFormatter.ofPattern("EEEE, d.MM.uuuu", Locale.ENGLISH);

@Override
public void start(Stage primaryStage) {
    Locale.setDefault(Locale.ENGLISH);

    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setConverter(new StringConverter<LocalDate>() {

        @Override
        public String toString(LocalDate object) {
            return object.format(formatter);
        }

        @Override
        public LocalDate fromString(String string) {
            return LocalDate.parse(string, formatter);
        }
    });
    StackPane root = new StackPane(datePicker);
    Scene scene = new Scene(root, 400, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}

编辑

按照设计,DatePicker 使用 Locale.getDefault() 应用于弹出窗口中显示的控件的所有格式.这可以在 com.sun.javafx.scene.control.skin.DatePickerContent 类中检查.

By design, DatePicker uses Locale.getDefault() in all the formats applied to the controls displayed on the popup. This can be checked in com.sun.javafx.scene.control.skin.DatePickerContent class.

除非您为更改这些格式化程序的控件提供自定义外观,以便将 DatePicker 内容更改为英语,避免其他本地化控件的进一步更改,解决方法可能是:

Unless you provide a custom skin for the control changing these formatters, in order to change the DatePicker content to English, avoiding further changes in other localized controls, a workaround could be this:

private final Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);

@Override
public void start(Stage primaryStage) {
    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setOnShowing(e-> Locale.setDefault(Locale.Category.FORMAT,Locale.ENGLISH));
    datePicker.setOnShown(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    ...
}

编辑 2

返回到 setOnShown 上的原始语言环境为时过早,因为如果用户更改月份,则会使用原始语言环境并且无法正确显示.要工作,它应该在 setOnHidingsetOnAction 上关闭.

Returning to the original locale on setOnShown is too soon, since if the user changes the month, the original locale is used and it will not be shown properly. To work it should be turned off both on setOnHiding and on setOnAction.

private final Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);

@Override
public void start(Stage primaryStage) {
    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setOnShowing(e-> Locale.setDefault(Locale.Category.FORMAT,Locale.ENGLISH));
    datePicker.setOnHiding(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    datePicker.setOnAction(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    ...
}

这篇关于更改 JavaFX 8 DatePicker 中的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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