Home > Software design >  java use switch statement with values retrieved by locale
java use switch statement with values retrieved by locale

Time:11-19

I want to change my switch statement from hardcoded values to something more specific to the current locale, because the names of the languages change from language to language.

I cant change this:

switch(Objects.requireNonNull(comboBox.getSelectedItem()).toString()) {
                case "Deutsch" -> Locale.setDefault(new Locale("de", "DE"));
                case "Englisch (UK)" -> Locale.setDefault(new Locale("en", "UK"));
                case "Englisch (US)" -> Locale.setDefault(new Locale("en", "US"));
                case "Französisch" -> Locale.setDefault(new Locale("fr", "FR"));
}

Into something like that

switch(Objects.requireNonNull(comboBox.getSelectedItem()).toString()) {
                case (resourceBundle.getString("menu.settings.language.german")) -> Locale.setDefault(new Locale("de", "DE"));
                case (resourceBundle.getString("menu.settings.language.english_uk")) -> Locale.setDefault(new Locale("en", "UK"));
                case (resourceBundle.getString("menu.settings.language.english_us")) -> Locale.setDefault(new Locale("en", "US"));
                case (resourceBundle.getString("menu.settings.language.french")) -> Locale.setDefault(new Locale("fr", "FR"));
}

How would I do that?

CodePudding user response:

The case labels in a switch need to be a constant expression in Java. You can either replace it with an if-statement or use a Map to look up actions (Runnables).

Approach using if

final String selected = Objects.requireNonNull(comboBox.getSelectedItem()).toString()
if (selected.equals(resourceBundle.getString("menu.settings.language.german"))) {
  Locale.setDefault(new Locale("de", "DE"));
} else if (selected.equals(resourceBundle.getString("menu.settings.language.english_uk"))) {
  Locale.setDefault(new Locale("en", "UK"))
} else if (selected.equals(resourceBundle.getString("menu.settings.language.english_us"))) {
  Locale.setDefault(new Locale("en", "US"))
} else if (selected.equals(resourceBundle.getString("menu.settings.language.french"))) {
  Locale.setDefault(new Locale("fr", "FR"));
}

Approach using a lookup map

final Map<String, Runnable> localeActions = Map.ofEntries(
    Map.entry(
        resourceBundle.getString("menu.settings.language.german"),
        () -> Locale.setDefault(new Locale("de", "DE"))
    ),
    Map.entry(
        resourceBundle.getString("menu.settings.language.english_uk")
        () -> Locale.setDefault(new Locale("en", "UK"))
    ),
    Map.entry(
        resourceBundle.getString("menu.settings.language.english_us")
        () -> Locale.setDefault(new Locale("en", "US"))
    ),
    Map.entry(
        resourceBundle.getString("menu.settings.language.french")
        () -> Locale.setDefault(new Locale("fr", "FR"))
    )
);

final Runnable action = localeActions.getOrDefault(
    Objects.requireNonNull(comboBox.getSelectedItem()).toString(),
    () -> {});
action.run();
  • Related