#293 Skip help sections if translation is empty

This commit is contained in:
ljacqu 2016-10-08 14:25:42 +02:00
parent 6b1112438a
commit f453a5b4f5
10 changed files with 125 additions and 100 deletions

View File

@ -4,15 +4,7 @@ package fr.xephi.authme.command.help;
* Common, non-generic keys for messages used when showing command help.
* All keys are prefixed with {@code common}.
*/
public enum HelpMessageKey {
SHORT_DESCRIPTION("description.short"),
DETAILED_DESCRIPTION("description.detailed"),
USAGE("usage"),
ARGUMENTS("arguments"),
public enum HelpMessage {
OPTIONAL("optional"),
@ -20,15 +12,9 @@ public enum HelpMessageKey {
NO_PERMISSION("noPermission"),
ALTERNATIVES("alternatives"),
DEFAULT("default"),
RESULT("result"),
PERMISSIONS("permissions"),
COMMANDS("commands");
RESULT("result");
private final String key;
@ -38,7 +24,7 @@ public enum HelpMessageKey {
*
* @param key the message key
*/
HelpMessageKey(String key) {
HelpMessage(String key) {
this.key = "common." + key;
}

View File

@ -65,8 +65,12 @@ public class HelpMessagesService implements Reloadable {
return builder.build();
}
public String getMessage(HelpMessageKey key) {
return messageFileHandler.getMessage(key.getKey());
public String getMessage(HelpMessage message) {
return messageFileHandler.getMessage(message.getKey());
}
public String getMessage(HelpSection section) {
return messageFileHandler.getMessage(section.getKey());
}
public String getMessage(DefaultPermission defaultPermission) {

View File

@ -19,8 +19,8 @@ import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static fr.xephi.authme.command.help.HelpMessageKey.DETAILED_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpMessageKey.SHORT_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpSection.DETAILED_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpSection.SHORT_DESCRIPTION;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
@ -32,16 +32,18 @@ public class HelpProvider implements SettingsDependent {
// --- Bit flags ---
/** Set to <i>not</i> show the command. */
public static final int HIDE_COMMAND = 0x001;
/** Set to show the detailed description of a command. */
public static final int SHOW_LONG_DESCRIPTION = 0x002;
/** Set to show the description of the command. */
public static final int SHOW_DESCRIPTION = 0x002;
/** Set to show the detailed description of the command. */
public static final int SHOW_LONG_DESCRIPTION = 0x004;
/** Set to include the arguments the command takes. */
public static final int SHOW_ARGUMENTS = 0x004;
public static final int SHOW_ARGUMENTS = 0x008;
/** Set to show the permissions required to execute the command. */
public static final int SHOW_PERMISSIONS = 0x008;
public static final int SHOW_PERMISSIONS = 0x010;
/** Set to show alternative labels for the command. */
public static final int SHOW_ALTERNATIVES = 0x010;
public static final int SHOW_ALTERNATIVES = 0x020;
/** Set to show the child commands of the command. */
public static final int SHOW_CHILDREN = 0x020;
public static final int SHOW_CHILDREN = 0x040;
/** Shortcut for setting all options apart from {@link HelpProvider#HIDE_COMMAND}. */
public static final int ALL_OPTIONS = ~HIDE_COMMAND;
@ -72,8 +74,18 @@ public class HelpProvider implements SettingsDependent {
if (!hasFlag(HIDE_COMMAND, options)) {
lines.add(ChatColor.GOLD + "Command: " + CommandSyntaxHelper.getSyntax(command, correctLabels));
}
if (hasFlag(SHOW_DESCRIPTION, options)) {
String description = helpMessagesService.getMessage(SHORT_DESCRIPTION);
if (!description.isEmpty()) {
lines.add(ChatColor.GOLD + description + ": " + ChatColor.WHITE + command.getDescription());
}
}
if (hasFlag(SHOW_LONG_DESCRIPTION, options)) {
printDetailedDescription(command, lines);
String description = helpMessagesService.getMessage(DETAILED_DESCRIPTION);
if (!description.isEmpty()) {
lines.add(ChatColor.GOLD + description + ":");
lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
}
}
if (hasFlag(SHOW_ARGUMENTS, options)) {
printArguments(command, lines);
@ -110,22 +122,18 @@ public class HelpProvider implements SettingsDependent {
helpHeader = settings.getProperty(PluginSettings.HELP_HEADER);
}
private void printDetailedDescription(CommandDescription command, List<String> lines) {
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(SHORT_DESCRIPTION) + ": "
+ ChatColor.WHITE + command.getDescription());
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(DETAILED_DESCRIPTION) + ":");
lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
}
private void printArguments(CommandDescription command, List<String> lines) {
if (command.getArguments().isEmpty()) {
return;
}
String arguments = helpMessagesService.getMessage(HelpSection.ARGUMENTS);
if (arguments.isEmpty()) {
return;
}
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessageKey.ARGUMENTS) + ":");
lines.add(ChatColor.GOLD + arguments + ":");
StringBuilder argString = new StringBuilder();
String optionalText = " (" + helpMessagesService.getMessage(HelpMessageKey.OPTIONAL) + ")";
String optionalText = " (" + helpMessagesService.getMessage(HelpMessage.OPTIONAL) + ")";
for (CommandArgumentDescription argument : command.getArguments()) {
argString.setLength(0);
argString.append(" ").append(ChatColor.YELLOW).append(ChatColor.ITALIC).append(argument.getName())
@ -142,8 +150,12 @@ public class HelpProvider implements SettingsDependent {
if (command.getLabels().size() <= 1 || correctLabels.size() <= 1) {
return;
}
String alternatives = helpMessagesService.getMessage(HelpSection.ALTERNATIVES);
if (alternatives.isEmpty()) {
return;
}
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessageKey.ALTERNATIVES) + ":");
lines.add(ChatColor.GOLD + alternatives + ":");
// Get the label used
final String parentLabel = correctLabels.get(0);
final String childLabel = correctLabels.get(1);
@ -158,10 +170,11 @@ public class HelpProvider implements SettingsDependent {
private void printPermissions(CommandDescription command, CommandSender sender, List<String> lines) {
PermissionNode permission = command.getPermission();
if (permission == null) {
String permissionsTitle = helpMessagesService.getMessage(HelpSection.PERMISSIONS);
if (permission == null || permissionsTitle.isEmpty()) {
return;
}
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessageKey.PERMISSIONS) + ":");
lines.add(ChatColor.GOLD + permissionsTitle + ":");
boolean hasPermission = permissionsManager.hasPermission(sender, permission);
lines.add(String.format(" " + ChatColor.YELLOW + ChatColor.ITALIC + "%s" + ChatColor.GRAY + " (%s)",
@ -173,7 +186,7 @@ public class HelpProvider implements SettingsDependent {
if (DefaultPermission.OP_ONLY.equals(defaultPermission)) {
addendum = " (" + getLocalPermissionText(defaultPermission.evaluate(sender)) + ")";
}
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessageKey.DEFAULT) + ": "
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessage.DEFAULT) + ": "
+ ChatColor.GRAY + ChatColor.ITALIC + helpMessagesService.getMessage(defaultPermission) + addendum);
// Evaluate if the sender has permission to the command
@ -187,14 +200,14 @@ public class HelpProvider implements SettingsDependent {
permissionText = getLocalPermissionText(false);
}
lines.add(String.format(ChatColor.GOLD + " %s: %s" + ChatColor.ITALIC + "%s",
helpMessagesService.getMessage(HelpMessageKey.RESULT), permissionColor, permissionText));
helpMessagesService.getMessage(HelpMessage.RESULT), permissionColor, permissionText));
}
private String getLocalPermissionText(boolean hasPermission) {
if (hasPermission) {
return helpMessagesService.getMessage(HelpMessageKey.HAS_PERMISSION);
return helpMessagesService.getMessage(HelpMessage.HAS_PERMISSION);
}
return helpMessagesService.getMessage(HelpMessageKey.NO_PERMISSION);
return helpMessagesService.getMessage(HelpMessage.NO_PERMISSION);
}
private void printChildren(CommandDescription command, List<String> parentLabels, List<String> lines) {
@ -202,7 +215,7 @@ public class HelpProvider implements SettingsDependent {
return;
}
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessageKey.COMMANDS) + ":");
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMANDS) + ":");
String parentCommandPath = String.join(" ", parentLabels);
for (CommandDescription child : command.getChildren()) {
lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0)

View File

@ -0,0 +1,38 @@
package fr.xephi.authme.command.help;
/**
* Translatable sections. Message keys are prefixed by {@code section}.
*/
public enum HelpSection {
SHORT_DESCRIPTION("description.short"),
DETAILED_DESCRIPTION("description.detailed"),
USAGE("usage"),
ARGUMENTS("arguments"),
ALTERNATIVES("alternatives"),
PERMISSIONS("permissions"),
COMMANDS("commands");
private final String key;
/**
* Constructor.
*
* @param key the message key
*/
HelpSection(String key) {
this.key = "section." + key;
}
/** @return the message key */
public String getKey() {
return key;
}
}

View File

@ -11,7 +11,7 @@ import java.io.InputStreamReader;
/**
* Handles a YAML message file with a default file fallback.
*/
public final class MessageFileHandler {
public class MessageFileHandler {
// regular file
private final String filename;
@ -78,7 +78,7 @@ public final class MessageFileHandler {
*/
private String getDefault(String key) {
if (defaultConfiguration == null) {
InputStream stream = Messages.class.getResourceAsStream(defaultFile);
InputStream stream = MessageFileHandler.class.getClassLoader().getResourceAsStream(defaultFile);
defaultConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
}
String message = defaultConfiguration.getString(key);

View File

@ -1,20 +1,21 @@
common:
optional: 'Optional'
hasPermission: 'Du hast Berechtigung'
noPermission: 'Keine Berechtigung'
default: 'Default'
result: 'Resultat'
defaultPermissions:
notAllowed: 'Kein Recht'
opOnly: 'Nur OP''s'
allowed: 'Allen erlaubt'
section:
description.short: 'Beschreibung'
description.detailed: 'Detaillierte Beschreibung'
usage: 'Gebrauch'
arguments: 'Argumente'
permissions: 'Rechte'
optional: 'Optional'
hasPermission: 'Du hast Berechtigung'
noPermission: 'Keine Berechtigung'
default: 'Default'
alternatives: 'Alternativen'
result: 'Resultat'
commands: 'Kommandos'
defaultPermissions:
notAllowed: 'Kein Recht'
opOnly: 'Nur OP''s'
allowed: 'Allen erlaubt'
commands:
authme.register:
description: 'Registriert einen Benutzer'

View File

@ -1,20 +1,21 @@
common:
description.short: 'Short description'
description.detailed: 'Detailed description'
usage: 'Usage'
arguments: 'Arguments'
permissions: 'Permissions'
optional: 'Optional'
hasPermission: 'You have permission'
noPermission: 'No permission'
default: 'Default'
alternatives: 'Alternatives'
result: 'Result'
commands: 'Commands'
defaultPermissions:
notAllowed: 'No permission'
opOnly: 'OP''s only'
allowed: 'Everyone allowed'
section:
description.short: 'Short description'
description.detailed: 'Detailed description'
arguments: 'Arguments'
permissions: 'Permissions'
alternatives: 'Alternatives'
commands: 'Commands'
usage: 'Usage'
commands:
authme.register:
description: 'Register a player'

View File

@ -64,9 +64,13 @@ public class HelpMessagesConsistencyTest {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE);
// when / then
for (HelpMessageKey key : HelpMessageKey.values()) {
assertThat("Default configuration has entry for key '" + key + "'",
configuration.contains(key.getKey()), equalTo(true));
for (HelpMessage message : HelpMessage.values()) {
assertThat("Default configuration has entry for message '" + message + "'",
configuration.contains(message.getKey()), equalTo(true));
}
for (HelpSection section : HelpSection.values()) {
assertThat("Default configuration has entry for section '" + section + "'",
configuration.contains(section.getKey()), equalTo(true));
}
}

View File

@ -32,6 +32,7 @@ import static fr.xephi.authme.command.help.HelpProvider.HIDE_COMMAND;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_ALTERNATIVES;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_ARGUMENTS;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_CHILDREN;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_LONG_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_PERMISSIONS;
import static org.hamcrest.Matchers.contains;
@ -83,7 +84,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login"));
// when
helpProvider.outputHelp(sender, result, SHOW_LONG_DESCRIPTION);
helpProvider.outputHelp(sender, result, SHOW_LONG_DESCRIPTION | SHOW_DESCRIPTION);
// then
List<String> lines = getLines(sender);
@ -368,7 +369,7 @@ public class HelpProviderTest {
private static void setDefaultHelpMessages(HelpMessagesService helpMessagesService) {
given(helpMessagesService.buildLocalizedDescription(any(CommandDescription.class)))
.willAnswer(new ReturnsArgumentAt(0));
for (HelpMessageKey key : HelpMessageKey.values()) {
for (HelpMessage key : HelpMessage.values()) {
String text = key.name().replace("_", " ").toLowerCase();
given(helpMessagesService.getMessage(key))
.willReturn(text.substring(0, 1).toUpperCase() + text.substring(1));
@ -378,6 +379,11 @@ public class HelpProviderTest {
given(helpMessagesService.getMessage(permission))
.willReturn(text.substring(0, 1).toUpperCase() + text.substring(1));
}
for (HelpSection section : HelpSection.values()) {
String text = section.name().replace("_", " ").toLowerCase();
given(helpMessagesService.getMessage(section))
.willReturn(text.substring(0, 1).toUpperCase() + text.substring(1));
}
}
}

View File

@ -34,7 +34,7 @@ import static org.mockito.Mockito.verify;
public class MessagesIntegrationTest {
private static final String YML_TEST_FILE = TestHelper.PROJECT_ROOT + "message/messages_test.yml";
private static final String YML_DEFAULT_TEST_FILE = TestHelper.PROJECT_ROOT + "message/messages_default.yml";
private static final String YML_DEFAULT_TEST_FILE = "messages/messages_en.yml";
private Messages messages;
@BeforeClass
@ -201,7 +201,8 @@ public class MessagesIntegrationTest {
String message = messages.retrieveSingle(key);
// then
assertThat(message, equalTo("Message from default file"));
assertThat(message,
equalTo("§4Only registered users can join the server! Please visit http://example.com to register yourself!"));
}
@Test
@ -217,35 +218,6 @@ public class MessagesIntegrationTest {
assertThat(message, equalTo("§cWrong password!"));
}
@Test
public void shouldReturnErrorForMissingMessage() {
// given
// Key is not present in test file or default file
MessageKey key = MessageKey.TWO_FACTOR_CREATE;
// when
String message = messages.retrieveSingle(key);
// then
assertThat(message, containsString("Error retrieving message"));
}
@Test
public void shouldAllowNullAsDefaultFile() {
// given
MessageFileHandlerProvider provider =
providerReturning(TestHelper.getJarFile(YML_TEST_FILE), YML_DEFAULT_TEST_FILE);
Messages testMessages = new Messages(provider);
// Key not present in test file
MessageKey key = MessageKey.TWO_FACTOR_CREATE;
// when
String message = testMessages.retrieveSingle(key);
// then
assertThat(message, containsString("Error retrieving message"));
}
@Test
public void shouldRetrieveMessageWithReplacements() {
// given