From e82b5ef9bdc4fb110de7fbd043a835186fd64781 Mon Sep 17 00:00:00 2001 From: Aikar Date: Mon, 17 Jul 2017 23:25:30 -0400 Subject: [PATCH] Refactor supported languages and move MessageKeyProvider to co.a.locales --- .../aikar/commands/MinecraftMessageKeys.java | 1 + .../aikar/commands/MinecraftMessageKeys.java | 1 + .../java/co/aikar/commands/CommandIssuer.java | 1 + .../co/aikar/commands/CommandManager.java | 25 +++++++++++++++- .../commands/InvalidCommandArgument.java | 1 + .../main/java/co/aikar/commands/Locales.java | 28 ++++++++++++----- .../co/aikar/commands/MessageKeyProvider.java | 30 ------------------- .../java/co/aikar/commands/MessageKeys.java | 1 + .../aikar/commands/MinecraftMessageKeys.java | 1 + 9 files changed, 51 insertions(+), 38 deletions(-) delete mode 100644 core/src/main/java/co/aikar/commands/MessageKeyProvider.java diff --git a/bukkit/src/main/java/co/aikar/commands/MinecraftMessageKeys.java b/bukkit/src/main/java/co/aikar/commands/MinecraftMessageKeys.java index e438b2e1..5503a5be 100644 --- a/bukkit/src/main/java/co/aikar/commands/MinecraftMessageKeys.java +++ b/bukkit/src/main/java/co/aikar/commands/MinecraftMessageKeys.java @@ -24,6 +24,7 @@ package co.aikar.commands; import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; public enum MinecraftMessageKeys implements MessageKeyProvider { INVALID_WORLD, diff --git a/bungee/src/main/java/co/aikar/commands/MinecraftMessageKeys.java b/bungee/src/main/java/co/aikar/commands/MinecraftMessageKeys.java index af84b083..4b3df34d 100644 --- a/bungee/src/main/java/co/aikar/commands/MinecraftMessageKeys.java +++ b/bungee/src/main/java/co/aikar/commands/MinecraftMessageKeys.java @@ -1,6 +1,7 @@ package co.aikar.commands; import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; public enum MinecraftMessageKeys implements MessageKeyProvider { USERNAME_TOO_SHORT, diff --git a/core/src/main/java/co/aikar/commands/CommandIssuer.java b/core/src/main/java/co/aikar/commands/CommandIssuer.java index 738ba950..6a253cfd 100644 --- a/core/src/main/java/co/aikar/commands/CommandIssuer.java +++ b/core/src/main/java/co/aikar/commands/CommandIssuer.java @@ -24,6 +24,7 @@ package co.aikar.commands; import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; public interface CommandIssuer { /** diff --git a/core/src/main/java/co/aikar/commands/CommandManager.java b/core/src/main/java/co/aikar/commands/CommandManager.java index c6afc342..379cdc2c 100644 --- a/core/src/main/java/co/aikar/commands/CommandManager.java +++ b/core/src/main/java/co/aikar/commands/CommandManager.java @@ -24,6 +24,8 @@ package co.aikar.commands; import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; +import com.google.common.collect.Sets; import java.lang.reflect.Method; import java.lang.reflect.Parameter; @@ -39,6 +41,7 @@ public abstract class CommandManager { protected Map rootCommands = new HashMap<>(); protected CommandReplacements replacements = new CommandReplacements(this); protected ExceptionHandler defaultExceptionHandler = null; + protected Set supportedLanguages = Sets.newHashSet(Locale.ENGLISH); protected Map formatters = new IdentityHashMap<>(); { MessageFormatter plain = new MessageFormatter() { @@ -165,6 +168,7 @@ public abstract class CommandManager { public void sendMessage(Object issuerArg, MessageType type, MessageKeyProvider key, String... replacements) { sendMessage(issuerArg, type, key.getMessageKey(), replacements); } + public void sendMessage(Object issuerArg, MessageType type, MessageKey key, String... replacements) { CommandIssuer issuer = issuerArg instanceof CommandIssuer ? (CommandIssuer) issuerArg : getCommandIssuer(issuerArg); String message = getLocales().getMessage(issuer, key); @@ -180,7 +184,6 @@ public abstract class CommandManager { } } - public Locale getIssuerLocale(CommandIssuer issuer) { return getLocales().getDefaultLocale(); } @@ -194,4 +197,24 @@ public abstract class CommandManager { args ); } + + /** + * Gets a list of all currently supported languages for this manager. + * These locales will be automatically loaded from + * @return + */ + public Set getSupportedLanguages() { + return supportedLanguages; + } + + /** + * Adds a new locale to the list of automatic Locales to load Message Bundles for. + * All bundles loaded under the previous supported languages will now automatically load for this new locale too. + * + * @param locale + */ + public void addSupportedLanguage(Locale locale) { + supportedLanguages.add(locale); + getLocales().loadMissingBundles(); + } } diff --git a/core/src/main/java/co/aikar/commands/InvalidCommandArgument.java b/core/src/main/java/co/aikar/commands/InvalidCommandArgument.java index 43d2f3ca..e6cf2060 100644 --- a/core/src/main/java/co/aikar/commands/InvalidCommandArgument.java +++ b/core/src/main/java/co/aikar/commands/InvalidCommandArgument.java @@ -24,6 +24,7 @@ package co.aikar.commands; import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; public class InvalidCommandArgument extends Exception { final boolean showSyntax; diff --git a/core/src/main/java/co/aikar/commands/Locales.java b/core/src/main/java/co/aikar/commands/Locales.java index bd067f6b..347050a0 100644 --- a/core/src/main/java/co/aikar/commands/Locales.java +++ b/core/src/main/java/co/aikar/commands/Locales.java @@ -23,9 +23,10 @@ package co.aikar.commands; -import co.aikar.locales.LanguageTable; import co.aikar.locales.LocaleManager; import co.aikar.locales.MessageKey; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.SetMultimap; import org.jetbrains.annotations.NotNull; import java.util.HashMap; @@ -34,12 +35,9 @@ import java.util.Map; @SuppressWarnings("WeakerAccess") public class Locales { - - private static final Locale[] CORE_LANGUAGES = new Locale[]{ - Locale.ENGLISH - }; private final CommandManager manager; private final LocaleManager localeManager; + private final SetMultimap loadedBundles = HashMultimap.create(); Locales(CommandManager manager) { this.manager = manager; @@ -51,14 +49,30 @@ public class Locales { } + /** + * Looks for all previously loaded bundles, and if any new Supported Languages have been added, load them. + */ + public void loadMissingBundles() { + for (Locale locale : manager.getSupportedLanguages()) { + for (String bundleName : loadedBundles.keys()) { + addMessageBundle(bundleName, locale); + } + } + } + public void addMessageBundles(String... bundleNames) { for (String bundleName : bundleNames) { - this.localeManager.addMessageBundle(bundleName, CORE_LANGUAGES); + for (Locale locale : manager.getSupportedLanguages()) { + addMessageBundle(bundleName, locale); + } } } public void addMessageBundle(String bundleName, Locale locale) { - this.localeManager.addMessageBundle(bundleName, locale); + if (!loadedBundles.containsEntry(bundleName, locale)) { + loadedBundles.put(bundleName, locale); + this.localeManager.addMessageBundle(bundleName, locale); + } } public void addMessageStrings(Locale locale, @NotNull Map messages) { diff --git a/core/src/main/java/co/aikar/commands/MessageKeyProvider.java b/core/src/main/java/co/aikar/commands/MessageKeyProvider.java deleted file mode 100644 index 3b88cc9a..00000000 --- a/core/src/main/java/co/aikar/commands/MessageKeyProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -package co.aikar.commands; - -import co.aikar.locales.MessageKey; - -public interface MessageKeyProvider { - MessageKey getMessageKey(); -} diff --git a/core/src/main/java/co/aikar/commands/MessageKeys.java b/core/src/main/java/co/aikar/commands/MessageKeys.java index 9cb3a788..7ea7673b 100644 --- a/core/src/main/java/co/aikar/commands/MessageKeys.java +++ b/core/src/main/java/co/aikar/commands/MessageKeys.java @@ -24,6 +24,7 @@ package co.aikar.commands; import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; /** * Enum Name = MessageKey in lowercase prefixed with acf-core. diff --git a/sponge/src/main/java/co/aikar/commands/MinecraftMessageKeys.java b/sponge/src/main/java/co/aikar/commands/MinecraftMessageKeys.java index e438b2e1..5503a5be 100644 --- a/sponge/src/main/java/co/aikar/commands/MinecraftMessageKeys.java +++ b/sponge/src/main/java/co/aikar/commands/MinecraftMessageKeys.java @@ -24,6 +24,7 @@ package co.aikar.commands; import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; public enum MinecraftMessageKeys implements MessageKeyProvider { INVALID_WORLD,