1

I use codenameone to develop my mobile application. In this application I implement some classes and codes manually for instance create all forms by hard coding not using codenameone designer for some reason.

By the way I wanted to navigate in forms like what codenameone use, so I use one variable from type of Form called it prevForm and when I want to open a form I set it to current form and then I show new form.

Ok, that is main scenario. In this application I wanna implement internationalization too, so I create my own hashtable (Farsi and English) for this application.

This is my problem:

  1. How can I set or change language and apply it to forms that I opened?

  2. Is my method for navigate between forms are good?

Here is my code:

public class BaseForm extends Form implements ActionListener {
public BaseForm(){
    this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
}

Command exit, ok, back;
Form prevForm;

protected void initForm(){

}

protected void showForm(){

}

protected void showForm(final Form prevForm){
    //String name = this.getName();
    //if("Reminder".equals(name) || "3Transaction".equals(name))
    {
        this.prevForm = prevForm;
        Form f = this;
        back = new Command("Back");
        //ok = new Command("Ok");
        //delete = new Command("Delete");;
        Button button = new Button("Button");

        f.addCommand(back);
        //f.addCommand(ok);
        //f.addCommand(delete);
        //f.addComponent(button);

        f.addCommandListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if (ae.getCommand().equals(back)) {
                    //Do Exit command code
                    System.out.println("Back pressed");
                    prevForm.showBack();
                } else if (ae.getCommand().equals(ok)) {
                    //Do Start command code
                    System.out.println("Ok pressed");
                }
            }
        });

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                //Do button code
                System.out.println("Action performed");
            }
        });
    }
    showForm();
}}

for open nested form I use this code:

LanguageUI lang = new LanguageUI();
lang.showForm(this);

change language [form]:

protected boolean onBtnSave() {
    if(isRbFarsiSelected()){
        UIManager.getInstance().setResourceBundle(new CommonSettings().getFarsi());
    }
    else {
        UIManager.getInstance().setResourceBundle(new CommonSettings().getEnglish());
    }

    return false;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmad
  • 437
  • 5
  • 20

1 Answers1

2

I also hard code my UI on lwuit, and i have a variable parentForm on every class so i can easily show previous form. For language change i know there is Localization in the resource editor that you can make use of. Below is how you can access it. I guess the trick is how to set the content of the L10N in the res file in code? On the other hand you can create your own helper classes that mirror the methods below.

Resources theme = Resources.open("/theme.res");
theme.getL10N(id, locale);
theme.getL10NResourceNames();
theme.isL10N(name);
theme.listL10NLocales(id)
Ajibola
  • 1,218
  • 16
  • 28
  • Thanks Ajibola, but I can't and I don't want to use resource file. Also I can't use this code ("esources.open("/theme.res");"). – Ahmad Dec 05 '12 at 15:12
  • Ok then, you will have to create your own helper methods, similar to how lwuit handles them or android cause its the better way to get it done, i.e. you save an id with the corresponding translation and fetch that id when you need it from the appropriate translation. – Ajibola Dec 06 '12 at 09:47
  • ok, but I don't know how to apply it in a form that called a language form. How can I do it? – Ahmad Dec 07 '12 at 13:32
  • Suppose that I have 3 form "Main", "Setting", "Language". I start with "Main" and then I open "Setting" from "Main" and after that open "Language" from "Setting" ("Main" -> "Setting" -> "Language"). After set language on Save in "Language" form I wanna it set to all form automatically. – Ahmad Dec 07 '12 at 13:36
  • There shouldn't be any need to explicitly set the language in a language form because you can get what locale the user is operating in programatically. String locale = System.getProperty("microedition.locale"); I suggest you do this as the MIDlet starts and then you can route the language to the appropriate hashtable. But to answer your question, best solution is to have a static global variable that represents the current language is the Main form, when the language changes just change this variable, and reference it when you need to know which language is being used. – Ajibola Dec 09 '12 at 06:27