From 625be46b3054b67a267aa60e7a77843a186e4ca4 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Sun, 17 Nov 2019 16:04:13 +1000 Subject: [PATCH] Allow translation files to be missing entries, and use the english version in place. --- .../util/translation/TranslationManager.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java index b98cb263e..1a2dbf9b8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java @@ -76,12 +76,17 @@ public class TranslationManager { this.defaultLocale = defaultLocale; } + private Map filterTranslations(Map translations) { + translations.entrySet().removeIf(entry -> entry.getValue().isEmpty()); + return translations; + } + private Map parseTranslationFile(File file) throws IOException { - return gson.fromJson(Files.toString(file, StandardCharsets.UTF_8), STRING_MAP_TYPE); + return filterTranslations(gson.fromJson(Files.toString(file, StandardCharsets.UTF_8), STRING_MAP_TYPE)); } private Map parseTranslationFile(URL file) throws IOException { - return gson.fromJson(Resources.toString(file, StandardCharsets.UTF_8), STRING_MAP_TYPE); + return filterTranslations(gson.fromJson(Resources.toString(file, StandardCharsets.UTF_8), STRING_MAP_TYPE)); } private Optional> loadTranslationFile(String filename) { @@ -112,12 +117,18 @@ public class TranslationManager { return false; } checkedLocales.add(locale); + // Make a copy of the default language file + Map baseTranslations = new HashMap<>(); + if (!locale.equals(defaultLocale)) { + baseTranslations.putAll(getTranslationMap(defaultLocale)); + } Optional> langData = loadTranslationFile(locale.getLanguage() + "-" + locale.getCountry() + "/strings.json"); if (!langData.isPresent()) { langData = loadTranslationFile(locale.getLanguage() + "/strings.json"); } if (langData.isPresent()) { - translationMap.put(locale, langData.get()); + baseTranslations.putAll(langData.get()); + translationMap.put(locale, baseTranslations); return true; } if (locale.equals(defaultLocale)) {