Search in sources :

Example 1 with MinecraftTranslation

use of org.lanternpowered.server.text.translation.MinecraftTranslation in project LanternServer by LanternPowered.

the class JsonTextTranslatableSerializer method serialize.

@SuppressWarnings("deprecation")
@Override
public JsonElement serialize(TranslatableText src, Type typeOfSrc, JsonSerializationContext context) {
    final Translation translation = src.getTranslation();
    if (this.networkingFormat && !(translation instanceof MinecraftTranslation)) {
        final ImmutableList<Object> arguments = src.getArguments();
        final Object[] rawArguments = arguments.toArray(new Object[arguments.size()]);
        final Locale locale = currentLocale.get();
        for (int i = 0; i < rawArguments.length; i++) {
            Object object = rawArguments[i];
            if (object instanceof TextRepresentable) {
                if (!(object instanceof Text)) {
                    object = ((TextRepresentable) object).toText();
                }
                rawArguments[i] = ((LanternTextSerializer) TextSerializers.LEGACY_FORMATTING_CODE).serialize((Text) object, locale);
            } else {
                rawArguments[i] = object.toString();
            }
        }
        final String content = src.getTranslation().get(locale, rawArguments);
        return JsonTextLiteralSerializer.serializeLiteralText(src, content, context, this.removeComplexity);
    }
    final JsonObject obj = new JsonObject();
    obj.addProperty(TRANSLATABLE, src.getTranslation().getId());
    final ImmutableList<Object> arguments = src.getArguments();
    if (!arguments.isEmpty()) {
        final JsonArray argumentsArray = new JsonArray();
        for (Object object : arguments) {
            // so we need to convert the objects if possible
            if (object instanceof TextRepresentable) {
                if (!(object instanceof Text)) {
                    object = ((TextRepresentable) object).toText();
                }
                argumentsArray.add(context.serialize(object, Text.class));
            } else {
                argumentsArray.add(new JsonPrimitive(object.toString()));
            }
        }
        obj.add(TRANSLATABLE_ARGS, argumentsArray);
    }
    serialize(obj, src, context);
    return obj;
}
Also used : Locale(java.util.Locale) TextRepresentable(org.spongepowered.api.text.TextRepresentable) MinecraftTranslation(org.lanternpowered.server.text.translation.MinecraftTranslation) Translation(org.spongepowered.api.text.translation.Translation) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) Text(org.spongepowered.api.text.Text) TranslatableText(org.spongepowered.api.text.TranslatableText) JsonArray(com.google.gson.JsonArray) MinecraftTranslation(org.lanternpowered.server.text.translation.MinecraftTranslation) JsonObject(com.google.gson.JsonObject)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Locale (java.util.Locale)1 MinecraftTranslation (org.lanternpowered.server.text.translation.MinecraftTranslation)1 Text (org.spongepowered.api.text.Text)1 TextRepresentable (org.spongepowered.api.text.TextRepresentable)1 TranslatableText (org.spongepowered.api.text.TranslatableText)1 Translation (org.spongepowered.api.text.translation.Translation)1