remove static language and read i18dir in constructor

This commit is contained in:
Armin Schrenk
2023-05-08 20:28:56 +02:00
parent baee23b6d4
commit 0f9d572677
2 changed files with 31 additions and 65 deletions

View File

@@ -15,41 +15,12 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
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> LANGUAGE_TAGS;
static {
List<String> supportedLanguages = new ArrayList<>();
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")).get().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('_', '-');
}
public static final String ENGLISH = "en";
@@ -63,12 +34,13 @@ public class SupportedLanguages {
preferredLocale = preferredLanguage == null ? Locale.getDefault() : Locale.forLanguageTag(preferredLanguage);
var collator = Collator.getInstance(preferredLocale);
collator.setStrength(Collator.PRIMARY);
var sorted = LANGUAGE_TAGS.stream() //
List<String> sortedTags = new ArrayList<>();
sortedTags.add(0, Settings.DEFAULT_LANGUAGE);
sortedTags.add(1, ENGLISH);
getSupportedLanguageTags().stream() //
.sorted((a, b) -> collator.compare(Locale.forLanguageTag(a).getDisplayName(), Locale.forLanguageTag(b).getDisplayName())) //
.collect(Collectors.toList());
sorted.add(0, Settings.DEFAULT_LANGUAGE);
sorted.add(1, ENGLISH);
sortedLanguageTags = Collections.unmodifiableList(sorted);
.forEach(sortedTags::add);
sortedLanguageTags = Collections.unmodifiableList(sortedTags);
}
public void applyPreferred() {
@@ -80,4 +52,29 @@ public class SupportedLanguages {
return sortedLanguageTags;
}
/**
* Iterates over the /i18n directory and extracts from every localization file the BCP 47 code.
*
* @return list of supported BCP 47 language codes
*/
private static List<String> getSupportedLanguageTags() {
try {
var i18Dir = Path.of(SupportedLanguages.class.getResource("/i18n").toURI());
try (var dirStream = Files.newDirectoryStream(i18Dir, "strings_*.properties")) {
return StreamSupport.stream(dirStream.spliterator(), false) //
.map(SupportedLanguages::getBCP47CodeFromFileName) //
.toList();
}
} catch (URISyntaxException | IOException e) {
LOG.warn("Unable to determine additional supported languages.", e);
return List.of();
}
}
private static String getBCP47CodeFromFileName(Path p) {
var fileName = p.getFileName().toString();
return fileName.substring("strings_".length(), fileName.indexOf(".properties")).replace('_', '-');
}
}

View File

@@ -1,31 +0,0 @@
package org.cryptomator.launcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.stream.Stream;
public class SupportedLanguagesTest {
@DisplayName("test if resource bundle is localized")
@ParameterizedTest(name = "{0}")
@MethodSource("languageTags")
public void testResourceBundleExists(String tag) {
var locale = Locale.forLanguageTag(tag);
Assertions.assertNotEquals("und", locale.toLanguageTag(), "Undefined language tag");
var bundle = Assertions.assertDoesNotThrow(() -> ResourceBundle.getBundle("i18n.strings", locale));
Assertions.assertEquals(locale, bundle.getLocale());
Assertions.assertFalse(bundle.keySet().isEmpty());
}
public static Stream<String> languageTags() {
return SupportedLanguages.LANGUAGE_TAGS.stream() //
.filter(tag -> !"en".equals(tag)); // english uses the default bundle
}
}