diff --git a/chat/pom.xml b/chat/pom.xml index 92f04c62..99b089cb 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 16e8d2a4..54da4467 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 lombok.Setter; 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 @@ import lombok.ToString; 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 final class TranslatableComponent extends BaseComponent @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 @@ public final class TranslatableComponent extends BaseComponent @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 00000000..89a4b668 --- /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 ); + } + } +}