How to create multi language app using redio buttons?

47 views
Skip to first unread message

samer.als...@gmail.com

unread,
Jul 12, 2016, 12:48:40 PM7/12/16
to CodenameOne Discussions
Hello

I set tow radio buttons in the same group, and prepare the Localization, I tried to refresh the app after the events but did not work, how to make it?

This is the code ::
    Hashtable<String, String> arabic, english;

   
protected void initVars(Resources res) {
        arabic
= res.getL10N("L", "ar");
        english
= res.getL10N("L", "en");      
   
}
   
   
protected void onMain_ArabicLanguageAction(Component c, ActionEvent event){
       
UIManager.getInstance().setBundle(arabic);
       
       
Form form = Display.getInstance().getCurrent();
        form
.revalidate();
   
}
   
   
protected void onMain_EnglishLanguageAction(Component c, ActionEvent event){
       
UIManager.getInstance().setBundle(english);
       
       
Form form = Display.getInstance().getCurrent();
        form
.revalidate();
   
}

Thanks
My regards ,,

Shai Almog

unread,
Jul 13, 2016, 1:27:08 AM7/13/16
to CodenameOne Discussions, samer.als...@gmail.com
Hi,
You need to recreate the form not just refresh. So if you have a method like createForm() that performs all the new label and add component operations you need to call that again to create an new form instance and show that.

samer.als...@gmail.com

unread,
Jul 13, 2016, 1:37:46 PM7/13/16
to CodenameOne Discussions, samer.als...@gmail.com
Thanks for replay ..

I set this code, it works to change the language by reopen the form..
    showForm("Main", null);

the first problem is the all radio buttons unchecked, so where I can checked it ?
the second problem is how to open the third tab, the third tab contains language radio buttons ?

Thanks ,,

Shai Almog

unread,
Jul 14, 2016, 1:30:38 AM7/14/16
to CodenameOne Discussions, samer.als...@gmail.com
You can do a reloadForm() for GUI builder apps.

Your before*Form event callback should initialize selection based on application state.


samer.als...@gmail.com

unread,
Jul 14, 2016, 3:17:34 PM7/14/16
to CodenameOne Discussions, samer.als...@gmail.com
The reload form it refresh the form and it change the language.
the problem now is select the radio button.. this the last code but it does not work ..

    @Override
   
public void reloadForm() {
       
super.reloadForm();
       
       
if(findArabicLanguage() != null && UIManager.getInstance().getBundle() == arabic) {
           
//findArabicLanguage().setSelected(true);
           
RadioButton ar = findArabicLanguage();
            ar
.setSelected(true);
       
}
       
       
if(findEnglishLanguage() != null && UIManager.getInstance().getBundle() == english) {
           
//findEnglishLanguage().setSelected(true);
           
RadioButton en = findEnglishLanguage();
            en
.setSelected(true);

       
}
   
}
   
   
protected void onMain_ArabicLanguageAction(Component c, ActionEvent event){

       
if(findArabicLanguage() != null && UIManager.getInstance().getBundle() != arabic) {
           
UIManager.getInstance().setBundle(arabic);
            reloadForm
();

       
}
   
}
   
   
protected void onMain_EnglishLanguageAction(Component c, ActionEvent event){

       
if(findEnglishLanguage() != null && UIManager.getInstance().getBundle() != english) {
           
UIManager.getInstance().setBundle(english);
            reloadForm
();
       
}
   
}


Shai Almog

unread,
Jul 15, 2016, 12:29:35 AM7/15/16
to CodenameOne Discussions, samer.als...@gmail.com
You need to initialize the variable states in the before form callback.

samer.als...@gmail.com

unread,
Jul 15, 2016, 7:01:50 AM7/15/16
to CodenameOne Discussions, samer.als...@gmail.com
I couldn't understand about you!!
Can you explain by code, please?

Shai Almog

unread,
Jul 16, 2016, 1:00:47 AM7/16/16
to CodenameOne Discussions, samer.als...@gmail.com
This is explained here: http://www.codenameone.com/how-do-i---handle-eventsnavigation-in-the-gui-builder--populate-the-form-from-code.html

The form is recreated on navigation, it is your responsibility to initialize it in the before show callback.
Reply all
Reply to author
Forward
0 new messages