您现在的位置是:首页 > 编程开发 > Java > Java

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

编辑:站长来源:互联网作者: 佚名2020-08-03Java

简介目前,我想知道在我的应用程序中加载了哪些属性文件. /* * To change this template, choose Tools | Templates * and open the template in the editor. */package example0;import java.util.Locale;/** * * @author yccheok *

目前,我想知道在我的应用程序中加载了哪些属性文件.

/*
 * 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());
    }

}

以下是输出:

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 :

以前,要确定属性文件Bundle_zh_CH.properties是否被加载,我正在执行以下比较.

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

但是,除SIMPLIFIED_CHINESE之外的某些区域设置也会加载Bundle_zh_CH.properties.

我有什么可靠的方法呢?

我可以吗

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

要么

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

解决方法

不要依赖于equals运算符比较,因为您可以使用其公共构造函数创建新的Locale实例.在以下代码中:
Locale simpChinese = new Locale("zh","CN","");
System.out.println(simpChinese == Locale.SIMPLIFIED_CHINESE);
System.out.println(simpChinese.equals(Locale.SIMPLIFIED_CHINESE));

打印:

false
true

很赞哦! ()