Search in sources :

Example 1 with Language

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());
        }
    }
}
Also used : Dynamic(alexh.weak.Dynamic) Arrays(java.util.Arrays) List(java.util.List) Language(github.scarsz.configuralize.Language) Map(java.util.Map) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) Dynamic(alexh.weak.Dynamic) Language(github.scarsz.configuralize.Language) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map)

Example 2 with Language

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 + ".");
    }
}
Also used : TextComponent(net.kyori.adventure.text.TextComponent) CommandSender(org.bukkit.command.CommandSender) Arrays(java.util.Arrays) Provider(github.scarsz.configuralize.Provider) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) ClickEvent(net.kyori.adventure.text.event.ClickEvent) Player(org.bukkit.entity.Player) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) File(java.io.File) MessageUtil(github.scarsz.discordsrv.util.MessageUtil) Language(github.scarsz.configuralize.Language) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) Source(github.scarsz.configuralize.Source) Component(net.kyori.adventure.text.Component) Map(java.util.Map) ChatColor(org.bukkit.ChatColor) HoverEvent(net.kyori.adventure.text.event.HoverEvent) TextComponent(net.kyori.adventure.text.TextComponent) Player(org.bukkit.entity.Player) Language(github.scarsz.configuralize.Language) Map(java.util.Map) File(java.io.File) Source(github.scarsz.configuralize.Source) Provider(github.scarsz.configuralize.Provider)

Aggregations

Language (github.scarsz.configuralize.Language)2 File (java.io.File)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2 FileUtils (org.apache.commons.io.FileUtils)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Dynamic (alexh.weak.Dynamic)1 Provider (github.scarsz.configuralize.Provider)1 Source (github.scarsz.configuralize.Source)1 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)1 MessageUtil (github.scarsz.discordsrv.util.MessageUtil)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Component (net.kyori.adventure.text.Component)1 TextComponent (net.kyori.adventure.text.TextComponent)1 ClickEvent (net.kyori.adventure.text.event.ClickEvent)1 HoverEvent (net.kyori.adventure.text.event.HoverEvent)1 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)1 ChatColor (org.bukkit.ChatColor)1