mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-09-06 20:28:06 +02:00
Create export/import messages task
- Allows communication with a web interface (PHP) to translate messages - Saving via import still needs to be improved
This commit is contained in:
parent
4911499333
commit
f4bc4322f0
60
src/tools/messages/translation/ExportMessagesTask.java
Normal file
60
src/tools/messages/translation/ExportMessagesTask.java
Normal file
@ -0,0 +1,60 @@
|
||||
package messages.translation;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import utils.FileUtils;
|
||||
import utils.ToolTask;
|
||||
import utils.ToolsConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Task to export all messages for translation purposes.
|
||||
*/
|
||||
public class ExportMessagesTask implements ToolTask {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
return "exportMessages";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Scanner scanner) {
|
||||
FileConfiguration defaultMessages = YamlConfiguration.loadConfiguration(
|
||||
new File(ToolsConstants.MAIN_RESOURCES_ROOT + "messages/messages_en.yml"));
|
||||
|
||||
File[] messageFiles = new File(ToolsConstants.MAIN_RESOURCES_ROOT + "messages").listFiles();
|
||||
if (messageFiles == null || messageFiles.length == 0) {
|
||||
throw new IllegalStateException("Could not read messages folder");
|
||||
}
|
||||
|
||||
for (File file : messageFiles) {
|
||||
String code = file.getName().substring("messages_".length(), file.getName().length() - ".yml".length());
|
||||
exportLanguage(code, defaultMessages, YamlConfiguration.loadConfiguration(file));
|
||||
}
|
||||
}
|
||||
|
||||
private void exportLanguage(String code, FileConfiguration defaultMessages, FileConfiguration messageFile) {
|
||||
List<MessageExport> list = new ArrayList<>();
|
||||
for (MessageKey key : MessageKey.values()) {
|
||||
list.add(new MessageExport(key.getKey(), key.getTags(), getString(key, defaultMessages),
|
||||
getString(key, messageFile)));
|
||||
}
|
||||
|
||||
FileUtils.writeToFile(
|
||||
ToolsConstants.TOOLS_SOURCE_ROOT + "messages/translation/export/messages_" + code + ".json",
|
||||
gson.toJson(new LanguageExport(code, list)));
|
||||
}
|
||||
|
||||
private static String getString(MessageKey key, FileConfiguration configuration) {
|
||||
return configuration.getString(key.getKey(), "");
|
||||
}
|
||||
|
||||
}
|
75
src/tools/messages/translation/ImportMessagesTask.java
Normal file
75
src/tools/messages/translation/ImportMessagesTask.java
Normal file
@ -0,0 +1,75 @@
|
||||
package messages.translation;
|
||||
|
||||
import com.google.common.io.Resources;
|
||||
import com.google.gson.Gson;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import utils.ToolTask;
|
||||
import utils.ToolsConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Imports a message file from a JSON export.
|
||||
*/
|
||||
public class ImportMessagesTask implements ToolTask {
|
||||
|
||||
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||
private Gson gson = new Gson();
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
return "importMessages";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Scanner scanner) {
|
||||
System.out.println("Enter URL to export from");
|
||||
String url = scanner.nextLine();
|
||||
LanguageExport languageExport = getLanguageExportFromUrl(url);
|
||||
if (languageExport == null) {
|
||||
throw new IllegalStateException("An error occurred: constructed language export is null");
|
||||
}
|
||||
|
||||
mergeExportIntoFile(languageExport);
|
||||
System.out.println("Saved to messages file for code '" + languageExport.code + "'");
|
||||
}
|
||||
|
||||
private LanguageExport getLanguageExportFromUrl(String url) {
|
||||
try {
|
||||
URL uri = new URL(url);
|
||||
String json = Resources.toString(uri, Charset.forName("UTF-8"));
|
||||
return gson.fromJson(json, LanguageExport.class);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void mergeExportIntoFile(LanguageExport export) {
|
||||
String languageCode = export.code;
|
||||
File file = new File(MESSAGES_FOLDER + "messages_" + languageCode + ".yml");
|
||||
if (!file.exists()) {
|
||||
throw new IllegalStateException("Messages file for language code " + languageCode + " does not exist");
|
||||
}
|
||||
FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
for (MessageExport messageExport : export.messages) {
|
||||
fileConfiguration.set(messageExport.key, messageExport.translatedMessage);
|
||||
}
|
||||
try {
|
||||
Field dumperOptionsField = YamlConfiguration.class.getDeclaredField("yamlOptions");
|
||||
dumperOptionsField.setAccessible(true);
|
||||
DumperOptions options = (DumperOptions) dumperOptionsField.get(fileConfiguration);
|
||||
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
|
||||
fileConfiguration.save(file);
|
||||
} catch (IOException | NoSuchFieldException | IllegalAccessException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
19
src/tools/messages/translation/LanguageExport.java
Normal file
19
src/tools/messages/translation/LanguageExport.java
Normal file
@ -0,0 +1,19 @@
|
||||
package messages.translation;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Export of a language's messages.
|
||||
*/
|
||||
public class LanguageExport {
|
||||
|
||||
public final String code;
|
||||
public final List<MessageExport> messages;
|
||||
|
||||
public LanguageExport(String code, List<MessageExport> messages) {
|
||||
this.code = code;
|
||||
this.messages = Collections.unmodifiableList(messages);
|
||||
}
|
||||
|
||||
}
|
22
src/tools/messages/translation/MessageExport.java
Normal file
22
src/tools/messages/translation/MessageExport.java
Normal file
@ -0,0 +1,22 @@
|
||||
package messages.translation;
|
||||
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Container class for one translatable message.
|
||||
*/
|
||||
public class MessageExport {
|
||||
|
||||
public final String key;
|
||||
public final String tags;
|
||||
public final String defaultMessage;
|
||||
public final String translatedMessage;
|
||||
|
||||
public MessageExport(String key, String[] tags, String defaultMessage, String translatedMessage) {
|
||||
this.key = key;
|
||||
this.tags = StringUtils.join(",", tags);
|
||||
this.defaultMessage = defaultMessage;
|
||||
this.translatedMessage = translatedMessage;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user