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)
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.