Linux Locale.getDefault() 总是返回 en

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10707238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:26:21  来源:igfitidea点击:

Locale.getDefault() returns en always

javalinuxunixlocale

提问by Delta

Servers on unix machine are always using en as default locale. Following is locale output

unix 机器上的服务器总是使用 en 作为默认语言环境。以下是语言环境输出

 LANG=en_US
 LC_CTYPE="C"
 LC_NUMERIC="C"
 LC_TIME="C"
 LC_COLLATE="C"
 LC_MONETARY="C"
 LC_MESSAGES="C"
 LC_ALL=C

I just don't understand if LANG is set correctly then why servers starts with en locale.

我只是不明白 LANG 是否设置正确,那么为什么服务器以 en locale 开头。

采纳答案by wkl

In Linux/Unix/Mac, the settings LC_ALLand LANGcan control the default locale for Java programs. In Windows, the locales are set from the Control Panel, under Regional and Language Options.

在 Linux/Unix/Mac 中,设置LC_ALLLANG可以控制 Java 程序的默认语言环境。在 Windows 中,区域设置是从控制面板的区域和语言选项下设置的。

When the JVM starts in a *nix environment, it will do this:

当 JVM 在 *nix 环境中启动时,它将执行以下操作:

  • Scan the environment for LC_ALL
  • If LC_ALLdoesn't exist, scan the environment for LANG
  • If the JVM setting user.languageis set, use that in place of the environment variables.
  • If nothing is set, default to en_US(I believe this is the final failure case)
  • 扫描环境 LC_ALL
  • 如果LC_ALL不存在,请扫描环境LANG
  • 如果设置了 JVM 设置user.language,请使用它代替环境变量。
  • 如果没有设置,默认为en_US(我相信这是最后的失败案例)

In your environment, you have LC_ALLset to C, which is just the C locale. It's basically a traditional fallback to the days when locales weren't used.

在您的环境中,您已LC_ALL设置为C,这只是 C 语言环境。这基本上是对不使用语言环境的日子的传统回退。

You can change LC_ALLin your case, and restart your JVM, and you should get a new value for java.util.Locale.getDefault().

您可以根据LC_ALL自己的情况进行更改,然后重新启动 JVM,您应该会为java.util.Locale.getDefault().

Example:

例子:

import java.util.Locale;

public class LocaleTest {
   public static void main(String[] args) {
      System.out.println(Locale.getDefault());
   }
}

Here's running:

这是运行:

> LC_ALL=en_UK java LocaleTest
en_UK

> LC_ALL=ja_JP java LocaleTest
ja_JP

Also note that if you're running Java 1.7.0-b147, there is a bug with the JRE not recognizing environment settings for locale, and will always use the default system locale.

另请注意,如果您运行的是 Java 1.7.0-b147,则 JRE 存在无法识别区域设置的环境设置的错误,并且将始终使用默认系统区域设置。

Bug report here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073906

错误报告在这里:http: //bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073906