67 lines
2.1 KiB
Java
67 lines
2.1 KiB
Java
/*
|
|
* 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<Language> 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<String, String> 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);
|
|
}
|
|
} |