Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to perform Locale Comparison

Tags:

java

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.

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

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

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"))
like image 257
Cheok Yan Cheng Avatar asked Jun 19 '10 08:06

Cheok Yan Cheng


People also ask

What is locale dependent comparison?

Performing a comparison using equals after changing the casing of strings is locale dependent. This comparison may lead to unexpected results. When performing changes to the casing of a string, a locale needs to be provided to map lowercase and uppercase characters.

What is locale type?

A locale consists of a number of categories for which country-dependent formatting or other specifications exist. A program's locale defines its code sets, date and time formatting conventions, monetary conventions, decimal formatting conventions, and collation (sort) order.

What is the use of locale?

A Locale is the mechanism for identifying the kind of object ( NumberFormat ) that you would like to get. The locale is just a mechanism for identifying objects, not a container for the objects themselves.


1 Answers

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

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

prints:

false
true
like image 161
krock Avatar answered Sep 27 '22 17:09

krock