Skip to content

Commit

Permalink
Make translation targets match languages more broadly
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Oct 30, 2023
1 parent 89bb092 commit 9b6f6f1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.lovetropics.extras.mixin.translation;

import com.lovetropics.extras.translation.TranslatableChatMessage;
import com.lovetropics.extras.translation.TranslatableLanguage;
import com.lovetropics.extras.translation.TranslationBundle;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.PlayerChatMessage;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -17,7 +19,7 @@ public abstract class PlayerChatMessageMixin implements TranslatableChatMessage
public abstract String signedContent();

@Unique
private final Map<String, String> ltextras$translations = new Object2ObjectOpenHashMap<>();
private final Map<TranslatableLanguage, String> ltextras$translations = new Object2ObjectOpenHashMap<>();

@Override
public void ltextras$addTranslations(final TranslationBundle bundle) {
Expand All @@ -27,7 +29,7 @@ public abstract class PlayerChatMessageMixin implements TranslatableChatMessage
@Override
public PlayerChatMessage ltextras$translate(final String language) {
final PlayerChatMessage self = (PlayerChatMessage) (Object) this;
final String translation = ltextras$translations.get(language);
final String translation = lTExtras$getTranslationFor(language);
if (translation != null && !translation.equals(signedContent())) {
return self.withUnsignedContent(Component.literal(translation));
}
Expand All @@ -36,7 +38,14 @@ public abstract class PlayerChatMessageMixin implements TranslatableChatMessage

@Override
public boolean ltextras$hasTranslationFor(final String language) {
final String translation = ltextras$translations.get(language);
final String translation = lTExtras$getTranslationFor(language);
return translation != null && !translation.equals(signedContent());
}

@Unique
@Nullable
private String lTExtras$getTranslationFor(final String language) {
final TranslatableLanguage languageType = TranslatableLanguage.byKey(language);
return languageType != null ? ltextras$translations.get(languageType) : null;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.lovetropics.extras.translation;

import net.minecraft.util.StringRepresentable;
import javax.annotation.Nullable;

public enum TranslatableLanguage implements StringRepresentable {
ENGLISH("en_us"),
SPANISH("es_es"),
FRENCH("fr_fr"),
public enum TranslatableLanguage {
ENGLISH,
SPANISH,
FRENCH,
;

public static final EnumCodec<TranslatableLanguage> CODEC = StringRepresentable.fromEnum(TranslatableLanguage::values);

private final String key;

TranslatableLanguage(final String key) {
this.key = key;
}

@Override
public String getSerializedName() {
return key;
@Nullable
public static TranslatableLanguage byKey(final String key) {
if (key.startsWith("en_")) {
return ENGLISH;
} else if (key.startsWith("es_")) {
return SPANISH;
} else if (key.startsWith("fr_")) {
return FRENCH;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import java.util.Map;

public record TranslationBundle(Map<String, String> stringsByLanguage) {
public record TranslationBundle(Map<TranslatableLanguage, String> stringsByLanguage) {
public static final TranslationBundle EMPTY = new TranslationBundle(Map.of());
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ private boolean isDisabled() {
}

public CompletableFuture<TranslationBundle> translate(final String language, final String text) {
final TranslatableLanguage languageType = TranslatableLanguage.CODEC.byName(language);
final TranslatableLanguage languageType = TranslatableLanguage.byKey(language);
if (languageType == null || isDisabled()) {
return CompletableFuture.completedFuture(new TranslationBundle(Map.of(language, text)));
return CompletableFuture.completedFuture(TranslationBundle.EMPTY);
}
return switch (languageType) {
case ENGLISH ->
Expand All @@ -58,15 +58,15 @@ public CompletableFuture<TranslationBundle> translate(final String language, fin
}

private static TranslationBundle createBundle(@Nullable final String english, @Nullable final String spanish, @Nullable final String french) {
final ImmutableMap.Builder<String, String> stringsByLanguage = ImmutableMap.builderWithExpectedSize(3);
final ImmutableMap.Builder<TranslatableLanguage, String> stringsByLanguage = ImmutableMap.builderWithExpectedSize(3);
if (english != null) {
stringsByLanguage.put(TranslatableLanguage.ENGLISH.getSerializedName(), english);
stringsByLanguage.put(TranslatableLanguage.ENGLISH, english);
}
if (spanish != null) {
stringsByLanguage.put(TranslatableLanguage.SPANISH.getSerializedName(), spanish);
stringsByLanguage.put(TranslatableLanguage.SPANISH, spanish);
}
if (french != null) {
stringsByLanguage.put(TranslatableLanguage.FRENCH.getSerializedName(), french);
stringsByLanguage.put(TranslatableLanguage.FRENCH, french);
}
return new TranslationBundle(stringsByLanguage.build());
}
Expand Down

0 comments on commit 9b6f6f1

Please sign in to comment.