Update Language.java

This commit is contained in:
Secretboy 2022-05-07 12:50:10 +08:00 committed by Melledy
parent f9b5478c9d
commit bf0d01778e

View File

@ -39,18 +39,47 @@ public final class Language {
} }
/** /**
* Reads a file and creates a language instance. * creates a language instance.
* @param fileName The name of the language file. * @param fileName The name of the language file.
*/ */
private Language(String fileName) { private Language(String fileName) {
@Nullable JsonObject languageData = null; @Nullable JsonObject languageData = null;
languageData = loadLanguage(fileName);
if (languageData == null) {
Grasscutter.getLogger().info("Now switch to default language");
languageData = loadDefaultLanguage();
}
assert languageData != null : "languageData is null";
this.languageData = languageData;
}
/**
* Load default language file and creates a language instance.
* @return language data
*/
private JsonObject loadDefaultLanguage() {
var fileName = Grasscutter.getConfig().DefaultLanguage.toLanguageTag() + ".json";
return loadLanguage(fileName);
}
/**
* Reads a file and creates a language instance.
* @param fileName The name of the language file.
* @return language data
*/
private JsonObject loadLanguage(String fileName) {
@Nullable JsonObject languageData = null;
try { try {
InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName); InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName);
languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(file), JsonObject.class); languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(file), JsonObject.class);
} catch (Exception exception) { } catch (Exception exception) {
Grasscutter.getLogger().error("Failed to load language file: " + fileName, exception); Grasscutter.getLogger().warn("Failed to load language file: " + fileName);
} this.languageData = languageData; }
return languageData;
} }
/** /**