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ương tự cho các components khác*/
else if(lComponent [j] instanceof Container)
defaultUpdateResourcesBund le((Contai ner)lCompo nent [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(lResourceB undle.getS tring("lan guage"));
jButton3.setText(lResource Bundle.get String("bu tton"));
}
but I encountered an exception when one button was clicked:
Exception in thread "AWT-EventQueue-0" java.util.MissingResourceE xception: Can't find bundle for base name language, locale vi
Any solution is greatly appreciated.
Regards.
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(
}
private void autoUpdateResourcesBundle(
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
( (PLabel) lComponent[j]).setText( getResourceString(( (PLabel) lComponent[j]).resourceKey
}/*Tương tự cho các components khác*/
else if(lComponent [j] instanceof Container)
defaultUpdateResourcesBund
}
}
}
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("
jLabel1.setText(lResourceB
jButton3.setText(lResource
}
but I encountered an exception when one button was clicked:
Exception in thread "AWT-EventQueue-0" java.util.MissingResourceE
Any solution is greatly appreciated.
Regards.
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)
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)
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?
where is it currently stored
and what is your classpath?
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(lResourceB undle.getS tring("lan guage"));
jButton3.setText(lResource Bundle.get String("bu tton"));
}
Anything need to be correct?
private void changeLanguage(String pNewLanguage){
Locale lLocale = new Locale(pNewLanguage);
ResourceBundle lResourceBundle = ResourceBundle.getBundle("
jLabel1.setText(lResourceB
jButton3.setText(lResource
}
Anything need to be correct?
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>I not sure about this.
System.out.println(System. getPropert y("java.cl ass.path") );
System.out.println(System.
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
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
ASKER
>System.out.println(System .getProper ty("java.c lass.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 ;)
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 ;)
ASKER
TheMajestic : thanks for the article :D
Means you need a properties bundle called "language_vi"