Selaa lähdekoodia

Refactoring

make LANGUAGE_TAGS private and provide a getter
Jürgen Kleer 1 vuosi sitten
vanhempi
commit
8e7e7de358

+ 1 - 4
src/main/java/org/cryptomator/launcher/Cryptomator.java

@@ -35,15 +35,13 @@ public class Cryptomator {
 	private static final Logger LOG = LoggerFactory.getLogger(Cryptomator.class);
 
 	private final DebugMode debugMode;
-	private final SupportedLanguages supportedLanguages;
 	private final Environment env;
 	private final Lazy<IpcMessageHandler> ipcMessageHandler;
 	private final ShutdownHook shutdownHook;
 
 	@Inject
-	Cryptomator(DebugMode debugMode, SupportedLanguages supportedLanguages, Environment env, Lazy<IpcMessageHandler> ipcMessageHandler, ShutdownHook shutdownHook) {
+	Cryptomator(DebugMode debugMode, Environment env, Lazy<IpcMessageHandler> ipcMessageHandler, ShutdownHook shutdownHook) {
 		this.debugMode = debugMode;
-		this.supportedLanguages = supportedLanguages;
 		this.env = env;
 		this.ipcMessageHandler = ipcMessageHandler;
 		this.shutdownHook = shutdownHook;
@@ -80,7 +78,6 @@ public class Cryptomator {
 		LOG.debug("Dagger graph initialized after {}ms", System.currentTimeMillis() - STARTUP_TIME);
 		LOG.info("Starting Cryptomator {} on {} {} ({})", env.getAppVersion(), SystemUtils.OS_NAME, SystemUtils.OS_VERSION, SystemUtils.OS_ARCH);
 		debugMode.initialize();
-		supportedLanguages.applyPreferred();
 
 		/*
 		 * Attempts to create an IPC connection to a running Cryptomator instance and sends it the given args.

+ 16 - 12
src/main/java/org/cryptomator/launcher/SupportedLanguages.java

@@ -1,16 +1,16 @@
 package org.cryptomator.launcher;
 
 import org.cryptomator.common.settings.Settings;
-import org.jetbrains.annotations.Nullable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import java.text.Collator;
-import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
+import java.util.stream.Collectors;
 
 @Singleton
 public class SupportedLanguages {
@@ -18,20 +18,16 @@ public class SupportedLanguages {
 	private static final Logger LOG = LoggerFactory.getLogger(SupportedLanguages.class);
 	// these are BCP 47 language codes, not ISO. Note the "-" instead of the "_".
 	// "en" is not part of this list - it is always inserted at the top.
-	public static final List<String> LANGUAGE_TAGS = Arrays.asList("ar", "be", "bn", "bs", "ca", "cs", "da", "de", "el", "es", "fil", "fa", "fr", "gl", "he", //
+	public static final List<String> LANGUAGE_TAGS = List.of("ar", "be", "bn", "bs", "ca", "cs", "da", "de", "el", "es", "fil", "fa", "fr", "gl", "he", //
 			"hi", "hr", "hu", "id", "it", "ja", "ko", "lv", "mk", "nb", "nl", "nn", "no", "pa", "pl", "pt", "pt-BR", "ro", "ru", "si", "sk", "sr", "sr-Latn", "sv", "sw", //
 			"ta", "te", "th", "tr", "uk", "vi", "zh", "zh-HK", "zh-TW");
-	public  static final String ENGLISH = "en";
+	public static final String ENGLISH = "en";
 
-	@Nullable
-	private final String preferredLanguage;
+	private final List<String> sortedLanguageTags;
 
 	@Inject
 	public SupportedLanguages(Settings settings) {
-		this.preferredLanguage = settings.languageProperty().get();
-	}
-
-	public void applyPreferred() {
+		var preferredLanguage = settings.languageProperty().get();
 		if (preferredLanguage == null) {
 			LOG.debug("Using system locale");
 		} else {
@@ -39,10 +35,18 @@ public class SupportedLanguages {
 			LOG.debug("Applying preferred locale {}", preferredLocale.getDisplayName(Locale.ENGLISH));
 			Locale.setDefault(preferredLocale);
 		}
-		SupportedLanguages.LANGUAGE_TAGS.sort((a, b) -> {
+		sortedLanguageTags = LANGUAGE_TAGS.stream().sorted((a, b) -> {
 			var collator = Collator.getInstance(Locale.getDefault());
 			collator.setStrength(Collator.PRIMARY);
 			return collator.compare(Locale.forLanguageTag(a).getDisplayName(), Locale.forLanguageTag(b).getDisplayName());
-		});
+		}).collect(Collectors.toList());
+		sortedLanguageTags.add(0, Settings.DEFAULT_LANGUAGE);
+		sortedLanguageTags.add(1, ENGLISH);
+
 	}
+
+	public List<String> getLanguageTags() {
+		return Collections.unmodifiableList(sortedLanguageTags);
+	}
+
 }

+ 4 - 5
src/main/java/org/cryptomator/ui/preferences/InterfacePreferencesController.java

@@ -34,6 +34,7 @@ public class InterfacePreferencesController implements FxController {
 	private final ObjectProperty<SelectedPreferencesTab> selectedTabProperty;
 	private final LicenseHolder licenseHolder;
 	private final ResourceBundle resourceBundle;
+	private final SupportedLanguages supportedLanguages;
 	public ChoiceBox<UiTheme> themeChoiceBox;
 	public CheckBox showMinimizeButtonCheckbox;
 	public CheckBox showTrayIconCheckbox;
@@ -43,13 +44,14 @@ public class InterfacePreferencesController implements FxController {
 	public RadioButton nodeOrientationRtl;
 
 	@Inject
-	InterfacePreferencesController(Settings settings, TrayMenuComponent trayMenu, ObjectProperty<SelectedPreferencesTab> selectedTabProperty, LicenseHolder licenseHolder, ResourceBundle resourceBundle) {
+	InterfacePreferencesController(Settings settings, SupportedLanguages supportedLanguages, TrayMenuComponent trayMenu, ObjectProperty<SelectedPreferencesTab> selectedTabProperty, LicenseHolder licenseHolder, ResourceBundle resourceBundle) {
 		this.settings = settings;
 		this.trayMenuInitialized = trayMenu.isInitialized();
 		this.trayMenuSupported = trayMenu.isSupported();
 		this.selectedTabProperty = selectedTabProperty;
 		this.licenseHolder = licenseHolder;
 		this.resourceBundle = resourceBundle;
+		this.supportedLanguages = supportedLanguages;
 	}
 
 	@FXML
@@ -65,10 +67,7 @@ public class InterfacePreferencesController implements FxController {
 
 		showTrayIconCheckbox.selectedProperty().bindBidirectional(settings.showTrayIcon());
 
-		// System default and English at the top of the list
-		preferredLanguageChoiceBox.getItems().add(Settings.DEFAULT_LANGUAGE);
-		preferredLanguageChoiceBox.getItems().add(SupportedLanguages.ENGLISH);
-		preferredLanguageChoiceBox.getItems().addAll(SupportedLanguages.LANGUAGE_TAGS);
+		preferredLanguageChoiceBox.getItems().addAll(supportedLanguages.getLanguageTags());
 		preferredLanguageChoiceBox.valueProperty().bindBidirectional(settings.languageProperty());
 		preferredLanguageChoiceBox.setConverter(new LanguageTagConverter(resourceBundle));