2014-02-27 101 views
1

我搜索了哪些是缺省语言环境的系统属性。我发现很多地方是这样的:user.language(它工作)和user.region(不会工作)。经过深入的搜索,我发现一个user.country(这有点工作 - 返回的结果应该是由user.region返回)。系统属性:user.region或user.country

那么这是怎么回事?这些参考都是错误的,或者我错过了什么?

+0

你为什么试图访问这些属性而不是使用java.util.Locale方法? –

+0

只有了解机制。 :-) –

回答

2

你应该能看到涉及的缺省语言环境的直接在JDK源代码的计算系统属性:

public static Locale getDefault() { 
    // do not synchronize this method - see 4071298 
    // it's OK if more than one default locale happens to be created 
    if (defaultLocale == null) { 
     String language, region, country, variant; 
     language = AccessController.doPrivileged(
      new GetPropertyAction("user.language", "en")); 
     // for compatibility, check for old user.region property 
     region = AccessController.doPrivileged(
      new GetPropertyAction("user.region")); 
     if (region != null) { 
      // region can be of form country, country_variant, or _variant 
      int i = region.indexOf('_'); 
      if (i >= 0) { 
       country = region.substring(0, i); 
       variant = region.substring(i + 1); 
      } else { 
       country = region; 
       variant = ""; 
      } 
     } else { 
      country = AccessController.doPrivileged(
       new GetPropertyAction("user.country", "")); 
      variant = AccessController.doPrivileged(
       new GetPropertyAction("user.variant", "")); 
     } 
     defaultLocale = getInstance(language, country, variant); 
    } 
    return defaultLocale; 
} 

从代码中,总是使用user.language。 user.region似乎不赞成user.country和user.variant。但是它出于兼容性的原因而被使用。代码评论应该提供足够的信息来了解区域和国家/地区属性如何工作