java - Correct way to perform Locale Comparison -


Currently, I want to know which property file is loading in my application.

  / * * To change this template, select the tool. Templates * and open the template in the editor. * / Package example 0; Import java.util.Locale; / ** * * author @ official * / public class main {/ ** * @ ultimate command line argument * / public static zero 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 ("Example 0 / Bundle"). GetString ("Hello"); Println (hello); System.out.println ("locale.SIMPLIFIED_CHINESE's language:" + Local .SIMPLIFIED_CHINESE.get language ()); System.out.println ("Locale: Chinese language:" + locale .CHINA.get language ()); System.out.println ("Language of Locale.TRADITIONAL_CHINESE:" + locale. Trident_CHINESE.get language ()); System.out.println ("Language of Locale.CHINESE:" + locale. CHINESE.getLanguage ()); System.out.println ("Country of Locale.SIMPLIFIED_CHINESE:" + Local. SIMPLIFIED_CHINESE.getCountry ()); System.out.println ("Locale: Country of China:" + locale. CHINA.getCountry ()); System.out.println ("Locale.TRADITIONAL_CHINESE Country:" + Locale Trident_CHINESE.getCountry ()); System.out.println ("Country of Locale.CHINESE:" + locale. CHINESE.getCountry ()); }}  

The following output is:

  Hello locale. SIMPLIFIED_CHINESE's language: zh Local. CHINA's language: zh Local. TRADITIONAL_CHINESE's language: zh Locale Chinese language: zh Country of locale.SIMPLIFIED_CHINESE: Country of CN Locale.CHINA: COUNTRY COUNTRY: First, determine the properties of the file that 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 this?

Do I

  if (Locale.getDefault () == Locale.SIMPLIFIED_CHINESE || locale.gate default () == locale.china)  < / Pre> 

or

  if (local.get default () equals ("cn"))  

Do not rely on equal operator comparison because you can create new locale examples with your public constructor. In the following code:

  local Simp China = new locale ("zh", "CN", ""); System.out.println (simpChinese == Locale.SIMPLIFIED_CHINESE); Println (simpChinese.equals (Locale.SIMPLIFIED_CHINESE));  

Print:

  false truth  

Comments