Java 7 默认语言环境

发布于 2024-11-30 00:45:22 字数 410 浏览 0 评论 0原文

我刚刚安装了 jre7,我很惊讶地发现我的默认区域设置现在是 en_US。对于jre6,它是de_CH。

与jre7有什么不同?默认区域设置不再是操作系统之一吗? (顺便说一句,我使用的是 Windows7)

谢谢您的回答。

编辑: 我看到 Category.FORMAT 的区域设置是“旧”区域设置 (de_CH)。 Category.DISPLAY 的区域设置从操作系统的语言中获取语言(在 Windows 中,这是在“控制面板”>“区域和语言”>“键盘和语言”>“显示语言”中完成的)以及来自...?

似乎不同的是属性“user.country”。使用 Java6,我得到“CH”,使用 Java7,我得到“US”。

I have just installed jre7 and I'm surprised to see that my default locale is now en_US. With jre6 it was de_CH.

What is different with jre7? Is the default locale no more the one of the Operating System? (btw, I'm using Windows7)

Thx for your answer.

Edit:
I have seen the Locale for Category.FORMAT is the "old" one (de_CH). The Locale for Category.DISPLAY takes the language from the language of the OS (in Windows this is done in Control Panel > Region and Language > Keyboard and Languages > Display Language) and the contry from...?

What seems to be different is the property "user.country". With Java6 I get "CH" and with Java7 I get "US".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

转角预定愛 2024-12-07 00:45:23

这是设计好的。 Java 7 改变了 Locale.getDefault() 的工作方式。 Oracle 已输入一个缺陷,但他们基本上说这是按设计的。

综上所述,必须修改操作系统的显示语言。仅修改区域格式已经不够了。

请阅读此处的错误报告:Locale.getDefault() 返回错误的 Java SE 7 语言环境

This is as designed. Java 7 has changed the way Locale.getDefault() works. A defect has been entered with Oracle, but they basically said this is As Designed.

To sum up, you must modify the Display Language of the OS. Modifying the Region Format only is no longer sufficient.

Read the bug report here: Locale.getDefault() returns wrong Locale for Java SE 7

暗恋未遂 2024-12-07 00:45:23

此更改很好地描述了 博客文章兼容性页面

请注意,您可以通过将 sun.locale.formatasdefault 系统属性设置为 true 来恢复到旧的行为。

The change is described quite well in this blog post and on the compatibility page.

Note that you can revert to the old behavior by setting the sun.locale.formatasdefault system property to true.

苦行僧 2024-12-07 00:45:23

Java 7 中的区域设置似乎有一些变化,即 UI 和“用户”区域设置之间的区别。请参阅。有 现在 setDefault(Locale.Category, Locale)。但是,这并不能真正解释您所经历的情况 - 我只是指出 Java 7 中有关区域设置处理的更改这一事实。

There seems to be some changes regarding Locale in Java 7, namely differentiation between UI and 'user' locale. See this. There is now setDefault(Locale.Category, Locale). However, this does not really explain what you are experiencing - I'm merely pointing out the fact that there has been changes in Java 7 regarding locale handling.

单调的奢华 2024-12-07 00:45:23

这对我来说确实看起来像一个错误:

public static void main(String[] args) throws FileNotFoundException, IOException {

System.err.println(Locale.getDefault());
}

用 java 5 或 java 6 打印运行它:'nl_NL'
java7: 'en_US'

This really looks like a bug to me:

public static void main(String[] args) throws FileNotFoundException, IOException {

System.err.println(Locale.getDefault());
}

running this with java 5 or java 6 prints: 'nl_NL'
java7: 'en_US'

小女人ら 2024-12-07 00:45:23

如何在程序开始时按照以下方式设置您的区域设置,具体取决于 java 版本:

public class LocaleFormatter {

private static Locale locale;

private LocaleFormatter() {

}

public static Locale setDefaultLocale() {
    if (locale == null) {
        if (!System.getProperty("java.version").startsWith("1.7.")) {
            locale = Locale.getDefault();
        } else {
            try {
                Class localeClass = Class.forName("java.util.Locale");
                Class categoryClass = Class.forName("java.util.Locale$Category");
                Object format = null;
                for (Object constant : categoryClass.getEnumConstants()) {
                    if (constant.toString().equals("FORMAT")) {
                        format = constant;
                    }
                }
                Method method = localeClass.getMethod("getDefault", categoryClass);

                locale = (Locale) method.invoke(Locale.getDefault(), format);
                Locale.setDefault(locale);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    return locale;
}

}

What about setting your Locale at the start of the program in the following way, depending on java version:

public class LocaleFormatter {

private static Locale locale;

private LocaleFormatter() {

}

public static Locale setDefaultLocale() {
    if (locale == null) {
        if (!System.getProperty("java.version").startsWith("1.7.")) {
            locale = Locale.getDefault();
        } else {
            try {
                Class localeClass = Class.forName("java.util.Locale");
                Class categoryClass = Class.forName("java.util.Locale$Category");
                Object format = null;
                for (Object constant : categoryClass.getEnumConstants()) {
                    if (constant.toString().equals("FORMAT")) {
                        format = constant;
                    }
                }
                Method method = localeClass.getMethod("getDefault", categoryClass);

                locale = (Locale) method.invoke(Locale.getDefault(), format);
                Locale.setDefault(locale);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    return locale;
}

}

要走干脆点 2024-12-07 00:45:23

检查 Windows 控制面板区域和语言选项中的“位置”设置(德语:“Region und Sprache”、“Aufenthaltsort”)。

Check the setting "Location" in Windows control panel Regional and language options (German: "Region und Sprache", "Aufenthaltsort").

娇俏 2024-12-07 00:45:23

如果您足够勇敢,您可以调用:

Locale.setDefault(Locale.getDefault());

这会为这两个类别设置默认区域设置

public static synchronized void setDefault(Locale newLocale) {
   setDefault(Category.DISPLAY, newLocale);
   setDefault(Category.FORMAT, newLocale);
   defaultLocale = newLocale;
}

但这当然可能会导致副作用。

If you are brave enough you can call the :

Locale.setDefault(Locale.getDefault());

This sets the default Locale for both of those categories

public static synchronized void setDefault(Locale newLocale) {
   setDefault(Category.DISPLAY, newLocale);
   setDefault(Category.FORMAT, newLocale);
   defaultLocale = newLocale;
}

But this of course could cause side effects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文