Skip to content

Commit

Permalink
Initial multi-language support #85
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueZeeKing committed Jan 2, 2023
1 parent 1666c02 commit 2e958c5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
49 changes: 37 additions & 12 deletions src/main/java/io/icker/factions/util/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,38 @@
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.ArrayList;
import java.util.UUID;

public class Message {
public static PlayerManager manager;
private MutableText text;
private final ArrayList<Message> children = new ArrayList<>();

public Message(String message) {
text = (MutableText) Text.of(message);
}

public Message(Text message) {
text = (MutableText) message;
}

public Message(String message, Object... args) {
text = (MutableText) Text.of(String.format(message, args));
}

public Message add(String message) {
text.append(message);
children.add(new Message(message));
return this;
}

public Message add(String message, Object... args) {
text.append(String.format(message, args));
children.add(new Message(String.format(message, args)));
return this;
}

public Message add(Message message) {
text.append(message.raw());
children.add(message);
return this;
}

Expand All @@ -59,7 +67,7 @@ public Message click(String message) {
}

public Message send(PlayerEntity player, boolean actionBar) {
player.sendMessage(text, actionBar);
player.sendMessage(this.build(player.getUuid()), actionBar);
return this;
}

Expand Down Expand Up @@ -87,20 +95,37 @@ public void sendToFactionChat(Faction faction) {
}

public Message prependFaction(Faction faction) {
text = new Message("")
.add(new Message(faction.getColor().toString() + Formatting.BOLD + faction.getName()).hover(faction.getDescription()))
.filler("»")
.raw()
.append(text);
return this;
Message message = new Message("").add(new Message(faction.getColor().toString() + Formatting.BOLD + faction.getName()).hover(faction.getDescription()))
.filler("»");
message.add(this);
return message;
}

public Message filler(String symbol) {
text.append(Text.of(" " + Formatting.RESET + Formatting.DARK_GRAY + symbol + Formatting.RESET + " "));
children.add(new Message(Text.of(" " + Formatting.RESET + Formatting.DARK_GRAY + symbol + Formatting.RESET + " ")));
return this;
}

public MutableText raw() {
return text;
MutableText built = text;

for (Message message : children) {
built.append(message.raw());
}

return built;
}

public MutableText build(UUID userId) {
MutableText built = text;
if (text.getString().startsWith("translate:")) {
built = Text.of(Translator.get(text.getString(), User.get(userId).language)).copy().setStyle(text.getStyle());
}

for (Message message : children) {
built.append(message.build(userId));
}

return built;
}
}
45 changes: 45 additions & 0 deletions src/main/java/io/icker/factions/util/Translator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.icker.factions.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.fabricmc.loader.api.FabricLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.file.Path;
import java.util.HashMap;

public class Translator {
private static final HashMap<String, HashMap<String, String>> CACHE = new HashMap<>();
private static final Path DIR = FabricLoader.getInstance().getGameDir().resolve("factions");

public static String get(String key, String lang) {
if (!CACHE.containsKey(lang)) {
loadFile(lang);
}

if (CACHE.get(lang).containsKey(key)) {
return CACHE.get(lang).get(key);
}

return key;
}

private static void loadFile(String lang) {
File file = DIR.resolve(lang+".json").toFile();

if (!file.exists()) {
CACHE.put(lang, new HashMap<>());
} else {
Gson gson = new GsonBuilder().create();

try {
CACHE.put(lang, gson.fromJson(new FileReader(file), new TypeToken<HashMap<String, String>>(){}.getType()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 2e958c5

Please sign in to comment.