Locale.getDefault()始终返回en [英] Locale.getDefault() returns en always

查看:1547
本文介绍了Locale.getDefault()始终返回en的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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



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




  • 扫描环境 LC_ALL

  • 如果 LC_ALL 不存在,扫描环境 LANG

  • 如果设置了JVM设置 user.language ,用它代替环境变量。

  • 如果没有设置,则默认为 en_US (我相信这是最后的失败案例)



在你的环境中,你将 LC_ALL 设置为 C ,这只是C语言环境。它基本上是传统的回退,当时没有使用语言环境。



您可以在您的情况下更改 LC_ALL ,并重新启动您的JVM,您应该为 java.util.Locale.getDefault()获取一个新值。



示例:

  import java.util.Locale; 

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

这里正在运行:

 > LC_ALL = en_UK java LocaleTest 
en_UK

> LC_ALL = ja_JP java LocaleTest
ja_JP

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



此处的错误报告: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073906


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

 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.

解决方案

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

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

  • Scan the environment for LC_ALL
  • If LC_ALL doesn't exist, scan the environment for LANG
  • If the JVM setting user.language is 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)

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

You can change LC_ALL in your case, and restart your JVM, and you should get a new value for 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.

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

这篇关于Locale.getDefault()始终返回en的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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