Remove generated code / allow import of new languages

- Exporting back messages to the remote server is now handled by IP whitelisting, not with temporary codes
This commit is contained in:
ljacqu 2016-04-17 23:17:38 +02:00
parent 354581160a
commit 92c476785b
2 changed files with 27 additions and 14 deletions

View File

@ -29,8 +29,7 @@ public class ExportMessagesTask implements ToolTask {
/** The folder containing the messages files. */
protected static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
/** The remote URL to send an updated file to. */
//private static final String UPDATE_URL = "http://jalu.ch/ext/authme/update.php";
private static final String UPDATE_URL = "http://localhost/AuthMe-translate/update.php";
private static final String UPDATE_URL = "http://jalu.ch/ext/authme/update.php";
private final Gson gson = new Gson();
@Override
@ -51,9 +50,7 @@ public class ExportMessagesTask implements ToolTask {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
String json = convertToJson(languageCode, loadDefaultMessages(), configuration);
System.out.println("Enter update code (generated on remote side)");
String updateCode = scanner.nextLine().trim();
String result = sendJsonToRemote(json, updateCode, languageCode);
String result = sendJsonToRemote(languageCode, json);
System.out.println("Answer: " + result);
}
@ -75,10 +72,9 @@ public class ExportMessagesTask implements ToolTask {
return configuration.getString(key.getKey(), "");
}
private static String sendJsonToRemote(String file, String code, String language) {
private static String sendJsonToRemote(String language, String json) {
try {
String encodedData = "file=" + URLEncoder.encode(file, "UTF-8")
+ "&code=" + URLEncoder.encode(code, "UTF-8")
String encodedData = "file=" + URLEncoder.encode(json, "UTF-8")
+ "&language=" + URLEncoder.encode(language, "UTF-8");
URL url = new URL(UPDATE_URL);

View File

@ -2,6 +2,7 @@ package messages.translation;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import fr.xephi.authme.output.MessageKey;
import messages.MessageFileVerifier;
import messages.VerifyMessagesTask;
import org.bukkit.configuration.file.FileConfiguration;
@ -14,7 +15,9 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;
/**
@ -28,6 +31,7 @@ public class ImportMessagesTask implements ToolTask {
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
private Gson gson = new Gson();
private Set<String> messageCodes;
@Override
public String getTaskName() {
@ -37,7 +41,8 @@ public class ImportMessagesTask implements ToolTask {
@Override
public void execute(Scanner scanner) {
System.out.println("Enter URL to import from");
String url = scanner.nextLine();
// Dirty trick: replace https:// with http:// so we don't have to worry about installing certificates...
String url = scanner.nextLine().replace("https://", "http://");
LanguageExport languageExport = getLanguageExportFromUrl(url);
if (languageExport == null) {
@ -62,14 +67,19 @@ public class ImportMessagesTask implements ToolTask {
String languageCode = export.code;
String fileName = MESSAGES_FOLDER + "messages_" + languageCode + ".yml";
File file = new File(fileName);
if (!file.exists()) {
throw new IllegalStateException("Messages file for language code " + languageCode + " does not exist");
FileConfiguration fileConfiguration;
if (file.exists()) {
removeAllTodoComments(fileName);
fileConfiguration = AuthMeYamlConfiguration.loadConfiguration(file);
} else {
fileConfiguration = new AuthMeYamlConfiguration();
}
removeAllTodoComments(fileName);
FileConfiguration fileConfiguration = AuthMeYamlConfiguration.loadConfiguration(file);
buildMessageCodeList();
for (MessageExport messageExport : export.messages) {
if (!messageExport.translatedMessage.isEmpty()) {
if (!messageCodes.contains(messageExport.key)) {
throw new IllegalStateException("Message key '" + messageExport.key + "' does not exist");
} else if (!messageExport.translatedMessage.isEmpty()) {
fileConfiguration.set(messageExport.key, messageExport.translatedMessage);
}
}
@ -84,6 +94,13 @@ public class ImportMessagesTask implements ToolTask {
new File(MESSAGES_FOLDER + "messages_en.yml")));
}
private void buildMessageCodeList() {
messageCodes = new HashSet<>(MessageKey.values().length);
for (MessageKey messageKey : MessageKey.values()) {
messageCodes.add(messageKey.getKey());
}
}
/**
* Removes all to-do comments written by {@link VerifyMessagesTask}. This is helpful as the YamlConfiguration
* moves those comments otherwise upon saving.