Add multiple translation registries, support for Mojang JSON
This commit is contained in:
parent
4c47549253
commit
9ea82e9541
@ -18,11 +18,6 @@
|
|||||||
<name>BungeeCord-Chat</name>
|
<name>BungeeCord-Chat</name>
|
||||||
<description>Minecraft JSON chat API intended for use with BungeeCord</description>
|
<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>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
@ -6,11 +6,10 @@ import lombok.Setter;
|
|||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.MissingResourceException;
|
|
||||||
import java.util.ResourceBundle;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
import net.md_5.bungee.chat.TranslationRegistry;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@ -19,7 +18,6 @@ import lombok.ToString;
|
|||||||
public final class TranslatableComponent extends BaseComponent
|
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%]|$)" );
|
private final Pattern format = Pattern.compile( "%(?:(\\d+)\\$)?([A-Za-z%]|$)" );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,14 +137,7 @@ public final class TranslatableComponent extends BaseComponent
|
|||||||
@Override
|
@Override
|
||||||
protected void toPlainText(StringBuilder builder)
|
protected void toPlainText(StringBuilder builder)
|
||||||
{
|
{
|
||||||
String trans;
|
String trans = TranslationRegistry.INSTANCE.translate( translate );
|
||||||
try
|
|
||||||
{
|
|
||||||
trans = locales.getString( translate );
|
|
||||||
} catch ( MissingResourceException ex )
|
|
||||||
{
|
|
||||||
trans = translate;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matcher matcher = format.matcher( trans );
|
Matcher matcher = format.matcher( trans );
|
||||||
int position = 0;
|
int position = 0;
|
||||||
@ -184,14 +175,7 @@ public final class TranslatableComponent extends BaseComponent
|
|||||||
@Override
|
@Override
|
||||||
protected void toLegacyText(StringBuilder builder)
|
protected void toLegacyText(StringBuilder builder)
|
||||||
{
|
{
|
||||||
String trans;
|
String trans = TranslationRegistry.INSTANCE.translate( translate );
|
||||||
try
|
|
||||||
{
|
|
||||||
trans = locales.getString( translate );
|
|
||||||
} catch ( MissingResourceException e )
|
|
||||||
{
|
|
||||||
trans = translate;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matcher matcher = format.matcher( trans );
|
Matcher matcher = format.matcher( trans );
|
||||||
int position = 0;
|
int position = 0;
|
||||||
|
114
chat/src/main/java/net/md_5/bungee/chat/TranslationRegistry.java
Normal file
114
chat/src/main/java/net/md_5/bungee/chat/TranslationRegistry.java
Normal 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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user