2014-02-27 192 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。但是它出於兼容性的原因而被使用。代碼評論應該提供足夠的信息來了解區域和國家/地區屬性如何工作