2010-06-19 18 views
10

Attualmente, desidero sapere quali file delle proprietà vengono caricati nella mia applicazione.Modo corretto per eseguire il confronto delle impostazioni internazionali

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

} 

Quello che segue è l'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 : 

In precedenza, per determinare se il file proprietà Bundle_zh_CH.properties verranno caricati, sto eseguendo il seguente confronto.

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

Tuttavia, alcuni Locale diverso SIMPLIFIED_CHINESE caricherà Bundle_zh_CH.properties pure.

Qual è il modo affidabile per me di farlo?

Devo

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

o

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

risposta

21

non si basano sul confronto uguale operatore è possibile creare nuove istanze Locale con i suoi costruttori pubblici. Nel codice seguente:

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

stampe:

false 
true 
+2

Controllare sempre l'uguaglianza tra gli oggetti con '.equals (Object)' – Radu

Problemi correlati