mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-05-17 02:01:27 +00:00
load supported languages on app start
This commit is contained in:
@@ -7,17 +7,48 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
@Singleton
|
||||
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 "_":
|
||||
public static final List<String> LANGUAGAE_TAGS = List.of("en", "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 List<String> LANGUAGE_TAGS;
|
||||
|
||||
static {
|
||||
List<String> supportedLanguages = new ArrayList<>();
|
||||
supportedLanguages.add("en");
|
||||
try {
|
||||
var i18Dir = getI18Dir();
|
||||
try (var dirStream = Files.newDirectoryStream(i18Dir, "strings_*.properties")) {
|
||||
StreamSupport.stream(dirStream.spliterator(), false) //
|
||||
.map(SupportedLanguages::getBCP47CodeFromFileName) //
|
||||
.forEach(supportedLanguages::add);
|
||||
}
|
||||
} catch (URISyntaxException | IOException e) {
|
||||
LOG.warn("Unable to determine additional supported languages.", e);
|
||||
}
|
||||
LANGUAGE_TAGS = supportedLanguages;
|
||||
}
|
||||
|
||||
private static Path getI18Dir() throws URISyntaxException {
|
||||
var i18nUri = Optional.of(SupportedLanguages.class.getResource("/i18n")).orElseThrow().toURI();
|
||||
return Path.of(i18nUri);
|
||||
}
|
||||
|
||||
private static String getBCP47CodeFromFileName(Path p) {
|
||||
var fileName = p.getFileName().toString();
|
||||
return fileName.substring("strings_".length(), fileName.indexOf(".properties")).replace('_', '-');
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private final String preferredLanguage;
|
||||
|
||||
@@ -67,7 +67,7 @@ public class InterfacePreferencesController implements FxController {
|
||||
showTrayIconCheckbox.selectedProperty().bindBidirectional(settings.showTrayIcon());
|
||||
|
||||
preferredLanguageChoiceBox.getItems().add(null);
|
||||
preferredLanguageChoiceBox.getItems().addAll(SupportedLanguages.LANGUAGAE_TAGS);
|
||||
preferredLanguageChoiceBox.getItems().addAll(SupportedLanguages.LANGUAGE_TAGS);
|
||||
preferredLanguageChoiceBox.valueProperty().bindBidirectional(settings.languageProperty());
|
||||
preferredLanguageChoiceBox.setConverter(new LanguageTagConverter(resourceBundle));
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public class SupportedLanguagesTest {
|
||||
}
|
||||
|
||||
public static Stream<String> languageTags() {
|
||||
return SupportedLanguages.LANGUAGAE_TAGS.stream() //
|
||||
return SupportedLanguages.LANGUAGE_TAGS.stream() //
|
||||
.filter(tag -> !"en".equals(tag)); // english uses the default bundle
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user