chat: Correct placeholder handling in translatable

This change makes TranslatableComponent still scan a
translation string for placeholders, even if the translation string
itself is being used (rather than a value found from the
ResourceBundle). This matches vanilla behavior and allows plugins that
use this system with serverside translations and other custom
translation systems to send output to the server console correctly.
This commit is contained in:
zml 2015-08-30 14:03:27 -07:00
parent b3d15d53d6
commit bd07be8772
No known key found for this signature in database
GPG Key ID: 75EFC1F079F6665E
2 changed files with 79 additions and 62 deletions

View File

@ -130,9 +130,14 @@ public class TranslatableComponent extends BaseComponent
@Override @Override
protected void toPlainText(StringBuilder builder) protected void toPlainText(StringBuilder builder)
{ {
String trans;
try try
{ {
String trans = locales.getString( translate ); 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;
int i = 0; int i = 0;
@ -162,10 +167,6 @@ public class TranslatableComponent extends BaseComponent
{ {
builder.append( trans.substring( position, trans.length() ) ); builder.append( trans.substring( position, trans.length() ) );
} }
} catch ( MissingResourceException e )
{
builder.append( translate );
}
super.toPlainText( builder ); super.toPlainText( builder );
} }
@ -173,9 +174,14 @@ public class TranslatableComponent extends BaseComponent
@Override @Override
protected void toLegacyText(StringBuilder builder) protected void toLegacyText(StringBuilder builder)
{ {
String trans;
try try
{ {
String trans = locales.getString( translate ); 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;
int i = 0; int i = 0;
@ -208,11 +214,6 @@ public class TranslatableComponent extends BaseComponent
addFormat( builder ); addFormat( builder );
builder.append( trans.substring( position, trans.length() ) ); builder.append( trans.substring( position, trans.length() ) );
} }
} catch ( MissingResourceException e )
{
addFormat( builder );
builder.append( translate );
}
super.toLegacyText( builder ); super.toLegacyText( builder );
} }

View File

@ -0,0 +1,16 @@
package net.md_5.bungee.api.chat;
import net.md_5.bungee.api.chat.TranslatableComponent;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TranslatableComponentTest {
@Test
public void testMissingPlaceholdersAdded()
{
TranslatableComponent testComponent = new TranslatableComponent( "Test string with %s placeholders: %s", "2", "aoeu" );
assertEquals( "Test string with 2 placeholders: aoeu", testComponent.toPlainText() );
assertEquals( "§fTest string with §f2§f placeholders: §faoeu", testComponent.toLegacyText() );
}
}