mirror of
https://github.com/YouHaveTrouble/BlockEdit.git
synced 2026-06-29 13:36:19 +00:00
locale files, maybe
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"could_not_find_wand_by_id": "Could not find wand with id %s"
|
||||
}
|
||||
Reference in New Issue
Block a user