We help IT Professionals succeed at work.
Get Started

How to change  language dynamically?

khoama
khoama asked
on
5,339 Views
Last Modified: 2013-12-22
Internationalize in Java.

Can we:
- Change all the swing components in runtime (using properties file and Locale)...
- With out rewrite every component to extend Swing components (to assign a resource key for it) and then use a recursive function to process every component to change it text? Like :

public void autoUpdateResourcesBundle(){
      autoUpdateResourcesBundle(this);
}

private void autoUpdateResourcesBundle(Container pContainer){
      Component lComponent [] = pContainer.getComponents();
    if(lComponent!=null && lComponent.length>0){
          for (int j = 0; j < lComponent.length; j++) {
              if (lComponent[j] instanceof PLabel && ( (PLabel) lComponent[j]).resourceKey!=null) {
                  ( (PLabel) lComponent[j]).setText( getResourceString(( (PLabel) lComponent[j]).resourceKey) +": ");
            }/*T&#432;&#417;ng t&#7921; cho các components khác*/
            else if(lComponent [j] instanceof Container)
                  defaultUpdateResourcesBundle((Container)lComponent [j]);
        }
    }
}

I code a demo application to change the language in runtime.2 buttons to switch between 2 language and execute this function :

private void changeLanguage(String pNewLanguage){
            Locale lLocale = new Locale(pNewLanguage);
            
            ResourceBundle lResourceBundle = ResourceBundle.getBundle("language", lLocale);
            
            jLabel1.setText(lResourceBundle.getString("language"));
            jButton3.setText(lResourceBundle.getString("button"));

      }

but I encountered an exception  when one button was clicked:

Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Can't find bundle for base name language, locale vi

Any solution is greatly appreciated.

Regards.
Comment
Watch Question
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE