系统属性:user.region或user.country

时间:2014-02-27 23:33:21

标签: java

我搜索了哪些是默认语言环境的系统属性。我发现很多地方都是这样的:user.language(它有效)和user.region(无法工作)。经过深入搜索后,我找到了user.country(有点工作 - 返回结果应该由user.region返回的内容。)

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

1 个答案:

答案 0 :(得分:2)

您应该能够直接在JDK源代码中看到计算默认Locale所涉及的系统属性:

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.cregtry似乎被user.country和user.variant弃用。但是出于兼容性原因使用它。代码注释应提供足够的信息,以了解区域和国家/地区属性的工作方式。