mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-23 22:54:29 +00:00
Simplify the language fallback system
This commit is contained in:
parent
d70df776b1
commit
330427f5a5
@ -23,7 +23,7 @@ public final class Config {
|
||||
public GameServerOptions GameServer = new GameServerOptions();
|
||||
public DispatchServerOptions DispatchServer = new DispatchServerOptions();
|
||||
public Locale LocaleLanguage = Locale.getDefault();
|
||||
public Locale DefaultLanguage = Locale.US;
|
||||
public Locale DefaultLanguage = Locale.ENGLISH;
|
||||
|
||||
public Boolean OpenStamina = true;
|
||||
public GameServerOptions getGameServerOptions() {
|
||||
|
@ -149,9 +149,10 @@ public final class Grasscutter {
|
||||
|
||||
public static void loadLanguage() {
|
||||
var locale = config.LocaleLanguage;
|
||||
String languageTag = locale.toLanguageTag();
|
||||
var languageTag = locale.toLanguageTag();
|
||||
|
||||
if (languageTag.equals("und")) {
|
||||
Grasscutter.getLogger().error("Illegal locale language, using en-US instead.");
|
||||
Grasscutter.getLogger().error("Illegal locale language, using 'en-US' instead.");
|
||||
language = Language.getLanguage("en-US");
|
||||
} else {
|
||||
language = Language.getLanguage(languageTag);
|
||||
|
@ -19,7 +19,7 @@ public final class Language {
|
||||
* @return A language instance.
|
||||
*/
|
||||
public static Language getLanguage(String langCode) {
|
||||
return new Language(langCode + ".json");
|
||||
return new Language(langCode + ".json", Grasscutter.getConfig().DefaultLanguage.toLanguageTag());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -30,6 +30,7 @@ public final class Language {
|
||||
*/
|
||||
public static String translate(String key, Object... args) {
|
||||
String translated = Grasscutter.getLanguage().get(key);
|
||||
|
||||
try {
|
||||
return translated.formatted(args);
|
||||
} catch (Exception exception) {
|
||||
@ -38,48 +39,25 @@ public final class Language {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a language instance.
|
||||
* @param fileName The name of the language file.
|
||||
*/
|
||||
private Language(String fileName) {
|
||||
@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) {
|
||||
private Language(String fileName, String fallback) {
|
||||
@Nullable JsonObject languageData = null;
|
||||
|
||||
|
||||
try {
|
||||
InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName);
|
||||
if(file == null) {
|
||||
file = Grasscutter.class.getResourceAsStream("/languages/" + fallback);
|
||||
}
|
||||
|
||||
languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(file), JsonObject.class);
|
||||
} catch (Exception exception) {
|
||||
Grasscutter.getLogger().warn("Failed to load language file: " + fileName);
|
||||
Grasscutter.getLogger().warn("Failed to load language file: " + fileName, exception);
|
||||
}
|
||||
return languageData;
|
||||
|
||||
this.languageData = languageData;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,7 @@
|
||||
package emu.grasscutter.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.time.*;
|
||||
@ -254,7 +255,7 @@ public final class Utils {
|
||||
*/
|
||||
public static String readFromInputStream(InputStream stream) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"UTF-8"))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
||||
String line; while ((line = reader.readLine()) != null) {
|
||||
stringBuilder.append(line);
|
||||
} stream.close();
|
||||
|
Loading…
Reference in New Issue
Block a user