Commit 5d49323c authored by KingRainbow44's avatar KingRainbow44
Browse files

my poor, poor, language system (Formatting refactor)

parent a26afe7d
...@@ -3,7 +3,6 @@ package emu.grasscutter.utils; ...@@ -3,7 +3,6 @@ package emu.grasscutter.utils;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import javax.annotation.Nullable; import javax.annotation.Nullable;
...@@ -12,10 +11,11 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -12,10 +11,11 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.Map; import java.util.Map;
public final class Language { public final class Language {
private static final Map<String, Language> cachedLanguages = new ConcurrentHashMap<>();
private final JsonObject languageData; private final JsonObject languageData;
private final String languageCode; private final String languageCode;
private final Map<String, String> cachedTranslations = new ConcurrentHashMap<>(); private final Map<String, String> cachedTranslations = new ConcurrentHashMap<>();
private static final Map<String, Language> cachedLanguages = new ConcurrentHashMap<>();
/** /**
* Creates a language instance from a code. * Creates a language instance from a code.
...@@ -28,16 +28,14 @@ public final class Language { ...@@ -28,16 +28,14 @@ public final class Language {
} }
var fallbackLanguageCode = Utils.getLanguageCode(Grasscutter.getConfig().DefaultLanguage); var fallbackLanguageCode = Utils.getLanguageCode(Grasscutter.getConfig().DefaultLanguage);
var descripter = getLanguageFileStreamDescripter(langCode, fallbackLanguageCode); var description = getLanguageFileStreamDescripter(langCode, fallbackLanguageCode);
var actualLanguageCode = descripter.getLanguageCode(); var actualLanguageCode = description.getLanguageCode();
Language languageInst = null; Language languageInst;
if (description.getLanguageFile() != null) {
if (descripter.getLanguageFile() != null) { languageInst = new Language(description);
languageInst = new Language(descripter);
cachedLanguages.put(actualLanguageCode, languageInst); cachedLanguages.put(actualLanguageCode, languageInst);
} } else {
else {
languageInst = cachedLanguages.get(actualLanguageCode); languageInst = cachedLanguages.get(actualLanguageCode);
cachedLanguages.put(langCode, languageInst); cachedLanguages.put(langCode, languageInst);
} }
...@@ -95,39 +93,41 @@ public final class Language { ...@@ -95,39 +93,41 @@ public final class Language {
/** /**
* Reads a file and creates a language instance. * Reads a file and creates a language instance.
*/ */
private Language(InternalLanguageFileStreamDescripter descripter) { private Language(LanguageStreamDescription description) {
@Nullable JsonObject languageData = null; @Nullable JsonObject languageData = null;
languageCode = descripter.getLanguageCode(); languageCode = description.getLanguageCode();
try { try {
languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(descripter.getLanguageFile()), JsonObject.class); languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(description.getLanguageFile()), JsonObject.class);
} catch (Exception exception) { } catch (Exception exception) {
Grasscutter.getLogger().warn("Failed to load language file: " + descripter.getLanguageCode(), exception); Grasscutter.getLogger().warn("Failed to load language file: " + description.getLanguageCode(), exception);
} }
this.languageData = languageData; this.languageData = languageData;
} }
/** /**
* create a InternalLanguageFileStreamDescripter * create a LanguageStreamDescription
* @param languageCode The name of the language code. * @param languageCode The name of the language code.
* @param fallbackLanguageCode The name of the fallback language code. * @param fallbackLanguageCode The name of the fallback language code.
*/ */
private static InternalLanguageFileStreamDescripter getLanguageFileStreamDescripter(String languageCode, String fallbackLanguageCode) { private static LanguageStreamDescription getLanguageFileStreamDescripter(String languageCode, String fallbackLanguageCode) {
var fileName = languageCode + ".json"; var fileName = languageCode + ".json";
var fallback = fallbackLanguageCode + ".json"; var fallback = fallbackLanguageCode + ".json";
String actualLanguageCode = languageCode; String actualLanguageCode = languageCode;
if (cachedLanguages.containsKey(actualLanguageCode)) { if (cachedLanguages.containsKey(actualLanguageCode)) {
return new InternalLanguageFileStreamDescripter(actualLanguageCode, null); return new LanguageStreamDescription(actualLanguageCode, null);
} }
InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName); InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName);
if (file == null) { // Provided fallback language. if (file == null) { // Provided fallback language.
actualLanguageCode = fallbackLanguageCode; actualLanguageCode = fallbackLanguageCode;
if (cachedLanguages.containsKey(actualLanguageCode)) { if (cachedLanguages.containsKey(actualLanguageCode)) {
return new InternalLanguageFileStreamDescripter(actualLanguageCode, null); return new LanguageStreamDescription(actualLanguageCode, null);
} }
file = Grasscutter.class.getResourceAsStream("/languages/" + fallback); file = Grasscutter.class.getResourceAsStream("/languages/" + fallback);
Grasscutter.getLogger().warn("Failed to load language file: " + fileName + ", falling back to: " + fallback); Grasscutter.getLogger().warn("Failed to load language file: " + fileName + ", falling back to: " + fallback);
} }
...@@ -135,8 +135,9 @@ public final class Language { ...@@ -135,8 +135,9 @@ public final class Language {
if(file == null) { // Fallback the fallback language. if(file == null) { // Fallback the fallback language.
actualLanguageCode = "en-US"; actualLanguageCode = "en-US";
if (cachedLanguages.containsKey(actualLanguageCode)) { if (cachedLanguages.containsKey(actualLanguageCode)) {
return new InternalLanguageFileStreamDescripter(actualLanguageCode, null); return new LanguageStreamDescription(actualLanguageCode, null);
} }
file = Grasscutter.class.getResourceAsStream("/languages/en-US.json"); file = Grasscutter.class.getResourceAsStream("/languages/en-US.json");
Grasscutter.getLogger().warn("Failed to load language file: " + fallback + ", falling back to: en-US.json"); Grasscutter.getLogger().warn("Failed to load language file: " + fallback + ", falling back to: en-US.json");
} }
...@@ -144,7 +145,7 @@ public final class Language { ...@@ -144,7 +145,7 @@ public final class Language {
if(file == null) if(file == null)
throw new RuntimeException("Unable to load the primary, fallback, and 'en-US' language files."); throw new RuntimeException("Unable to load the primary, fallback, and 'en-US' language files.");
return new InternalLanguageFileStreamDescripter(actualLanguageCode, file); return new LanguageStreamDescription(actualLanguageCode, file);
} }
/** /**
...@@ -180,11 +181,11 @@ public final class Language { ...@@ -180,11 +181,11 @@ public final class Language {
this.cachedTranslations.put(key, result); return result; this.cachedTranslations.put(key, result); return result;
} }
private static class InternalLanguageFileStreamDescripter { private static class LanguageStreamDescription {
private String languageCode; private final String languageCode;
private InputStream languageFile; private final InputStream languageFile;
public InternalLanguageFileStreamDescripter(String languageCode, InputStream languageFile) { public LanguageStreamDescription(String languageCode, InputStream languageFile) {
this.languageCode = languageCode; this.languageCode = languageCode;
this.languageFile = languageFile; this.languageFile = languageFile;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment