Initial commit

This commit is contained in:
2022-09-17 10:49:36 +02:00
parent 1e8420a83e
commit 59a6e1c423
368 changed files with 26176 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package eu.mhsl.minenet.minigames.lang;
public class DummyLang extends Lang {
public DummyLang() {
super("dummy");
}
@Override
public String getEntry(String key) {
return "translation:" + key;
}
}

View File

@@ -0,0 +1,30 @@
package eu.mhsl.minenet.minigames.lang;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
public class Lang {
private String langId;
private Map<String, String> entries = new HashMap<>();
public Lang(String langId) {
this.langId = langId;
}
public void addEntry(String key, String value) {
entries.put(key, value);
}
public String getEntry(String key) {
return entries.computeIfAbsent(key, s -> {
Logger.getLogger("localisation").warning(s + " is not known by translation files!");
return new DummyLang().getEntry(s);
});
}
public String getLangId() {
return langId;
}
}

View File

@@ -0,0 +1,105 @@
package eu.mhsl.minenet.minigames.lang;
import eu.mhsl.minenet.minigames.Resource;
import net.minestom.server.entity.Player;
import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class Languages {
private static Languages instance;
private boolean blockAccess = false;
private Map<String, Lang> languages = new HashMap<>();
public static Languages getInstance() {
if(instance == null) instance = new Languages();
return instance;
}
private Languages() {
readAll();
}
public Lang getLanguage(Player p) {
return getLanguage(p.getSettings().getLocale());
}
public Lang getLanguage(String mapId) {
return languages.computeIfAbsent(mapId, unused -> languages.computeIfAbsent("en_us", (key) -> new DummyLang()));
}
private void readAll() {
File locales = new File(Resource.LOCALES.getPath().toString());
File[] files = locales.listFiles(File::canRead);
if(files.length == 0) {
System.err.println("Failed to find any Language-files!");
return;
}
for(File locale : files) {
try {
System.out.print("reading translation " + locale.getName() + " ... ");
Map<Integer, Lang> langColumn = new HashMap<>();
String namespace = "";
boolean computedFileHeader = false;
for(String line : Files.readAllLines(locale.toPath())) {
line = line.replaceAll("[^\\p{L}\\s,#_+.:;]+", "");
String[] columns = line.split(";");
if(columns.length < 1) continue;
if(columns[0].equalsIgnoreCase("map")) {
// file header
computedFileHeader = true;
int index = -1;
for(String langId : columns) {
index++;
if(langId.endsWith("map")) continue;
languages.computeIfAbsent(langId, Lang::new);
Lang lang = languages.get(langId);
langColumn.put(index, lang);
languages.put(langId, lang);
}
} else if(columns[0].startsWith("ns:")) {
if(!computedFileHeader) throw new IllegalStateException("Cannot compute namespace data before file-header was red!");
namespace = columns[0].split(":")[1];
} else {
// file contents
if(!computedFileHeader) throw new IllegalStateException("Cannot compute translation data before file-header was red!");
int index = 0;
String mapId = "";
for(String translation : columns) {
if(index == 0) {
// store map name
mapId = translation;
} else {
// add map name and value
langColumn.get(index).addEntry(namespace + mapId, translation);
}
index++;
}
}
}
System.out.println("ok");
} catch (Exception e) {
System.out.println("fail: " + e.getMessage());
e.printStackTrace();
}
}
}
}