Commit 5a37acde authored by Secretboy-SMR's avatar Secretboy-SMR Committed by Melledy
Browse files

Fix the following issues:

1. HashMap non-thread-safe issus
2. Fix the same problem in pr621, but use a better implementation

Add the following functions:
1. There is now a language cache inside getLanguage to prepare for different languages corresponding to different time zones where the accounts in the server are located
parent d179b5c7
...@@ -22,7 +22,7 @@ public final class Config { ...@@ -22,7 +22,7 @@ public final class Config {
public GameServerOptions GameServer = new GameServerOptions(); public GameServerOptions GameServer = new GameServerOptions();
public DispatchServerOptions DispatchServer = new DispatchServerOptions(); public DispatchServerOptions DispatchServer = new DispatchServerOptions();
public Locale LocaleLanguage = Locale.getDefault(); public Locale LocaleLanguage = Locale.getDefault();
public Locale DefaultLanguage = Locale.ENGLISH; public Locale DefaultLanguage = Locale.US;
public Boolean OpenStamina = true; public Boolean OpenStamina = true;
public GameServerOptions getGameServerOptions() { public GameServerOptions getGameServerOptions() {
......
...@@ -149,14 +149,7 @@ public final class Grasscutter { ...@@ -149,14 +149,7 @@ public final class Grasscutter {
public static void loadLanguage() { public static void loadLanguage() {
var locale = config.LocaleLanguage; var locale = config.LocaleLanguage;
var languageTag = locale.toLanguageTag(); language = Language.getLanguage(Utils.getLanguageCode(locale));
if (languageTag.equals("und")) {
Grasscutter.getLogger().error("Illegal locale language, using 'en-US' instead.");
language = Language.getLanguage("en-US");
} else {
language = Language.getLanguage(languageTag);
}
} }
public static void saveConfig() { public static void saveConfig() {
......
...@@ -6,12 +6,13 @@ import emu.grasscutter.Grasscutter; ...@@ -6,12 +6,13 @@ import emu.grasscutter.Grasscutter;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.Map; import java.util.Map;
public final class Language { public final class Language {
private final JsonObject languageData; private final JsonObject languageData;
private final Map<String, String> cachedTranslations = new HashMap<>(); 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.
...@@ -19,7 +20,13 @@ public final class Language { ...@@ -19,7 +20,13 @@ public final class Language {
* @return A language instance. * @return A language instance.
*/ */
public static Language getLanguage(String langCode) { public static Language getLanguage(String langCode) {
return new Language(langCode + ".json", Grasscutter.getConfig().DefaultLanguage.toLanguageTag() + ".json"); if (cachedLanguages.containsKey(langCode)) {
return cachedLanguages.get(langCode);
}
var languageInst = new Language(langCode + ".json", Utils.getLanguageCode(Grasscutter.getConfig().DefaultLanguage) + ".json");
cachedLanguages.put(langCode, languageInst);
return languageInst;
} }
/** /**
...@@ -42,6 +49,7 @@ public final class Language { ...@@ -42,6 +49,7 @@ public final class Language {
/** /**
* Reads a file and creates a language instance. * Reads a file and creates a language instance.
* @param fileName The name of the language file. * @param fileName The name of the language file.
* @param fallback The name of the fallback language file.
*/ */
private Language(String fileName, String fallback) { private Language(String fileName, String fallback) {
@Nullable JsonObject languageData = null; @Nullable JsonObject languageData = null;
......
...@@ -9,6 +9,7 @@ import java.time.temporal.TemporalAdjusters; ...@@ -9,6 +9,7 @@ import java.time.temporal.TemporalAdjusters;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Locale;
import emu.grasscutter.Config; import emu.grasscutter.Config;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
...@@ -306,4 +307,12 @@ public final class Utils { ...@@ -306,4 +307,12 @@ public final class Utils {
return map; return map;
} }
/**
* get language code from Locale
*/
public static String getLanguageCode(Locale locale) {
return String.format("%s-%s", locale.getLanguage(), locale.getCountry());
}
} }
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