Link to home
Start Free TrialLog in
Avatar of khoama
khoama

asked on

How to change language dynamically?

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>Can't find bundle for base name language, locale vi

Means you need a properties bundle called "language_vi"
Its looking for the resource bundle for that language and not finding anything that matches.
Its looking for a class named language_vi, or a properties file name language_vi.properties, or language.properties in your classpath.

for more info see the javadoc

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader)
Avatar of khoama
khoama

ASKER

I made a file name language_vi.properties already but the exception still appears.There are 2 properties files, language_en.properties and language_vi.properties.
sounds like u have it in the wrong location, it needs to be accessible from your classpath.
where is it currently stored
and what is your classpath?
Avatar of khoama

ASKER

I just pass a string "vi" in changLanguage function and expect it return "language_vi.properties":

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"));
      }
Anything need to be correct?
Avatar of khoama

ASKER

>where is it currently stored
It is stored at the same folder level with my mainFrame.java

>and what is your classpath?
I not sure about this.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I not sure about this.


System.out.println(System.getProperty("java.class.path"));
hai khoama,

i think this site will be very useful for u regarding ur question

http://today.java.net/pub/a/today/2006/07/25/aop-and-i18n.html
Avatar of khoama

ASKER

>System.out.println(System.getProperty("java.class.path"));
CEHJ: I check this and did what objects said is put all properties files on the class path directory and it works.Thanks for your tip ;)
Avatar of khoama

ASKER

TheMajestic : thanks for the article :D