Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 01:40, 4 July 2008.

How to use ResourceBundle for localization in Java ME

From Forum Nokia Wiki

Contents

Main MIDlet code

package i18n;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
 
public class i18n extends MIDlet implements CommandListener {
    
    private Display display;
    private Form form;
    
    private Command exit;
    
    public i18n() {
        
        form = new Form(" i18n ");
        exit = new Command("Exit",Command.EXIT,0);
        String msg = ResourceBundle.getString("Test_fr", "hello senthil" );
        form.append(msg);
        form.addCommand(exit);
        form.setCommandListener(this);
        
    }
    
    public void startApp() {
        
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean uc) {
    }
    
    public void commandAction(Command c , Displayable d) {
        if(c==exit) {
            destroyApp(true);
        }
    }
}

ResourceBundle class

package i18n;
 
import java.util.Hashtable;
 
 
public class ResourceBundle {
    
    private static Hashtable groups = new Hashtable();
    
    public static Object getObject( String group, String key ) {
        ResourceBundle bundle;
        
        synchronized( groups ){
            bundle = (ResourceBundle) groups.get( group );
            if( bundle == null ){
                bundle = loadBundle( group );
            }
        }
        
        return bundle.getResource( key );
    }
    
    public static String getString( String group, String key ) {
        return (String) getObject( group, key );
    }
    
    public static ResourceBundle loadBundle( String name ) {
        ResourceBundle bundle = null;
        Locale locale = Locale.getDefaultLocale();
        String language = locale.getLanguage();
        String country = locale.getCountry();
        
        try {
            bundle = (ResourceBundle) Class.forName( name ).newInstance();
        } catch( Exception e ){
        }
        
        if(language != null ){
            ResourceBundle child;
            
            try {
                child = (ResourceBundle) Class.forName(name + '_' + language).newInstance();
                child.setParent( bundle );
                bundle = child;
            } catch( Exception e ){ }
            
            if( country != null ){
                try {
                    child = (ResourceBundle) Class.forName(
                            name + '_' + language +
                            '_' + country
                            ).newInstance();
                    child.setParent( bundle );
                    bundle = child;
                } catch( Exception e ){
                }
            }
        }
        
        if( bundle == null ){
            bundle = new ResourceBundle();
        }
        
        groups.put( name, bundle );
        return bundle;
    }
    
    protected Hashtable resources = new Hashtable();
    private ResourceBundle parent;
    
    protected ResourceBundle() {
// System.out.println("Iam in Cons");
    }
    
    protected void setParent( ResourceBundle parent ) {
        this.parent = parent;
    }
    
    protected Object getResource( String key ) {
        Object obj = null;
        
        if( resources != null ){
            obj = resources.get( key );
        }
        
        if( obj == null && parent != null ){
            obj = parent.getResource( key );
        }
        
        return obj;
    }
    
    public static class Locale {
        private String language = "en";
        private String country = "US";
        
        public Locale( String language, String country ) {
            this.language = language;
            this.country = country;
        }
        
        public Locale( String locale ) {
            if( locale != null ){
                int pos = locale.indexOf( '-' );
                if( pos != -1 ){
                    language = locale.substring( 0, pos );
                    locale = locale.substring( pos+1 );
                    
                    pos = locale.indexOf( '-' );
                    if( pos == -1 ){
                        country = locale;
                    } else {
                        country = locale.substring( 0, pos );
                    }
                }
            }
        }
        
        public String getLanguage() {
            return language;
        }
        
        public String getCountry() {
            return country;
        }
        
        private static Locale defaultLocale = new Locale( System.getProperty("microedition.locale" ) );
        
        public static Locale getDefaultLocale() {
            return defaultLocale;
        }
        
        public static void setDefaultLocale( Locale locale ) {
            defaultLocale = locale;
        }
    }
}

Test English, Default Locale

// Default locale (base name)
 
package i18n;
 
 
public class Test extends ResourceBundle {
    public Test() {
        resources.put( "hello", "Hello!" );
        resources.put( "goodbye", "Goodbye!" );
        resources.put( "stop", "Stop!" );
        resources.put( "notranslation", "This is English." );
    }
}

French (fr) language, no specific country

package i18n;
 
 
public class Test_fr extends ResourceBundle {
    public Test_fr() {
        resources.put( "hello sen", "Bonjour!" );
        resources.put( "goodbye", "Aurevoir!" );
    }
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
S60 3rd edition emulator has blank screen vkhaitan Symbian Tools & SDKs 10 2007-03-11 06:17
Russian... davidmaxwaterman General Symbian C++ 6 2007-08-24 09:59
Java Downloads nickpage Mobile Java General 1 2003-10-02 13:17
Browsing Japanese Webpges on S60 Browser (N95) senocs General Browsing 25 2008-07-11 19:43
MMConverter ver 2.0 zvone.c Graphics & Video & Streaming 1 2003-06-09 11:49
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZjavaQ
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX