Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #221 from SpigotMC/master
Browse files Browse the repository at this point in the history
Add multiple translation registries, support for Mojang JSON
  • Loading branch information
sleiss authored Aug 22, 2018
2 parents 84ccde5 + 9ea82e9 commit 8f5e906
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 24 deletions.
5 changes: 0 additions & 5 deletions chat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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%]|$)" );

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
114 changes: 114 additions & 0 deletions chat/src/main/java/net/md_5/bungee/chat/TranslationRegistry.java
Original file line number Diff line number Diff line change
@@ -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 );
}
}
}

0 comments on commit 8f5e906

Please sign in to comment.