Refactor supported languages and move MessageKeyProvider to co.a.locales

This commit is contained in:
Aikar
2017-07-17 23:25:30 -04:00
parent 668fa650ba
commit e82b5ef9bd
9 changed files with 51 additions and 38 deletions
@@ -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,
@@ -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,
@@ -24,6 +24,7 @@
package co.aikar.commands;
import co.aikar.locales.MessageKey;
import co.aikar.locales.MessageKeyProvider;
public interface CommandIssuer {
/**
@@ -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<String, RootCommand> rootCommands = new HashMap<>();
protected CommandReplacements replacements = new CommandReplacements(this);
protected ExceptionHandler defaultExceptionHandler = null;
protected Set<Locale> supportedLanguages = Sets.newHashSet(Locale.ENGLISH);
protected Map<MessageType, MessageFormatter> formatters = new IdentityHashMap<>();
{
MessageFormatter plain = new MessageFormatter<Object>() {
@@ -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<Locale> 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();
}
}
@@ -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;
@@ -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<CommandIssuer> localeManager;
private final SetMultimap<String, Locale> 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<String, String> messages) {
@@ -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();
}
@@ -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.
@@ -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,