diff --git a/src/main/java/eu/univento/commons/player/DatabasePlayer.java b/src/main/java/eu/univento/commons/player/DatabasePlayer.java index 2c39736..f1a176a 100644 --- a/src/main/java/eu/univento/commons/player/DatabasePlayer.java +++ b/src/main/java/eu/univento/commons/player/DatabasePlayer.java @@ -6,6 +6,7 @@ import eu.univento.commons.player.ban.BanReason; import eu.univento.commons.player.friend.FriendData; import eu.univento.commons.player.kick.KickData; import eu.univento.commons.player.kick.KickReason; +import eu.univento.commons.player.language.Language; import eu.univento.commons.player.mute.MuteData; import eu.univento.commons.player.mute.MuteReason; import eu.univento.commons.player.rank.Group; @@ -47,6 +48,10 @@ public class DatabasePlayer { return settings; } + public Language getLanguage() { + return Language.getLanguage(Locale.forLanguageTag(getSettings().getLanguage())); + } + public CompletableFuture getSettingsFromDatabase() { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase("Settings").whenComplete((entries, throwable) -> { diff --git a/src/main/java/eu/univento/commons/player/language/Language.java b/src/main/java/eu/univento/commons/player/language/Language.java new file mode 100644 index 0000000..4280c22 --- /dev/null +++ b/src/main/java/eu/univento/commons/player/language/Language.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2017 univento.eu - All rights reserved + * You are not allowed to use, distribute or modify this code + */ + +package eu.univento.commons.player.language; + +import eu.univento.commons.Commons; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.sql.ResultSet; +import io.vertx.ext.sql.SQLConnection; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.Locale; + +/** + * @author joethei + * @version 0.1 + */ +public class Language { + + private static ArrayList languages = new ArrayList<>(); + + static { + languages.add(new Language(Locale.GERMAN)); + languages.add(new Language(Locale.ENGLISH)); + languages.add(new Language(Locale.ITALIAN)); + } + + @Getter private Locale locale; + private LinkedHashMap translations = new LinkedHashMap<>(); + + private Language(Locale locale) { + this.locale = locale; + Commons.getCommons().getDatabaseManager().getMySQL().getClient().getConnection(res -> { + if(res.succeeded()) { + SQLConnection connection = res.result(); + connection.query("SELECT identifier, " + locale.getISO3Language() + " FROM translations;", result -> { + if(result.succeeded()) { + ResultSet rs = result.result(); + for(JsonObject json : rs.getRows()) { + translations.put(json.getString("translations"), json.getString(locale.getISO3Language())); + } + + }else result.cause().printStackTrace(); + }); + }else res.cause().printStackTrace(); + }); + } + + public static Language getLanguage(Locale locale) { + for(Language language : languages) { + if(language.getLocale().equals(locale)) return language; + } + return null; + } + + public String getMessage(MessageConstant constant) { + return getWord(constant.getName()); + } + + public String getWord(String constant) { + return translations.get(constant); + } +} \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/utils/Strings.java b/src/main/java/eu/univento/commons/utils/Strings.java index 19ddd48..e3ea2f6 100644 --- a/src/main/java/eu/univento/commons/utils/Strings.java +++ b/src/main/java/eu/univento/commons/utils/Strings.java @@ -1,5 +1,7 @@ package eu.univento.commons.utils; +import java.lang.reflect.Array; + /** * @author joethei, Alex Gitelman * @version 1.0 @@ -37,4 +39,25 @@ public class Strings { // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4" return Integer.signum(vals1.length - vals2.length); } + + public static String[] remove(String[] array, String element) { + if (array.length > 0) { + int index = -1; + for (int i = 0; i < array.length; i++) { + if (array[i].equals(element)) { + index = i; + break; + } + } + if (index >= 0) { + String[] copy = (String[]) Array.newInstance(array.getClass().getComponentType(), array.length - 1); + if (copy.length > 0) { + System.arraycopy(array, 0, copy, 0, index); + System.arraycopy(array, index + 1, copy, index, copy.length - index); + } + return copy; + } + } + return array; + } } \ No newline at end of file