Fix #1493 Extract handling of message file paths to a separate class with constants
This commit is contained in:
@@ -14,13 +14,13 @@ import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
|
||||
|
||||
/**
|
||||
* Handles a YAML message file with a default file fallback.
|
||||
*/
|
||||
public abstract class AbstractMessageFileHandler implements Reloadable {
|
||||
|
||||
protected static final String DEFAULT_LANGUAGE = "en";
|
||||
|
||||
@DataFolder
|
||||
@Inject
|
||||
private File dataFolder;
|
||||
|
||||
@@ -9,6 +9,8 @@ import javax.inject.Inject;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
|
||||
|
||||
/**
|
||||
* File handler for the help_xx.yml resource.
|
||||
*/
|
||||
@@ -57,6 +59,6 @@ public class HelpMessagesFileHandler extends AbstractMessageFileHandler {
|
||||
|
||||
@Override
|
||||
protected String createFilePath(String language) {
|
||||
return "messages/help_" + language + ".yml";
|
||||
return MessagePathHelper.createHelpMessageFilePath(language);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Helper for creating and processing paths to message files.
|
||||
*/
|
||||
public final class MessagePathHelper {
|
||||
|
||||
/** The default language (used as fallback, assumed to be complete, etc.). */
|
||||
public static final String DEFAULT_LANGUAGE = "en";
|
||||
/** Local path to the folder containing the message files. */
|
||||
public static final String MESSAGES_FOLDER = "messages/";
|
||||
/** Local path to the default messages file (messages/messages_en.yml). */
|
||||
public static final String DEFAULT_MESSAGES_FILE = createMessageFilePath(DEFAULT_LANGUAGE);
|
||||
|
||||
private static final Pattern MESSAGE_FILE_PATTERN = Pattern.compile("messages_([a-z]+)\\.yml");
|
||||
private static final Pattern HELP_MESSAGES_FILE = Pattern.compile("help_[a-z]+\\.yml");
|
||||
|
||||
private MessagePathHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the local path to the messages file for the provided language code.
|
||||
*
|
||||
* @param languageCode the language code
|
||||
* @return local path to the messages file of the given language
|
||||
*/
|
||||
public static String createMessageFilePath(String languageCode) {
|
||||
return "messages/messages_" + languageCode + ".yml";
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the local path to the help messages file for the provided language code.
|
||||
*
|
||||
* @param languageCode the language code
|
||||
* @return local path to the help messages file of the given language
|
||||
*/
|
||||
public static String createHelpMessageFilePath(String languageCode) {
|
||||
return "messages/help_" + languageCode + ".yml";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given file name is a messages file.
|
||||
*
|
||||
* @param filename the file name to test
|
||||
* @return true if it is a messages file, false otherwise
|
||||
*/
|
||||
public static boolean isMessagesFile(String filename) {
|
||||
return MESSAGE_FILE_PATTERN.matcher(filename).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language code the given file name is for if it is a messages file, otherwise null is returned.
|
||||
*
|
||||
* @param filename the file name to process
|
||||
* @return the language code the file name is a messages file for, or null if not applicable
|
||||
*/
|
||||
public static String getLanguageIfIsMessagesFile(String filename) {
|
||||
Matcher matcher = MESSAGE_FILE_PATTERN.matcher(filename);
|
||||
if (matcher.matches()) {
|
||||
return matcher.group(1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given file name is a help messages file.
|
||||
*
|
||||
* @param filename the file name to test
|
||||
* @return true if it is a help messages file, false otherwise
|
||||
*/
|
||||
public static boolean isHelpFile(String filename) {
|
||||
return HELP_MESSAGES_FILE.matcher(filename).matches();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import fr.xephi.authme.message.updater.MessageUpdater;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
|
||||
|
||||
/**
|
||||
* File handler for the messages_xx.yml resource.
|
||||
*/
|
||||
@@ -38,6 +40,6 @@ public class MessagesFileHandler extends AbstractMessageFileHandler {
|
||||
|
||||
@Override
|
||||
protected String createFilePath(String language) {
|
||||
return "messages/messages_" + language + ".yml";
|
||||
return MessagePathHelper.createMessageFilePath(language);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import fr.xephi.authme.command.help.HelpMessage;
|
||||
import fr.xephi.authme.command.help.HelpMessagesService;
|
||||
import fr.xephi.authme.command.help.HelpSection;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.message.MessagePathHelper;
|
||||
import fr.xephi.authme.permission.DefaultPermission;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
@@ -49,7 +50,7 @@ public class HelpTranslationGenerator {
|
||||
*/
|
||||
public File updateHelpFile() throws IOException {
|
||||
String languageCode = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
||||
File helpFile = new File(dataFolder, "messages/help_" + languageCode + ".yml");
|
||||
File helpFile = new File(dataFolder, MessagePathHelper.createHelpMessageFilePath(languageCode));
|
||||
Map<String, Object> helpEntries = generateHelpMessageEntries();
|
||||
|
||||
String helpEntriesYaml = exportToYaml(helpEntries);
|
||||
|
||||
Reference in New Issue
Block a user