TranslationManager fixes and improvements (#3165)

This commit is contained in:
Luck 2021-09-18 17:30:17 +01:00
parent d1cb644712
commit 2cf77db8b6
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 16 additions and 14 deletions

View File

@ -40,6 +40,7 @@ import me.lucko.luckperms.common.util.Predicates;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.io.IOException; import java.io.IOException;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -70,12 +71,12 @@ public class TranslationsCommand extends SingleCommand {
return; 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); Message.AVAILABLE_TRANSLATIONS_HEADER.send(sender);
for (LanguageInfo language : availableTranslations) { availableTranslations.stream()
Message.AVAILABLE_TRANSLATIONS_ENTRY.send(sender, language.locale().toString(), TranslationManager.localeDisplayName(language.locale()), language.progress(), language.contributors()); .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())); sender.sendMessage(Message.prefixed(Component.empty()));
Message.TRANSLATIONS_DOWNLOAD_PROMPT.send(sender, label); Message.TRANSLATIONS_DOWNLOAD_PROMPT.send(sender, label);
} }

View File

@ -123,9 +123,10 @@ public class TranslationManager {
for (Path translationFile : translationFiles) { for (Path translationFile : translationFiles) {
try { try {
Map.Entry<Locale, ResourceBundle> result = loadCustomTranslationFile(translationFile); Map.Entry<Locale, ResourceBundle> 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) { } catch (Exception e) {
this.plugin.getLogger().warn("Error loading locale file: " + translationFile.getFileName(), e); this.plugin.getLogger().warn("Error loading locale file: " + translationFile.getFileName(), e);
} }
@ -135,27 +136,27 @@ public class TranslationManager {
loaded.forEach((locale, bundle) -> { loaded.forEach((locale, bundle) -> {
Locale localeWithoutCountry = new Locale(locale.getLanguage()); Locale localeWithoutCountry = new Locale(locale.getLanguage());
if (!locale.equals(localeWithoutCountry) && !localeWithoutCountry.equals(DEFAULT_LOCALE) && this.installed.add(localeWithoutCountry)) { 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<Locale, ResourceBundle> loadCustomTranslationFile(Path translationFile) { private Map.Entry<Locale, ResourceBundle> loadCustomTranslationFile(Path translationFile) throws IOException {
String fileName = translationFile.getFileName().toString(); String fileName = translationFile.getFileName().toString();
String localeString = fileName.substring(0, fileName.length() - ".properties".length()); String localeString = fileName.substring(0, fileName.length() - ".properties".length());
Locale locale = parseLocale(localeString); Locale locale = parseLocale(localeString);
if (locale == null) { if (locale == null) {
this.plugin.getLogger().warn("Unknown locale '" + localeString + "' - unable to register."); throw new IllegalStateException("Unknown locale '" + localeString + "' - unable to register.");
return null;
} }
PropertyResourceBundle bundle; PropertyResourceBundle bundle;
try (BufferedReader reader = Files.newBufferedReader(translationFile, StandardCharsets.UTF_8)) { try (BufferedReader reader = Files.newBufferedReader(translationFile, StandardCharsets.UTF_8)) {
bundle = new PropertyResourceBundle(reader); 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); this.registry.registerAll(locale, bundle, false);