Skip to content

Commit

Permalink
Simplify the language fallback system
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed May 7, 2022
1 parent d70df77 commit 330427f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/main/java/emu/grasscutter/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/emu/grasscutter/Grasscutter.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ public static void loadConfig() {

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);
Expand Down
44 changes: 11 additions & 33 deletions src/main/java/emu/grasscutter/utils/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -30,6 +30,7 @@ public static Language getLanguage(String langCode) {
*/
public static String translate(String key, Object... args) {
String translated = Grasscutter.getLanguage().get(key);

try {
return translated.formatted(args);
} catch (Exception exception) {
Expand All @@ -38,48 +39,25 @@ public static String translate(String key, Object... args) {
}
}

/**
* 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;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/emu/grasscutter/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -254,7 +255,7 @@ public static int getNextTimestampOfThisHourInNextMonth(int hour, String timeZon
*/
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();
Expand Down

0 comments on commit 330427f

Please sign in to comment.