-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1666c02
commit 2e958c5
Showing
2 changed files
with
82 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |