added translation support for extensions
This commit is contained in:
1 parent
706ddb546a
commit
e7170ddef9
2 files changed
+42
-13
No files matched your search
@@ -6,6 +6,8 @@ import net.minecraft.server.MinecraftServer;
|
|||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public interface CarpetExtension
|
public interface CarpetExtension
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -51,4 +53,6 @@ public interface CarpetExtension
|
|||||||
default String version() {return null;}
|
default String version() {return null;}
|
||||||
|
|
||||||
default void registerLoggers() {}
|
default void registerLoggers() {}
|
||||||
|
|
||||||
|
default Map<String, String> canHasTranslations(String lang) { return null;}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package carpet.utils;
|
package carpet.utils;
|
||||||
|
|
||||||
|
import carpet.CarpetExtension;
|
||||||
|
import carpet.CarpetServer;
|
||||||
import carpet.CarpetSettings;
|
import carpet.CarpetSettings;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
@@ -8,8 +10,8 @@ import net.minecraft.server.command.ServerCommandSource;
|
|||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@@ -37,6 +39,22 @@ public class Translations
|
|||||||
return translationMap != null && translationMap.containsKey(key);
|
return translationMap != null && translationMap.containsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> getTranslationFromResourcePath(String path)
|
||||||
|
{
|
||||||
|
String dataJSON;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
dataJSON = IOUtils.toString(
|
||||||
|
Objects.requireNonNull(Translations.class.getClassLoader().getResourceAsStream(String.format("assets/carpet/lang/%s.json", CarpetSettings.language))),
|
||||||
|
StandardCharsets.UTF_8);
|
||||||
|
} catch (NullPointerException | IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
|
||||||
|
return gson.fromJson(dataJSON, new TypeToken<Map<String, String>>() {}.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void updateLanguage(ServerCommandSource source)
|
public static void updateLanguage(ServerCommandSource source)
|
||||||
{
|
{
|
||||||
if (CarpetSettings.language.equalsIgnoreCase("none"))
|
if (CarpetSettings.language.equalsIgnoreCase("none"))
|
||||||
@@ -44,23 +62,30 @@ public class Translations
|
|||||||
translationMap = null;
|
translationMap = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String langJs;
|
Map<String, String> translations = new HashMap<>();
|
||||||
try
|
Map<String, String> trans = getTranslationFromResourcePath(String.format("assets/carpet/lang/%s.json", CarpetSettings.language));
|
||||||
|
if (trans != null) trans.forEach(translations::put);
|
||||||
|
|
||||||
|
for (CarpetExtension ext : CarpetServer.extensions)
|
||||||
{
|
{
|
||||||
langJs = IOUtils.toString(
|
Map<String, String> extMappings = ext.canHasTranslations(CarpetSettings.language);
|
||||||
Objects.requireNonNull(Translations.class.getClassLoader().getResourceAsStream(String.format("assets/carpet/lang/%s.json", CarpetSettings.language))),
|
if (extMappings != null)
|
||||||
StandardCharsets.UTF_8);
|
{
|
||||||
} catch (NullPointerException | IOException e) {
|
extMappings.forEach((key, value) ->
|
||||||
Messenger.m(source, "r Failed to update language");
|
{
|
||||||
|
if (!translations.containsKey(key)) translations.put(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translations.entrySet().removeIf(e -> e.getKey().startsWith("//"));
|
||||||
|
if (translations.isEmpty())
|
||||||
|
{
|
||||||
|
translationMap = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
|
translationMap = translations;
|
||||||
Type type = new TypeToken<Map<String, String>>() {}.getType();
|
|
||||||
translationMap = gson.fromJson(langJs, type);
|
|
||||||
translationMap.entrySet().removeIf(e -> e.getKey().startsWith("//"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean isValidLanguage(String newValue)
|
public static boolean isValidLanguage(String newValue)
|
||||||
{
|
{
|
||||||
// will put some validations for availble languages at some point
|
// will put some validations for availble languages at some point
|
||||||
|
|||||||
Reference in new issue
Block a user