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

49 lines
1.5 KiB
Java

package eu.univento.commons.player.language;
import com.zaxxer.sansorm.OrmElf;
import com.zaxxer.sansorm.SqlClosure;
import eu.univento.commons.Commons;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
/**
* @author joethei
* @version 2.0
*/
//TODO: optimize
public class Language {
private HashMap<String, String> translations;
public Language(Locale locale) {
translations = new HashMap<>();
try {
List<Translation> translationList = new SqlClosure<List<Translation>>() {
public List<Translation> execute(Connection connection) throws SQLException {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM translations;");
return OrmElf.statementToList(statement, Translation.class);
}
}.execute(Commons.getCommons().getDatabaseManager().getMySQL().getConnection());
for(Translation translation : translationList) {
translations.put(translation.getIdentifier(), translation.getTranslation(locale));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public String getWord(String keyword) {
return translations.get(keyword);
}
public String getMessage(MessageConstant constant) {
return translations.get(constant.getName());
}
}