Wednesday, 3 July 2013

Java Internationalization (I18N)


The purpose of International (I18N) is to provide a more user-friendly way to change the language of label or messages in a program. The following are important class in Java Internationalization (I18N) (1) java.util.ResourceBundle (2) java.util.Locale (3) java.text.MessageFormat (A) The following code could obtain all locale supported by JAVA:
package com.easyinfo100;  
  
import java.util.Locale;  
  
public class LocaleList {  
  
    public static void main(String[] args) {  
  
        // Get All supported locale  
        Locale[] localeList = Locale.getAvailableLocales();  

        // loop throuth each locale and get language and country
        for (int i = 0; i < localeList.length; i++) {  
    
            System.out.println(localeList[i].getDisplayCountry() + "="  
                    + localeList[i].getCountry() + "  "  
                    + localeList[i].getDisplayLanguage() + "="  
                    + localeList[i].getLanguage());  
  
        }  
    }  
  
}  
(B) The following example is a simple example of Intenrationalization
import java.util.Locale;  
import java.util.ResourceBundle;  
  
public class Hello {  
  
    public static void main(String[] args) {  
        // get default locale 
        Locale myLocale = Locale.getDefault();  

        // get resource bundle of default locale

        ResourceBundle bundle1 = ResourceBundle.getBundle("mess", myLocale);  
        // get resource bundle of specific locale 
        ResourceBundle bundle2 = ResourceBundle.getBundle("mess", Locale.US);  

        // print out the resource properties  
        System.out.println(bundle1.getString("hello"));  
        System.out.println(bundle2.getString("hello"));  
    }  
  
}  

0 comments:

Post a Comment