From 2cf77db8b639f494f2060c1ffd6f7748136400f3 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 18 Sep 2021 17:30:17 +0100 Subject: [PATCH] TranslationManager fixes and improvements (#3165) --- .../commands/misc/TranslationsCommand.java | 9 ++++---- .../common/locale/TranslationManager.java | 21 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java index 76061e9d4..052031cb9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java @@ -40,6 +40,7 @@ import me.lucko.luckperms.common.util.Predicates; import net.kyori.adventure.text.Component; import java.io.IOException; +import java.util.Comparator; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; @@ -70,12 +71,12 @@ public class TranslationsCommand extends SingleCommand { return; } - Message.INSTALLED_TRANSLATIONS.send(sender, plugin.getTranslationManager().getInstalledLocales().stream().map(Locale::toString).collect(Collectors.toList())); + Message.INSTALLED_TRANSLATIONS.send(sender, plugin.getTranslationManager().getInstalledLocales().stream().map(Locale::toLanguageTag).sorted().collect(Collectors.toList())); Message.AVAILABLE_TRANSLATIONS_HEADER.send(sender); - for (LanguageInfo language : availableTranslations) { - Message.AVAILABLE_TRANSLATIONS_ENTRY.send(sender, language.locale().toString(), TranslationManager.localeDisplayName(language.locale()), language.progress(), language.contributors()); - } + availableTranslations.stream() + .sorted(Comparator.comparing(language -> language.locale().toLanguageTag())) + .forEach(language -> Message.AVAILABLE_TRANSLATIONS_ENTRY.send(sender, language.locale().toLanguageTag(), TranslationManager.localeDisplayName(language.locale()), language.progress(), language.contributors())); sender.sendMessage(Message.prefixed(Component.empty())); Message.TRANSLATIONS_DOWNLOAD_PROMPT.send(sender, label); } diff --git a/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java b/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java index f14f83efb..d02094a57 100644 --- a/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java @@ -123,9 +123,10 @@ public class TranslationManager { for (Path translationFile : translationFiles) { try { Map.Entry result = loadCustomTranslationFile(translationFile); - if (result != null) { - loaded.put(result.getKey(), result.getValue()); - } + loaded.put(result.getKey(), result.getValue()); + } catch (IllegalArgumentException e) { + // common error is from adventure "java.lang.IllegalArgumentException: Invalid key" -- don't print the whole stack trace. + this.plugin.getLogger().warn("Error loading locale file: " + translationFile.getFileName() + " - " + e); } catch (Exception e) { this.plugin.getLogger().warn("Error loading locale file: " + translationFile.getFileName(), e); } @@ -135,27 +136,27 @@ public class TranslationManager { loaded.forEach((locale, bundle) -> { Locale localeWithoutCountry = new Locale(locale.getLanguage()); if (!locale.equals(localeWithoutCountry) && !localeWithoutCountry.equals(DEFAULT_LOCALE) && this.installed.add(localeWithoutCountry)) { - this.registry.registerAll(localeWithoutCountry, bundle, false); + try { + this.registry.registerAll(localeWithoutCountry, bundle, false); + } catch (IllegalArgumentException e) { + // ignore "IllegalArgumentException: Invalid key" from adventure TranslationRegistry + } } }); } - private Map.Entry loadCustomTranslationFile(Path translationFile) { + private Map.Entry loadCustomTranslationFile(Path translationFile) throws IOException { String fileName = translationFile.getFileName().toString(); String localeString = fileName.substring(0, fileName.length() - ".properties".length()); Locale locale = parseLocale(localeString); if (locale == null) { - this.plugin.getLogger().warn("Unknown locale '" + localeString + "' - unable to register."); - return null; + throw new IllegalStateException("Unknown locale '" + localeString + "' - unable to register."); } PropertyResourceBundle bundle; try (BufferedReader reader = Files.newBufferedReader(translationFile, StandardCharsets.UTF_8)) { bundle = new PropertyResourceBundle(reader); - } catch(IOException e) { - this.plugin.getLogger().warn("Error loading locale file: " + localeString, e); - return null; } this.registry.registerAll(locale, bundle, false);