×
Namespaces

Variants
Actions

How to use ResourceBundle for localization in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: senthilkumar05 (28 Dec 2007)
Last edited: hamishwillee (29 Jul 2013)

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!" );
}
}
This page was last modified on 29 July 2013, at 07:49.
52 page views in the last 30 days.
×