2018-10-05 21:42:15 -04:00
|
|
|
/*
|
2022-04-14 03:42:00 +00:00
|
|
|
* Copyright (c) Forge Development LLC and contributors
|
2022-02-28 13:41:23 -08:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
2018-10-05 21:42:15 -04:00
|
|
|
*/
|
|
|
|
|
|
2021-11-26 18:57:41 +00:00
|
|
|
package net.minecraftforge.server;
|
2018-10-05 21:42:15 -04:00
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
import com.google.gson.JsonObject;
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.server.packs.resources.Resource;
|
|
|
|
|
import net.minecraft.server.packs.resources.ResourceManager;
|
2018-10-05 21:42:15 -04:00
|
|
|
import net.minecraft.server.MinecraftServer;
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.util.GsonHelper;
|
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2021-11-26 18:57:41 +00:00
|
|
|
import net.minecraftforge.common.ForgeI18n;
|
2018-10-05 21:42:15 -04:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
public class LanguageHook
|
|
|
|
|
{
|
|
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
|
private static final Gson GSON = new Gson();
|
|
|
|
|
private static final Pattern PATTERN = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]");
|
|
|
|
|
private static List<Map<String, String>> capturedTables = new ArrayList<>(2);
|
|
|
|
|
private static Map<String, String> modTable;
|
|
|
|
|
/**
|
|
|
|
|
* Loads lang files on the server
|
|
|
|
|
*/
|
|
|
|
|
public static void captureLanguageMap(Map<String, String> table)
|
|
|
|
|
{
|
|
|
|
|
capturedTables.add(table);
|
|
|
|
|
if (modTable != null) {
|
|
|
|
|
capturedTables.forEach(t->t.putAll(modTable));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The below is based on client side net.minecraft.client.resources.Locale code
|
2021-07-17 00:08:49 -05:00
|
|
|
private static void loadLocaleData(final List<Resource> allResources) {
|
|
|
|
|
allResources.stream().map(Resource::getInputStream).forEach(LanguageHook::loadLocaleData);
|
2018-10-05 21:42:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void loadLocaleData(final InputStream inputstream) {
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
JsonElement jsonelement = GSON.fromJson(new InputStreamReader(inputstream, StandardCharsets.UTF_8), JsonElement.class);
|
2021-07-17 00:08:49 -05:00
|
|
|
JsonObject jsonobject = GsonHelper.convertToJsonObject(jsonelement, "strings");
|
2018-10-05 21:42:15 -04:00
|
|
|
|
|
|
|
|
jsonobject.entrySet().forEach(entry -> {
|
2021-07-17 00:08:49 -05:00
|
|
|
String s = PATTERN.matcher(GsonHelper.convertToString(entry.getValue(), entry.getKey())).replaceAll("%$1s");
|
2018-10-05 21:42:15 -04:00
|
|
|
modTable.put(entry.getKey(), s);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
IOUtils.closeQuietly(inputstream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void loadLanguage(String langName, MinecraftServer server) {
|
2022-02-15 18:27:10 +01:00
|
|
|
String langFile = String.format(Locale.ROOT, "lang/%s.json", langName);
|
2022-03-01 14:59:27 -06:00
|
|
|
ResourceManager resourceManager = server.getServerResources().resourceManager();
|
2021-03-09 13:37:23 -08:00
|
|
|
resourceManager.getNamespaces().forEach(namespace -> {
|
2020-06-23 20:56:24 -04:00
|
|
|
try {
|
|
|
|
|
ResourceLocation langResource = new ResourceLocation(namespace, langFile);
|
2021-03-09 13:37:23 -08:00
|
|
|
loadLocaleData(resourceManager.getResources(langResource));
|
2020-06-23 20:56:24 -04:00
|
|
|
} catch (FileNotFoundException fnfe) {
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
LOGGER.warn("Skipped language file: {}:{}", namespace, langFile, exception);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-10-05 21:42:15 -04:00
|
|
|
|
|
|
|
|
}
|
2019-02-15 20:00:41 -05:00
|
|
|
|
2019-08-04 20:16:01 -04:00
|
|
|
public static void loadForgeAndMCLangs() {
|
2019-02-15 20:00:41 -05:00
|
|
|
modTable = new HashMap<>(5000);
|
|
|
|
|
final InputStream mc = Thread.currentThread().getContextClassLoader().getResourceAsStream("assets/minecraft/lang/en_us.json");
|
|
|
|
|
final InputStream forge = Thread.currentThread().getContextClassLoader().getResourceAsStream("assets/forge/lang/en_us.json");
|
|
|
|
|
loadLocaleData(mc);
|
|
|
|
|
loadLocaleData(forge);
|
|
|
|
|
capturedTables.forEach(t->t.putAll(modTable));
|
|
|
|
|
ForgeI18n.loadLanguageData(modTable);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 21:42:15 -04:00
|
|
|
static void loadLanguagesOnServer(MinecraftServer server) {
|
|
|
|
|
modTable = new HashMap<>(5000);
|
|
|
|
|
// Possible multi-language server support?
|
|
|
|
|
for (String lang : Arrays.asList("en_us")) {
|
|
|
|
|
loadLanguage(lang, server);
|
|
|
|
|
}
|
|
|
|
|
capturedTables.forEach(t->t.putAll(modTable));
|
2019-02-15 20:00:41 -05:00
|
|
|
ForgeI18n.loadLanguageData(modTable);
|
2018-10-05 21:42:15 -04:00
|
|
|
}
|
|
|
|
|
}
|