use of github.scarsz.configuralize.Language in project DiscordSRV by Scarsz.
the class AllConfigsHaveKeysTest method test.
// @Test
public void test() {
List<File> configs = Arrays.asList(new File(resources, "config"));
for (File parent : configs) {
try {
Dynamic english = Dynamic.from(yaml.loadAs(FileUtils.readFileToString(new File(parent, "en.yml"), "UTF-8"), Map.class));
english.allChildren().filter(d -> d.getClass().getSimpleName().equals("Child")).filter(d -> d.children().count() == 0).map(d -> {
String almostClean = d.key().toString().substring(6).replace("->", ".");
do {
almostClean = almostClean.substring(0, almostClean.lastIndexOf('.'));
} while (almostClean.matches(".+\\.\\d+$"));
return almostClean;
}).distinct().filter(StringUtils::isNotBlank).forEach(System.out::println);
for (Language language : Language.values()) {
if (language == Language.EN)
continue;
File file = new File(parent, language.getCode().toLowerCase() + ".yml");
if (!file.exists())
continue;
Map<String, Object> other = yaml.loadAs(FileUtils.readFileToString(file, "UTF-8"), Map.class);
// for (String key : english.keySet()) {
// Assert.assertTrue(String.format("%s %s config (%s/%s.yml) does not contain key %s",
// language.getName(), parent.getName(), parent.getName(), language.getCode().toLowerCase(), key),
// other.containsKey(key)
// );
// }
//
// other.keySet().removeAll(english.keySet());
// for (String key : other.keySet()) {
// Assert.fail(String.format("%s %s config (%s/%s.yml) has unused key %s",
// language.getName(), parent.getName(), parent.getName(), language.getCode().toLowerCase(), key)
// );
// }
}
} catch (IOException e) {
System.out.println("Failed to read file " + parent.getPath() + ": " + e.getMessage());
}
}
}
use of github.scarsz.configuralize.Language in project DiscordSRV by Scarsz.
the class CommandLanguage method execute.
@Command(commandNames = { "language", "setlanguage", "lang", "setlang" }, helpMessage = "Changes the language of DiscordSRV to whatever is specified.", permission = "discordsrv.language", usageExample = "language japanese")
public static void execute(CommandSender sender, String[] args) throws IOException {
Language currentLanguage = DiscordSRV.config().getLanguage();
String currentLanguageName = StringUtils.capitalize(currentLanguage.getName().toLowerCase());
Language targetLanguage = null;
outer: for (String arg : args) {
for (Language language : Language.values()) {
if (language.getCode().equalsIgnoreCase(arg) || language.getName().equalsIgnoreCase(arg)) {
targetLanguage = language;
break outer;
}
}
}
if (targetLanguage == null) {
MessageUtil.sendMessage(sender, ChatColor.DARK_AQUA + "DiscordSRV is currently in " + currentLanguageName + ". " + "Change it by giving a language as an argument.");
return;
}
String targetLanguageName = StringUtils.capitalize(targetLanguage.getName().toLowerCase());
if (!DiscordSRV.config().isLanguageAvailable(targetLanguage)) {
String available = Arrays.stream(Language.values()).filter(DiscordSRV.config()::isLanguageAvailable).map(language -> StringUtils.capitalize(language.getName().toLowerCase())).collect(Collectors.joining(", "));
MessageUtil.sendMessage(sender, ChatColor.DARK_AQUA + "DiscordSRV does not have a translation for " + targetLanguageName + ". " + "Supported languages are as follows: " + available + ".");
return;
}
if (Arrays.stream(args).noneMatch(s -> s.equalsIgnoreCase("-confirm"))) {
TextComponent message = Component.text("This will reset your DiscordSRV configuration files to be in ", NamedTextColor.DARK_AQUA).append(Component.text(targetLanguageName, NamedTextColor.WHITE)).append(Component.text(". Your old config files will be renamed to have ", NamedTextColor.DARK_AQUA)).append(Component.text(currentLanguageName + ".", NamedTextColor.WHITE)).append(Component.text(" on the beginning of the file name. ")).append(Component.text("[Confirm" + (sender instanceof Player ? "?" : " by running the command again, adding \" -confirm\" to the end") + "]").color(NamedTextColor.GREEN).clickEvent(ClickEvent.runCommand("/discord language " + targetLanguage.getCode() + " -confirm")).hoverEvent(HoverEvent.showText(Component.text("Click to confirm the config change.", NamedTextColor.GREEN))));
MessageUtil.sendMessage(sender, message);
} else {
DiscordSRV.config().setLanguage(targetLanguage);
for (Map.Entry<Source, Provider> entry : DiscordSRV.config().getSources().entrySet()) {
File source = entry.getKey().getFile();
File target = new File(source.getParentFile(), currentLanguageName + "." + source.getName());
FileUtils.moveFile(source, target);
entry.getValue().saveDefaults();
// set the ForcedLanguage value to the new language so language change will be persistent
if (entry.getKey().getResourceName().equals("config")) {
String file = FileUtils.readFileToString(source, "UTF-8");
file = file.replace("\nForcedLanguage: none", "\nForcedLanguage: " + targetLanguageName);
FileUtils.writeStringToFile(source, file, "UTF-8");
}
}
MessageUtil.sendMessage(sender, ChatColor.DARK_AQUA + "DiscordSRV language successfully changed to " + targetLanguageName + ".");
}
}
Aggregations