Dynamic menu language list

This commit is contained in:
Anurag Lint 2024-09-16 21:24:18 +02:00
parent f5c79e33cd
commit b33459f5d1
2 changed files with 29 additions and 25 deletions

View file

@ -141,7 +141,10 @@ public class AppController implements Initializable {
private ToggleGroup theme;
@FXML
private ToggleGroup language;
private ToggleGroup languages;
@FXML
private Menu languagesMenu;
@FXML
private CheckMenuItem openWalletsInNewWindows;
@ -378,14 +381,7 @@ public class AppController implements Initializable {
selectedThemeToggle.ifPresent(toggle -> theme.selectToggle(toggle));
setTheme(null);
Language configLanguage = Config.get().getLanguage();
if(configLanguage == null) {
configLanguage = LanguagesManager.DEFAULT_LANGUAGE;
Config.get().setLanguage(configLanguage);
}
final Language selectedLanguage = configLanguage;
Optional<Toggle> selectedLanguageToggle = language.getToggles().stream().filter(toggle -> selectedLanguage.equals(toggle.getUserData())).findFirst();
selectedLanguageToggle.ifPresent(toggle -> language.selectToggle(toggle));
prepareLanguages();
openWalletsInNewWindowsProperty.set(Config.get().isOpenWalletsInNewWindows());
openWalletsInNewWindows.selectedProperty().bindBidirectional(openWalletsInNewWindowsProperty);
@ -444,6 +440,27 @@ public class AppController implements Initializable {
setNetworkLabel();
}
private void prepareLanguages() {
Language[] languagesList = Language.values();
for(Language language : languagesList) {
RadioMenuItem languageItem = new RadioMenuItem(LanguagesManager.getMessage("language." + language.getCode()));
languageItem.setMnemonicParsing(false);
languageItem.setToggleGroup(languages);
languageItem.setOnAction(this::setLanguage);
languageItem.setUserData(language);
languagesMenu.getItems().add(languageItem);
}
Language configLanguage = Config.get().getLanguage();
if(configLanguage == null) {
configLanguage = LanguagesManager.DEFAULT_LANGUAGE;
Config.get().setLanguage(configLanguage);
}
final Language selectedLanguage = configLanguage;
Optional<Toggle> selectedLanguageToggle = languages.getToggles().stream().filter(toggle -> selectedLanguage.equals(toggle.getUserData())).findFirst();
selectedLanguageToggle.ifPresent(toggle -> languages.selectToggle(toggle));
}
private void registerShortcuts() {
org.controlsfx.tools.Platform platform = org.controlsfx.tools.Platform.getCurrent();
if(platform == org.controlsfx.tools.Platform.OSX) {
@ -2358,7 +2375,7 @@ public class AppController implements Initializable {
}
public void setLanguage(ActionEvent event) {
Language selectedLanguage = (Language)language.getSelectedToggle().getUserData();
Language selectedLanguage = (Language)languages.getSelectedToggle().getUserData();
if(Config.get().getLanguage() != selectedLanguage) {
Config.get().setLanguage(selectedLanguage);
}

View file

@ -66,7 +66,7 @@
<ToggleGroup fx:id="theme"/>
</fx:define>
<fx:define>
<ToggleGroup fx:id="language"/>
<ToggleGroup fx:id="languages"/>
</fx:define>
<Menu fx:id="viewMenu" mnemonicParsing="false" text="View">
<items>
@ -117,20 +117,7 @@
</RadioMenuItem>
</items>
</Menu>
<Menu mnemonicParsing="false" text="%language">
<items>
<RadioMenuItem mnemonicParsing="false" text="%language.en" toggleGroup="$language" onAction="#setLanguage">
<userData>
<Language fx:constant="ENGLISH" />
</userData>
</RadioMenuItem>
<RadioMenuItem mnemonicParsing="false" text="%language.es" toggleGroup="$language" onAction="#setLanguage">
<userData>
<Language fx:constant="SPANISH" />
</userData>
</RadioMenuItem>
</items>
</Menu>
<Menu fx:id="languagesMenu" mnemonicParsing="false" text="%language" />
<SeparatorMenuItem />
<CheckMenuItem fx:id="openWalletsInNewWindows" mnemonicParsing="false" text="Open Wallets In New Windows" onAction="#openWalletsInNewWindows"/>
<CheckMenuItem fx:id="hideEmptyUsedAddresses" mnemonicParsing="false" text="Hide Empty Used Addresses" onAction="#hideEmptyUsedAddresses"/>