Skip to content

Commit

Permalink
chore: Removed ComponentAwareTranslation
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Jun 8, 2024
1 parent 22c7612 commit b4dd1f3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 83 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface MessageFormatTranslation extends Translation {

/**
* Creates a new {@link MessageFormatTranslation} with the given pattern and locale.
* The implementation returned by this method is also {@link ComponentAwareTranslation compatible with components}.
* The implementation returned by this method supports {@link com.xpdustry.distributor.api.component.render.ComponentStringBuilder}.
*
* @param pattern the pattern
* @param locale the locale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
import java.util.List;
import java.util.Locale;

record MessageFormatTranslationImpl(String pattern, Locale locale)
implements MessageFormatTranslation, ComponentAwareTranslation {
record MessageFormatTranslationImpl(String pattern, Locale locale) implements MessageFormatTranslation {

private static final int MAX_NAMED_INDEX = 63;
private static final Object[] EMPTY_ARRAY = new Object[MAX_NAMED_INDEX + 1];
Expand All @@ -57,49 +56,28 @@ public Locale getLocale() {
}

@Override
public void formatTo(final TranslationArguments parameters, final ComponentStringBuilder builder) {
if (parameters instanceof TranslationArguments.Array array) {
formatTo(array.getArguments(), builder);
} else if (parameters instanceof TranslationArguments.Named named) {
final List<Object> entries = new ArrayList<>();
for (final var entry : named.getArguments().entrySet()) {
final int index;
try {
index = Integer.parseInt(entry.getKey());
} catch (NumberFormatException ignored) {
continue;
}
if (index > MAX_NAMED_INDEX) {
throw new IllegalArgumentException(
"Max argument index exceeded, expected less than " + MAX_NAMED_INDEX + ", got " + index);
}
for (int i = entries.size(); i <= index; i++) {
entries.add(null);
}
entries.set(index, entry.getValue());
}
formatTo(entries, builder);
} else {
throw new IllegalArgumentException("Unsupported arguments type: " + parameters.getClass());
}
public String format(final TranslationArguments parameters) {
return createFormat().format(getArguments(parameters).toArray());
}

@SuppressWarnings("JdkObsolete")
private void formatTo(final List<Object> arguments, final ComponentStringBuilder builder) {
final var format = new MessageFormat(pattern, locale);
@Override
public void formatTo(final TranslationArguments parameters, final ComponentStringBuilder builder) {
final var format = createFormat();
final var arguments = getArguments(parameters);
if (arguments.isEmpty()) {
builder.append(format.format(null));
return;
}

final var buffer = format.format(EMPTY_ARRAY, new StringBuffer(), null);
final var unformatted = format.format(EMPTY_ARRAY);
final var iterator = format.formatToCharacterIterator(EMPTY_ARRAY);

while (iterator.getIndex() < iterator.getEndIndex()) {
final int end = iterator.getRunLimit();
final var index = (Integer) iterator.getAttribute(MessageFormat.Field.ARGUMENT);
if (index == null) {
builder.append(buffer, iterator.getIndex(), end);
builder.append(unformatted, iterator.getIndex(), end);
} else {
var argument = arguments.get(index);
var style = TextStyle.none();
Expand Down Expand Up @@ -140,4 +118,35 @@ private void formatTo(final List<Object> arguments, final ComponentStringBuilder
iterator.setIndex(end);
}
}

private List<Object> getArguments(final TranslationArguments arguments) {
if (arguments instanceof TranslationArguments.Array array) {
return array.getArguments();
} else if (arguments instanceof TranslationArguments.Named named) {
final List<Object> entries = new ArrayList<>();
for (final var entry : named.getArguments().entrySet()) {
final int index;
try {
index = Integer.parseInt(entry.getKey());
} catch (NumberFormatException ignored) {
continue;
}
if (index > MAX_NAMED_INDEX) {
throw new IllegalArgumentException(
"Max argument index exceeded, expected less than " + MAX_NAMED_INDEX + ", got " + index);
}
for (int i = entries.size(); i <= index; i++) {
entries.add(null);
}
entries.set(index, entry.getValue());
}
return entries;
} else {
throw new IllegalArgumentException("Unsupported arguments type: " + arguments.getClass());
}
}

private MessageFormat createFormat() {
return new MessageFormat(pattern, locale);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package com.xpdustry.distributor.api.translation;

import com.xpdustry.distributor.api.component.render.ComponentStringBuilder;

/**
* Represents a translation from a given translation system.
*/
Expand All @@ -30,4 +32,14 @@ public interface Translation {
* @return the formatted translation
*/
String format(final TranslationArguments parameters);

/**
* Formats the translation to the given {@link ComponentStringBuilder}.
*
* @param parameters the translation parameters
* @param builder the builder
*/
default void formatTo(final TranslationArguments parameters, final ComponentStringBuilder builder) {
builder.append(format(parameters));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.xpdustry.distributor.api.component.render.ComponentStringBuilder;
import com.xpdustry.distributor.api.component.style.TemporalStyle;
import com.xpdustry.distributor.api.key.StandardKeys;
import com.xpdustry.distributor.api.translation.ComponentAwareTranslation;
import java.time.ZoneId;
import java.util.Locale;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -104,18 +103,14 @@ private static final class TranslatableComponentRenderer implements ComponentRen

@Override
public void render(final TranslatableComponent component, final ComponentStringBuilder builder) {
final var translation = DistributorProvider.get()
DistributorProvider.get()
.getGlobalTranslationSource()
.getTranslationOrMissing(
component.getKey(),
builder.getContext()
.getOptional(StandardKeys.LOCALE)
.orElseGet(Locale::getDefault));
if (translation instanceof ComponentAwareTranslation aware) {
aware.formatTo(component.getParameters(), builder);
} else {
builder.append(translation.format(component.getParameters()));
}
.orElseGet(Locale::getDefault))
.formatTo(component.getParameters(), builder);
}
}

Expand Down

0 comments on commit b4dd1f3

Please sign in to comment.