Skip to content

Commit

Permalink
#3558: Add Translatable interface for fluid creation of TranslatableC…
Browse files Browse the repository at this point in the history
…omponents
  • Loading branch information
2008Choco authored and md-5 committed Nov 9, 2023
1 parent 39b10c0 commit 16298a7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
27 changes: 27 additions & 0 deletions chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ public ComponentBuilder append(BaseComponent[] components, FormatRetention reten
return this;
}

/**
* Appends the {@link TranslationProvider} object to the builder and makes
* the last element the current target for formatting. The components will
* have all the formatting from previous part.
*
* @param translatable the translatable object to append
* @return this ComponentBuilder for chaining
*/
public ComponentBuilder append(TranslationProvider translatable)
{
return append( translatable, FormatRetention.ALL );
}

/**
* Appends the {@link TranslationProvider} object to the builder and makes
* the last element the current target for formatting. You can specify the
* amount of formatting retained from previous part.
*
* @param translatable the translatable object to append
* @param retention the formatting to retain
* @return this ComponentBuilder for chaining
*/
public ComponentBuilder append(TranslationProvider translatable, FormatRetention retention)
{
return append( translatable.asTranslatableComponent(), retention );
}

/**
* Appends the text to the builder and makes it the current target for
* formatting. The text will have all the formatting from previous part.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ public TranslatableComponent(String translate, Object... with)
}
}

/**
* Creates a translatable component with the passed substitutions
*
* @param translatable the translatable object
* @param with the {@link java.lang.String}s and
* {@link net.md_5.bungee.api.chat.BaseComponent}s to use into the
* translation
* @see #translate
* @see #setWith(java.util.List)
*/
public TranslatableComponent(TranslationProvider translatable, Object... with)
{
this( translatable.getTranslationKey(), with );
}

/**
* Creates a duplicate of this TranslatableComponent.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.md_5.bungee.api.chat;

/**
* An object capable of being translated by the client in a
* {@link TranslatableComponent}.
*/
public interface TranslationProvider
{

/**
* Get the translation key.
*
* @return the translation key
*/
String getTranslationKey();

/**
* Get this translatable object as a {@link TranslatableComponent}.
*
* @return the translatable component
*/
default TranslatableComponent asTranslatableComponent()
{
return asTranslatableComponent( (Object[]) null );
}

/**
* Get this translatable object as a {@link TranslatableComponent}.
*
* @param with the {@link String Strings} and
* {@link BaseComponent BaseComponents} to use in the translation
* @return the translatable component
*/
default TranslatableComponent asTranslatableComponent(Object... with)
{
return new TranslatableComponent( this, with );
}
}

0 comments on commit 16298a7

Please sign in to comment.