Add multiple translation registries, support for Mojang JSON

This commit is contained in:
md_5 2018-08-16 07:46:26 +10:00
parent 4c47549253
commit 9ea82e9541
3 changed files with 117 additions and 24 deletions

View File

@ -18,11 +18,6 @@
<name>BungeeCord-Chat</name>
<description>Minecraft JSON chat API intended for use with BungeeCord</description>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>

View File

@ -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;

View File

@ -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<TranslationProvider> 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<String, String> 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<String, JsonElement> entries : obj.entrySet() )
{
translations.put( entries.getKey(), entries.getValue().getAsString() );
}
}
}
@Override
public String translate(String s)
{
return translations.get( s );
}
}
}