Grasscutter/src/main/java/emu/grasscutter/utils/Language.java

111 lines
4.1 KiB
Java
Raw Normal View History

package emu.grasscutter.utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
import javax.annotation.Nullable;
import java.io.InputStream;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
public final class Language {
private final JsonObject languageData;
private final Map<String, String> cachedTranslations = new ConcurrentHashMap<>();
private static final Map<String, Language> cachedLanguages = new ConcurrentHashMap<>();
/**
* Creates a language instance from a code.
* @param langCode The language code.
* @return A language instance.
*/
public static Language getLanguage(String langCode) {
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;
}
/**
* Returns the translated value from the key while substituting arguments.
* @param key The key of the translated value to return.
* @param args The arguments to substitute.
* @return A translated value with arguments substituted.
*/
public static String translate(String key, Object... args) {
2022-05-07 09:52:10 +08:00
String translated = Grasscutter.getLanguage().get(key);
2022-05-08 06:12:53 +08:00
2022-05-07 09:52:10 +08:00
try {
return translated.formatted(args);
} catch (Exception exception) {
Grasscutter.getLogger().error("Failed to format string: " + key, exception);
return translated;
}
}
2022-05-07 12:50:10 +08:00
/**
* Reads a file and creates a language instance.
* @param fileName The name of the language file.
* @param fallback The name of the fallback language file.
2022-05-07 12:50:10 +08:00
*/
2022-05-08 06:12:53 +08:00
private Language(String fileName, String fallback) {
2022-05-07 12:50:10 +08:00
@Nullable JsonObject languageData = null;
2022-05-08 06:12:53 +08:00
2022-05-08 23:53:37 +08:00
InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName);
2022-05-08 23:58:32 +08:00
if (file == null) { // Provided fallback language.
2022-05-08 23:53:37 +08:00
file = Grasscutter.class.getResourceAsStream("/languages/" + fallback);
2022-05-08 23:58:32 +08:00
Grasscutter.getLogger().warn("Failed to load language file: " + fileName + ", falling back to: " + fallback);
}
if(file == null) { // Fallback the fallback language.
2022-05-08 23:53:37 +08:00
file = Grasscutter.class.getResourceAsStream("/languages/en-US.json");
2022-05-08 23:58:32 +08:00
Grasscutter.getLogger().warn("Failed to load language file: " + fallback + ", falling back to: en-US.json");
}
2022-05-08 23:53:37 +08:00
if(file == null)
throw new RuntimeException("Unable to load the primary, fallback, and 'en-US' language files.");
try {
languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(file), JsonObject.class);
} catch (Exception exception) {
2022-05-08 06:12:53 +08:00
Grasscutter.getLogger().warn("Failed to load language file: " + fileName, exception);
2022-05-07 12:50:10 +08:00
}
2022-05-08 06:12:53 +08:00
this.languageData = languageData;
}
/**
* Returns the value (as a string) from a nested key.
* @param key The key to look for.
* @return The value (as a string) from a nested key.
*/
public String get(String key) {
if(this.cachedTranslations.containsKey(key)) {
return this.cachedTranslations.get(key);
}
String[] keys = key.split("\\.");
JsonObject object = this.languageData;
int index = 0;
String result = "This value does not exist. Please report this to the Discord: " + key;
while (true) {
if(index == keys.length) break;
String currentKey = keys[index++];
if(object.has(currentKey)) {
JsonElement element = object.get(currentKey);
if(element.isJsonObject())
object = element.getAsJsonObject();
else {
result = element.getAsString(); break;
}
} else break;
}
this.cachedTranslations.put(key, result); return result;
}
}