Search code examples
javajava-melocalizationinternationalizationlwuit

How to solve localization problem with LWUIT


I have made a simple form with one textField and command, I want to have two languages: Farsi and English so that I can use them according to my needs at runtime.

I made some translations using ResourceEditor and saved them under the name English.res and Farsi.res, then added them to my resources.

Now I have a problem calling Localization method, as I have no idea how to do that. I will post my code here, please correct me.

This is my code:

public class Midlet extends javax.microedition.midlet.MIDlet {

    private Hashtable locale;

    public void startApp() {
        Display.init(this);

        try {
            Resources res = Resources.open("/Lang.res");    
            //Lang.res is resource file where these languages are stored
            // using resoureEditor.
            locale = res.getL10N("English.res", "en");

           /* See text below. */
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        Form main = new Form((String) locale.get("FORM"));
        main.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        Label label = new Label((String) locale.get("NAME"));
        TextField tf1 = new TextField("");
        Button button = new Button((String) locale.get("OK"));

        main.addComponent(label);
        main.addComponent(tf1);
        main.addComponent(button);

        main.addCommand(new Command((String) locale.get("BACK")) {
            public void actionPerformed(ActionEvent evt) {}
        });
        main.show();
    }
    public void pauseApp() {}
    public void destroyApp(boolean unconditional) {}
}

And this is my English.res:

#Export locale from the Theme Creator
#Tue Aug 02 19:35:55 IRDT 2011
FORM=form
OK=ok
BACK=back
NAME=name

Are these two lines in the try block correct?

       Resources res = Resources.open("/Lang.res");    
       locale = res.getL10N("English.res", "en");

Lang.res is resource file where these languages are stored using resoureEditor.

What else do I need to do?

I get an exception in the first line. en is the name of the needed language locale, matching to the value column in res file.

I'm still not sure if it's correct or not but, what is this first argument? Locale?


Solution

  • I suggest using "Resource Editor" for creating L10N resource, it can be synchronized with the gui, making this task easier to do and it's easire to retrieve in the code.

    try {
            Constants.res = Resources.open("/Lang.res");
        } catch (Exception e){
            System.err.println("can't load resource file:" + e);
        }
    Hashtable h = Constants.res.getL10N("English.res","en");