@@ -64,30 +64,31 @@ Below is an example of how one might implement a custom `Translator`.
6464public class MyTranslator implements Translator {
6565
6666 @Override
67- public @ NotNull Key name () {
67+ public Key name () {
6868 // Every translator has a name which is used to identify this specific translator instance.
6969 return Key . key(" mynamespace:mykey" );
7070 }
7171
7272 @Override
73- public @Nullable MessageFormat translate (final @NotNull String key , final @NotNull Locale locale ) {
73+ public @Nullable MessageFormat translate (final String key , final Locale locale ) {
7474 // You could retrieve a string from a properties file, a config file, or some other system.
75- // An an example, we will hard-code a check for a specific key here.
75+ // As an example, we will hard-code a check for a specific key here.
7676 if (key. equals(" mytranslation.key" ) && locale == Locale . US ) {
77- return new MessageFormat (" Hello %s !" , locale);
77+ return new MessageFormat (" Hello {0} !" , locale);
7878 } else {
7979 // If you only want to use component translation, you can override this method and always return `null`.
8080 return null ;
8181 }
8282 }
8383
8484 @Override
85- public @Nullable Component translate (@NotNull TranslatableComponent component , @NotNull Locale locale ) {
85+ public @Nullable Component translate (final TranslatableComponent component , final Locale locale ) {
8686 // As above, we will hardcode a check here, but you should be reading this from elsewhere.
87- if (key. equals(" mytranslation.colorful" ) && locale == Locale . US ) {
87+ if (component . key() . equals(" mytranslation.colorful" ) && locale == Locale . US ) {
8888 return Component . text(" Hello, " , NamedTextColor . GREEN )
89- .append(component. arguments(). get(0 ). color(NamedTextColor . RED ))
90- .append(component. children()); // Always make sure to copy the children over!
89+ .append(component. arguments(). stream(). map(it - > it. asComponent(). color(NamedTextColor . RED )). toList())
90+ .append(component. children()) // Always make sure to copy the children over!
91+ .applyFallbackStyle(component. style());
9192 } else {
9293 return null ;
9394 }
@@ -107,10 +108,10 @@ An example of how to use a translation store is below.
107108``` java
108109// As above, every translator needs an identifying name!
109110// Could also use TranslationStore#component(Key) to work with components instead.
110- final TranslationStore myStore = TranslationStore . messageFormat(Key . key(" mynamespace:mykey" ));
111+ final TranslationStore . StringBased< MessageFormat > myStore = TranslationStore . messageFormat(Key . key(" mynamespace:mykey" ));
111112
112113// You can add translations one-by-one, or in bulk. Consult the Javadocs for a full list of methods.
113- myStore. register(" mytranslation.key" , Locale . US , new MessageFormat (" Hello %s !" , Locale . US ));
114+ myStore. register(" mytranslation.key" , Locale . US , new MessageFormat (" Hello {0} !" , Locale . US ));
114115
115116// You can then register this to the global translator so the translations are available there!
116117GlobalTranslator . translator(). addSource(myStore);
0 commit comments