Commons/src/main/java/eu/univento/commons/player/language/Language.java

61 lines
1.6 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.web.client.WebClient;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Locale;
/**
* @author joethei
* @version 1.5
*/
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 JsonObject json;
private Language(Locale locale) {
this.locale = locale;
WebClient webClient = WebClient.create(Commons.getCommons().getVertx());
webClient.get("download.unit.univento.eu", "/config/languages/" + locale.getLanguage() + ".json").send(ar -> {
if(ar.succeeded()) {
this.json = ar.result().bodyAsJsonObject();
}else Commons.getCommons().getLogger().severe(ar.cause().getMessage());
});
}
public static Language getLanguage(String locale) {
for(Language language : languages) {
if(language.getLocale().getLanguage().equals(locale)) return language;
}
return null;
}
public String getMessage(MessageConstant constant) {
return getWord(constant.getName());
}
public String getWord(String key) {
return json.getString(key);
}
}