locale files, maybe

This commit is contained in:
2024-12-05 19:00:18 +01:00
parent 56e212b8e7
commit f5ed0b1c2c
3 changed files with 86 additions and 0 deletions
@@ -0,0 +1,33 @@
package me.youhavetrouble.blockedit;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class BELocale {
private static final Map<Locale, BELocale> locales = new HashMap<>();
private static final Locale defaultLocale = Locale.ENGLISH;
public final String COULD_NOT_FIND_WAND_BY_ID;
protected BELocale(JsonObject json) {
COULD_NOT_FIND_WAND_BY_ID = json.get("could_not_find_wand_by_id").getAsString();
}
protected static void registerLocale(Locale locale, BELocale blockEditLocale) {
locales.put(locale, blockEditLocale);
System.out.println("Registered locale " + locale.getISO3Country() + " " + locale.getISO3Language());
}
public static BELocale getLocale(@NotNull Locale locale) {
BELocale beLocale = locales.get(locale);
if (beLocale == null) beLocale = locales.get(defaultLocale);
return beLocale;
}
}
@@ -1,8 +1,15 @@
package me.youhavetrouble.blockedit;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import me.youhavetrouble.blockedit.wands.SelectionWand;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
import java.util.List;
import java.util.Locale;
public final class BlockEdit extends JavaPlugin {
private static BlockEdit plugin;
@@ -13,6 +20,12 @@ public final class BlockEdit extends JavaPlugin {
public void onEnable() {
plugin = this;
if (!initLocales()) {
plugin.getSLF4JLogger().error("Could not load locale files");
plugin.getServer().getPluginManager().disablePlugin(plugin);
return;
}
getServer().getPluginManager().registerEvents(new JoinLeaveListener(), this);
schematicHandler = new SchematicHandler(this);
@@ -37,4 +50,41 @@ public final class BlockEdit extends JavaPlugin {
return wandsHandler;
}
private boolean initLocales() {
List<String> localeFiles;
try (InputStream in = BlockEdit.class.getClassLoader().getResourceAsStream("locale");
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
localeFiles = br.lines().toList();
} catch (IOException e) {
plugin.getSLF4JLogger().error("Error loading locale files", e);
return false;
}
Gson gson = new Gson();
for (String fileName : localeFiles) {
Locale locale;
try {
String localeString = fileName.replace(".json", "");
String[] split = localeString.split("_");
if (split.length == 1) {
locale = Locale.of(split[0]);
} else {
locale = Locale.of(split[0], split[1]);
}
} catch (IllegalArgumentException e) {
plugin.getSLF4JLogger().error("Invalid locale file name: {}", fileName);
continue;
}
try (InputStream fileStream = BlockEdit.class.getClassLoader().getResourceAsStream("locale/" + fileName);
JsonReader reader = new JsonReader(new InputStreamReader(fileStream))) {
JsonObject json = gson.fromJson(reader, JsonObject.class);
BELocale beLocale = new BELocale(json);
BELocale.registerLocale(locale, beLocale);
} catch (IOException e) {
plugin.getSLF4JLogger().error("Error reading locale file: {}", fileName, e);
}
}
return true;
}
}
+3
View File
@@ -0,0 +1,3 @@
{
"could_not_find_wand_by_id": "Could not find wand with id %s"
}