Java Swing - dynamic switch localization at runtime

I understand how to internationalize a java program, but I have a problem. The language in my program can be switched at any time, but my program can exist in many states, which means that it may or may not have several JLabels, JPanels, JFrames, etc., Open. Is there a class or method that will update the current dial-up GUI, or should it be done manually?

If nothing works, I just ask the user to restart the program to switch the language, but changing the runtime would be nice ...

+5
source share
1 answer

, , , . , :

JLabel label = new JLabel();
label.setText(LocalizationManager.get("MY_LABEL_TEXT"));

LocalizationManager , MY_LABEL_TEXT . "" , .

; (.. "MY_LABEL_TEXT" ) , ( " !" "Bienvenido!" ) , . , , ( ) /.

: (1)

public class LocalizationManager {
  private SupportedLanguage currentLanguage = SupportedLanguage.ENGLISH;//defaults to english
  private Map<SupportedLanguage, Map<String, String>> translations;

  public LocalizationManager() {
    //Initialize the strings. 
    //This is NOT a good way; don't hardcode it. But it shows how they're set up.

    Map<String, String> english = new HashMap<String, String>();
    Map<String, String> french = new HashMap<String, String>();

    english.set("MY_LABEL_TEXT", "Good day!");
    french.set("MY_LABEL_TEXT", "Beinvenido!");//is that actually french?

    translations.set(SupportedLanguage.ENGLISH, english);
    translations.set(SupportedLanguage.FRENCH, french);
  }

  public get(String key) {
    return this.translations.get(this.currentLanguage).get(key);
  }

  public setLanguage(SupportedLanguage language) {
    this.currentLanguage = language;
  }

  public enum SupportedLanguage {
    ENGLISH, CHINESE, FRENCH, KLINGON, RUSSIAN; 
  }
}

(1) , , .

+1

All Articles