2010-06-19 4 views
10

Actuellement, je souhaite savoir quel fichier de propriétés est chargé dans mon application.Méthode correcte pour effectuer la comparaison des paramètres régionaux

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

} 

Ce qui suit est la sortie:

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 : 

Auparavant, pour déterminer si le fichier de propriétés Bundle_zh_CH.properties seront chargées, j'effectuer la comparaison suivante.

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

Cependant, une autre que SIMPLIFIED_CHINESE Locale chargera Bundle_zh_CH.properties ainsi.

Quelle est la manière fiable pour moi de le faire?

-je

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

ou

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

Répondre

21

Ne comptez pas sur l'opérateur égal comparaison que vous pouvez créer de nouvelles instances de ses constructeurs locaux avec publics. Dans le code suivant:

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

impressions:

false 
true 
+2

Toujours vérifier l'égalité entre les objets avec 'equals (Object)' – Radu

Questions connexes