Skip to content

Commit

Permalink
Add getText method with handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Alemiz112 committed Mar 25, 2023
1 parent c86958f commit ceb7755
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.mizerak.alemiz.translationlib.common.string;

import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;

public class JoinLocalString<T> implements LocalString<T> {
Expand Down Expand Up @@ -60,8 +61,8 @@ public String getFormatted(Locale locale) {
}

@Override
public String getText(T object) {
return this.left.getText(object) + this.delimiter + this.right.getText(object);
public String getText(T object, Consumer<TranslationContext<T>> handler) {
return this.left.getText(object, handler) + this.delimiter + this.right.getText(object, handler);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import eu.mizerak.alemiz.translationlib.common.structure.TranslationTerm;

import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;

public interface LocalString<T> {
Expand Down Expand Up @@ -59,7 +60,11 @@ default LocalString<T> withArgument(String name, Object argument) {

String getFormatted(Locale locale);

String getText(T object);
default String getText(T object) {
return this.getText(object, null);
}

String getText(T object, Consumer<TranslationContext<T>> handler);

void uploadFallbackMessage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;

public abstract class LocalStringBase<T> implements LocalString<T> {
Expand Down Expand Up @@ -63,7 +64,7 @@ public Collection<String> getArgumentNames() {
}

@Override
public String getText(T object) {
public String getText(T object, Consumer<TranslationContext<T>> handler) {
TranslationContext.Factory<?> factory = contextFactories.get(object.getClass());
if (factory == null) {
factory = contextFactories.get(object.getClass().getSuperclass());
Expand All @@ -73,6 +74,9 @@ public String getText(T object) {
}

TranslationContext<T> ctx = ((TranslationContext.Factory<T>) factory).create(object);
if (handler != null) {
handler.accept(ctx);
}

String formatted = this.getFormatted(ctx.getLocale());
if (!this.arguments.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.mizerak.alemiz.translationlib.common.string;

import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ public String getFormatted(Locale locale) {
}

@Override
public String getText(T t) {
public String getText(T object, Consumer<TranslationContext<T>> handler) {
return this.text;
}

Expand Down

0 comments on commit ceb7755

Please sign in to comment.