ton more I18N/Locales work, messages migrated!

This commit is contained in:
Aikar
2017-06-29 00:36:59 -04:00
parent a93d1cf5bc
commit c9acb55a48
21 changed files with 85 additions and 64 deletions
+1 -1
View File
@@ -8,6 +8,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>
+1 -1
View File
@@ -29,6 +29,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.18" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>
@@ -53,8 +53,8 @@ public class BukkitCommandIssuer implements CommandIssuer {
}
@Override
public void sendMessage(MessageType type, String message) {
sender.sendMessage(ACFBukkitUtil.color(format(manager, type, message)));
public void sendMessageInternal(String message) {
sender.sendMessage(ACFBukkitUtil.color(message));
}
@Override
@@ -83,7 +83,6 @@ public class BukkitRootCommand extends Command implements RootCommand {
if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
this.defCommand = command;
this.setPermission(command.permission);
this.setPermissionMessage(command.permissionMessage);
//this.setDescription(command.getDescription());
//this.setUsage(command.getUsage());
}
+1 -1
View File
@@ -34,6 +34,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-resolver:4.1.12.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.sf.trove4j:trove4j:3.0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:20.0" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>
@@ -54,8 +54,7 @@ public class BungeeCommandIssuer implements CommandIssuer{
}
@Override
public void sendMessage(MessageType type, String message) {
message = format(manager, type, message);
public void sendMessageInternal(String message) {
sender.sendMessage(new TextComponent(ACFBungeeUtil.color(message)));
}
@@ -74,7 +74,6 @@ public abstract class BaseCommand {
String commandName;
String usageMessage;
String permission;
public String permissionMessage = "&cI'm sorry, but you do not have permission to perform this command.";
private ExceptionHandler exceptionHandler = null;
@@ -315,7 +314,7 @@ public abstract class BaseCommand {
public void execute(CommandIssuer issuer, String commandLabel, String[] args) {
if (!this.hasPermission(issuer)) {
issuer.sendMessage(MessageType.ERROR, permissionMessage);
issuer.sendMessage(MessageType.ERROR, MessageKeys.PERMISSION_DENIED);
return;
}
commandLabel = commandLabel.toLowerCase();
@@ -393,7 +392,7 @@ public abstract class BaseCommand {
List<String> sargs = Lists.newArrayList(args);
cmd.invoke(issuer, sargs);
} else {
issuer.sendMessage(MessageType.ERROR, cmd.scope.permissionMessage);
issuer.sendMessage(MessageType.ERROR, MessageKeys.PERMISSION_DENIED);
}
}
@@ -489,7 +488,7 @@ public abstract class BaseCommand {
help(manager.getCommandIssuer(issuer), args);
}
public void help(CommandIssuer issuer, String[] args) {
issuer.sendMessage(MessageType.ERROR, "&cUnknown Command, please type &f/help");
issuer.sendMessage(MessageType.ERROR, MessageKeys.UNKNOWN_COMMAND);
}
public void doHelp(Object issuer, String... args) {
doHelp(manager.getCommandIssuer(issuer), args);
@@ -499,7 +498,10 @@ public abstract class BaseCommand {
}
public void showSyntax(CommandIssuer issuer, RegisteredCommand<?> cmd) {
issuer.sendMessage(MessageType.SYNTAX, "Usage: <c2>/" + cmd.command + "</c2> <c3>" + cmd.syntaxText + "</c3>");
issuer.sendMessage(MessageType.SYNTAX, MessageKeys.INVALID_SYNTAX,
"{command}", "/" + cmd.command,
"{syntax}", cmd.syntaxText
);
}
public boolean hasPermission(Object issuer) {
@@ -46,7 +46,7 @@ public interface CommandIssuer {
* @param message
*/
default void sendMessage(String message) {
sendMessage(MessageType.ERROR, message);
getManager().sendMessage(this, MessageType.INFO, MessageKeys.INFO_MESSAGE, "{message}", message);
}
/**
@@ -60,13 +60,10 @@ public interface CommandIssuer {
getManager().sendMessage(this, type, key, replacements);
}
void sendMessage(MessageType type, String message);
default String format(CommandManager manager, MessageType type, String message) {
MessageFormatter formatter = manager.formatters.get(type);
if (formatter != null) {
message = formatter.format(message);
}
return message;
}
/**
* @deprecated Do not call this, for internal use. Not considered part of the API and may break.
* @param message
*/
@Deprecated
void sendMessageInternal(String message);
}
@@ -157,9 +157,14 @@ abstract class CommandManager {
if (replacements.length > 0) {
message = ACFUtil.replaceStrings(message, replacements);
}
issuer.sendMessage(type, message);
MessageFormatter formatter = formatters.get(type);
if (formatter != null) {
message = formatter.format(message);
}
issuer.sendMessageInternal(message);
}
public Locale getIssuerLocale(CommandIssuer issuer) {
return getLocales().getDefaultLocale();
}
@@ -23,6 +23,7 @@
package co.aikar.commands;
import co.aikar.locales.LanguageTable;
import co.aikar.locales.LocaleManager;
import co.aikar.locales.MessageKey;
import org.jetbrains.annotations.NotNull;
@@ -30,12 +31,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Locale;
import java.util.Map;
/**
* This isn't public yet, still WIP - API will break
* @deprecated
*/
@SuppressWarnings("WeakerAccess")
@Deprecated
public class Locales {
private final CommandManager manager;
@@ -44,36 +40,27 @@ public class Locales {
Locales(CommandManager manager) {
this.manager = manager;
this.localeManager = LocaleManager.create(manager::getIssuerLocale);
this.initializeSystemMessages();
}
private void initializeSystemMessages() {
//table.addMessage(MessageKey.FOO, "bar");
}
/**
* Changes the default locale to use if the specified language key is missing for the desired locale
* @param locale
* @return Previous default locale
*/
public Locale setDefaultLocale(Locale locale) {
return localeManager.setDefaultLocale(locale);
this.localeManager.addMessageBundle("acf-core", Locale.ENGLISH);
}
public Locale getDefaultLocale() {
return localeManager.getDefaultLocale();
return this.localeManager.getDefaultLocale();
}
public void addMessageBundle(String bundleName, Locale locale) {
this.localeManager.addMessageBundle(bundleName, locale);
}
public void addMessages(Locale locale, @NotNull Map<MessageKey, String> messages) {
localeManager.addMessages(locale, messages);
this.localeManager.addMessages(locale, messages);
}
public String addMessage(Locale locale, MessageKey key, String message) {
return localeManager.addMessage(locale, key, message);
return this.localeManager.addMessage(locale, key, message);
}
public String getMessage(CommandIssuer issuer, MessageKey key) {
String message = localeManager.getMessage(issuer, key);
String message = this.localeManager.getMessage(issuer, key);
if (message == null) {
manager.log(LogLevel.ERROR, "Missing Language Key: " + key);
message = "<MISSING_LANGUAGE_KEY:" + key + ">";
@@ -35,20 +35,35 @@ public abstract class MessageFormatter <C> {
private final List<C> colors = new ArrayList<>();
@SafeVarargs
public MessageFormatter(C... colors) {
for (int i = 0; i < colors.length; i++) {
this.colors.set(i, colors[i]);
}
this.colors.addAll(Arrays.asList(colors));
}
public C setColor(int index, C color) {
if (this.colors.size() < index) {
this.colors.addAll(Collections.nCopies(index - this.colors.size(), null));
if (index > 0) {
index--;
} else {
index = 0;
}
if (this.colors.size() <= index) {
int needed = index - this.colors.size();
if (needed > 0) {
this.colors.addAll(Collections.nCopies(needed, null));
}
colors.add(color);
return null;
} else {
return colors.set(index, color);
}
return colors.set(index, color);
}
public C getColor(int index) {
if (index > 0) {
index--;
} else {
index = 0;
}
C color = colors.get(index);
if (color == null) {
color = getDefaultColor();
@@ -25,6 +25,13 @@ package co.aikar.commands;
import co.aikar.locales.MessageKey;
public class MessageKeys {
public static final MessageKey KEY = MessageKey.of("");
@SuppressWarnings("WeakerAccess")
public class MessageKeys {
public static final MessageKey PERMISSION_DENIED = MessageKey.of("permission_denied");
public static final MessageKey ERROR_GENERIC_LOGGED = MessageKey.of("error_generic_logged");
public static final MessageKey UNKNOWN_COMMAND = MessageKey.of("unknown_command");
public static final MessageKey INVALID_SYNTAX = MessageKey.of("invalid_syntax");
public static final MessageKey ERROR_PREFIX = MessageKey.of("error_prefix");
public static final MessageKey ERROR_PERFORMING_COMMAND = MessageKey.of("error_performing_command");
public static final MessageKey INFO_MESSAGE = MessageKey.of("info_message");
}
@@ -27,11 +27,13 @@ import java.util.concurrent.atomic.AtomicInteger;
@SuppressWarnings("WeakerAccess")
public class MessageType {
private static final AtomicInteger counter = new AtomicInteger(1);
public static MessageType INFO = new MessageType();
public static MessageType SYNTAX = new MessageType();
public static MessageType ERROR = new MessageType();
private static final AtomicInteger counter = new AtomicInteger();
private final int id = counter.getAndIncrement();
public int hashCode() {
@@ -154,7 +154,7 @@ public class RegisteredCommand <R extends CommandExecutionContext<? extends Comm
}
if (e instanceof InvalidCommandArgument) {
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
sender.sendMessage(MessageType.ERROR, "&cError: " + e.getMessage());
sender.sendMessage(MessageType.ERROR, MessageKeys.ERROR_PREFIX, "{message}", e.getMessage());
}
if (((InvalidCommandArgument) e).showSyntax) {
scope.showSyntax(sender, this);
@@ -162,7 +162,7 @@ public class RegisteredCommand <R extends CommandExecutionContext<? extends Comm
} else {
boolean handeled = this.scope.manager.handleUncaughtException(scope, this, sender, args, e);
if(!handeled){
sender.sendMessage(MessageType.ERROR, "&cI'm sorry, but there was an error performing this command.");
sender.sendMessage(MessageType.ERROR, MessageKeys.ERROR_PERFORMING_COMMAND);
}
this.scope.manager.log(LogLevel.ERROR, "Exception in command: " + command + " " + ACFUtil.join(args), e);
}
@@ -0,0 +1,7 @@
permission_denied = I'm sorry, but you do not have permission to perform this command.
error_generic_logged = An error occured. This problem has been logged. Sorry for the inconvienence.
unknown_command = Unknown Command, please type <c2>/help</c2>
invalid_syntax = Usage: <c2>{command}</c2> <c3>{syntax}</c3>
error_prefix = Error: {message}
error_performing_command = I'm sorry, but there was an error performing this command.
info_message = {message}
+1 -1
View File
@@ -36,8 +36,8 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm-all:5.0.4" level="project" />
<orderEntry type="module" module-name="acf-bukkit" />
<orderEntry type="module" module-name="acf-core" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:15.0" level="project" />
<orderEntry type="library" name="Maven: co.aikar:locales:1.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: co.aikar:minecraft-timings:1.0.4" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>
@@ -24,6 +24,7 @@
package co.aikar.acfexample;
import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.MessageKeys;
import co.aikar.commands.MessageType;
import com.google.common.collect.Lists;
import org.bukkit.plugin.java.JavaPlugin;
@@ -48,7 +49,7 @@ public final class ACFExample extends JavaPlugin {
));
commandManager.registerCommand(new SomeCommand().setExceptionHandler((command, registeredCommand, sender, args, t) -> {
sender.sendMessage(MessageType.ERROR, "An error occured. This problem has been logged. Sorry for the inconvienence.");
sender.sendMessage(MessageType.ERROR, MessageKeys.ERROR_GENERIC_LOGGED);
return true; // mark as handeled, default message will not be send to sender
}));
commandManager.registerCommand(new SomeCommand_ExtraSubs());
+1 -1
View File
@@ -39,6 +39,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.18" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.12-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm-all:5.0.4" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>
+1
View File
@@ -110,6 +110,7 @@
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
+1 -1
View File
@@ -41,6 +41,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: com.flowpowered:flow-math:1.0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.flowpowered:flow-noise:1.0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm:5.0.3" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>
@@ -56,8 +56,7 @@ public class SpongeCommandIssuer implements CommandIssuer {
}
@Override
public void sendMessage(MessageType type, String message) {
message = format(manager, type, message);
public void sendMessageInternal(String message) {
this.source.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message));
}