java 执行区域设置比较的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3074884/
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-10-30 00:14:54  来源:igfitidea点击:

Correct way to perform Locale Comparison

java

提问by Cheok Yan Cheng

Currently, I wish to know which properties file is being loaded in my application.

目前,我想知道我的应用程序中正在加载哪个属性文件。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package example0;

import java.util.Locale;

/**
 *
 * @author yccheok
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Locale.setDefault(Locale.SIMPLIFIED_CHINESE);     // Bundle_zh_CH.properties will be loaded.
        //Locale.setDefault(Locale.CHINA);                  // Bundle_zh_CH.properties will be loaded.
        //Locale.setDefault(Locale.TRADITIONAL_CHINESE);    // Bundle.properties will be loaded.
        //Locale.setDefault(Locale.CHINESE);                // Bundle.properties will be loaded.

        String Hello = java.util.ResourceBundle.getBundle("example0/Bundle").getString("HELLO");
        System.out.println(Hello);

        System.out.println("Locale.SIMPLIFIED_CHINESE's language : " + Locale.SIMPLIFIED_CHINESE.getLanguage());
        System.out.println("Locale.CHINA's language : " + Locale.CHINA.getLanguage());
        System.out.println("Locale.TRADITIONAL_CHINESE's language : " + Locale.TRADITIONAL_CHINESE.getLanguage());
        System.out.println("Locale.CHINESE's language : " + Locale.CHINESE.getLanguage());

        System.out.println("Locale.SIMPLIFIED_CHINESE's country : " + Locale.SIMPLIFIED_CHINESE.getCountry());
        System.out.println("Locale.CHINA's country : " + Locale.CHINA.getCountry());
        System.out.println("Locale.TRADITIONAL_CHINESE's country : " + Locale.TRADITIONAL_CHINESE.getCountry());
        System.out.println("Locale.CHINESE's country : " + Locale.CHINESE.getCountry());
    }

}

The following is the output :

以下是输出:

Hello
Locale.SIMPLIFIED_CHINESE's language : zh
Locale.CHINA's language : zh
Locale.TRADITIONAL_CHINESE's language : zh
Locale.CHINESE's language : zh
Locale.SIMPLIFIED_CHINESE's country : CN
Locale.CHINA's country : CN
Locale.TRADITIONAL_CHINESE's country : TW
Locale.CHINESE's country : 

Previously, to determine whether properties file Bundle_zh_CH.properties will be loaded, I am performing the following comparison.

之前,为了确定是否会加载属性文件Bundle_zh_CH.properties,我进行了以下比较。

if (Locale.getDefault() == Locale.SIMPLIFIED_CHINESE)

However, some Locale other than SIMPLIFIED_CHINESE will load Bundle_zh_CH.properties as well.

但是,除了 SIMPLIFIED_CHINESE 之外的一些 Locale 也会加载 Bundle_zh_CH.properties。

What is the reliable way for me to do so?

我这样做的可靠方法是什么?

Shall I

我可以吗

if (Locale.getDefault() == Locale.SIMPLIFIED_CHINESE || Locale.getDefault() == Locale.China)

or

或者

if (Locale.getDefault().equals("CN"))

回答by krock

Don't rely on equals operator comparison as you can create new Locale instances with its public constructors. In the following code:

不要依赖等于运算符比较,因为您可以使用其公共构造函数创建新的 Locale 实例。在以下代码中:

Locale simpChinese = new Locale("zh","CN","");
System.out.println(simpChinese == Locale.SIMPLIFIED_CHINESE);
System.out.println(simpChinese.equals(Locale.SIMPLIFIED_CHINESE));

prints:

印刷:

false
true