From 9ea82e95417ed034a57c218c68bf2ded57111aa7 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 16 Aug 2018 07:46:26 +1000 Subject: [PATCH] Add multiple translation registries, support for Mojang JSON --- chat/pom.xml | 5 - .../api/chat/TranslatableComponent.java | 22 +--- .../md_5/bungee/chat/TranslationRegistry.java | 114 ++++++++++++++++++ 3 files changed, 117 insertions(+), 24 deletions(-) create mode 100644 chat/src/main/java/net/md_5/bungee/chat/TranslationRegistry.java diff --git a/chat/pom.xml b/chat/pom.xml index 92f04c6205..99b089cbd8 100644 --- a/chat/pom.xml +++ b/chat/pom.xml @@ -18,11 +18,6 @@ BungeeCord-Chat Minecraft JSON chat API intended for use with BungeeCord - - 1.6 - 1.6 - - com.google.code.gson diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java index 16e8d2a462..54da446724 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java @@ -6,11 +6,10 @@ import net.md_5.bungee.api.ChatColor; import java.util.ArrayList; import java.util.List; -import java.util.MissingResourceException; -import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.ToString; +import net.md_5.bungee.chat.TranslationRegistry; @Getter @Setter @@ -19,7 +18,6 @@ public final class TranslatableComponent extends BaseComponent { - private final ResourceBundle locales = ResourceBundle.getBundle( "mojang-translations/en_US" ); private final Pattern format = Pattern.compile( "%(?:(\\d+)\\$)?([A-Za-z%]|$)" ); /** @@ -139,14 +137,7 @@ public void addWith(BaseComponent component) @Override protected void toPlainText(StringBuilder builder) { - String trans; - try - { - trans = locales.getString( translate ); - } catch ( MissingResourceException ex ) - { - trans = translate; - } + String trans = TranslationRegistry.INSTANCE.translate( translate ); Matcher matcher = format.matcher( trans ); int position = 0; @@ -184,14 +175,7 @@ protected void toPlainText(StringBuilder builder) @Override protected void toLegacyText(StringBuilder builder) { - String trans; - try - { - trans = locales.getString( translate ); - } catch ( MissingResourceException e ) - { - trans = translate; - } + String trans = TranslationRegistry.INSTANCE.translate( translate ); Matcher matcher = format.matcher( trans ); int position = 0; diff --git a/chat/src/main/java/net/md_5/bungee/chat/TranslationRegistry.java b/chat/src/main/java/net/md_5/bungee/chat/TranslationRegistry.java new file mode 100644 index 0000000000..89a4b668f4 --- /dev/null +++ b/chat/src/main/java/net/md_5/bungee/chat/TranslationRegistry.java @@ -0,0 +1,114 @@ +package net.md_5.bungee.chat; + +import com.google.common.base.Charsets; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.ResourceBundle; +import lombok.AccessLevel; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TranslationRegistry +{ + + public static final TranslationRegistry INSTANCE = new TranslationRegistry(); + // + private final List providers = new LinkedList<>(); + + static + { + try + { + INSTANCE.addProvider( new JsonProvider( "/assets/minecraft/lang/en_us.json" ) ); + } catch ( Exception ex ) + { + } + + try + { + INSTANCE.addProvider( new ResourceBundleProvider( "mojang-translations/en_US" ) ); + } catch ( Exception ex ) + { + } + } + + private void addProvider(TranslationProvider provider) + { + providers.add( provider ); + } + + public String translate(String s) + { + for ( TranslationProvider provider : providers ) + { + String translation = provider.translate( s ); + + if ( translation != null ) + { + return translation; + } + } + + return s; + } + + private interface TranslationProvider + { + + String translate(String s); + } + + @Data + private static class ResourceBundleProvider implements TranslationProvider + { + + private final ResourceBundle bundle; + + public ResourceBundleProvider(String bundlePath) + { + this.bundle = ResourceBundle.getBundle( bundlePath ); + } + + @Override + public String translate(String s) + { + return ( bundle.containsKey( s ) ) ? bundle.getString( s ) : null; + } + } + + @Data + @ToString(exclude = "translations") + private static class JsonProvider implements TranslationProvider + { + + private final Map translations = new HashMap<>(); + + public JsonProvider(String resourcePath) throws IOException + { + try ( InputStreamReader rd = new InputStreamReader( JsonProvider.class.getResourceAsStream( resourcePath ), Charsets.UTF_8 ) ) + { + JsonObject obj = new Gson().fromJson( rd, JsonObject.class ); + for ( Map.Entry entries : obj.entrySet() ) + { + translations.put( entries.getKey(), entries.getValue().getAsString() ); + } + } + } + + @Override + public String translate(String s) + { + return translations.get( s ); + } + } +}