Skip to content

Commit

Permalink
fix: Ensure MessageFormat thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed May 16, 2024
1 parent 3700973 commit 01fcdf5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

record MessageFormatTranslation(MessageFormat format) implements Translation {
record MessageFormatTranslation(String pattern, Locale locale) implements Translation {

private static final int MAX_NAMED_INDEX = 63;

@Override
public String formatArray(final List<Object> args) {
return format.format(args.toArray());
return createFormat().format(args.toArray());
}

@Override
public String formatArray(Object... args) {
return format.format(args);
return createFormat().format(args);
}

@Override
Expand All @@ -56,11 +57,15 @@ public String formatNamed(final Map<String, Object> args) {
}
entries.set(index, entry.getValue());
}
return format.format(entries.toArray());
return createFormat().format(entries.toArray());
}

@Override
public String formatEmpty() {
return format.toPattern();
return createFormat().toPattern();
}

private MessageFormat createFormat() {
return new MessageFormat(pattern, locale);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
Expand Down Expand Up @@ -73,8 +72,7 @@ default void registerAll(final Locale locale, final Map<String, Translation> for
* @throws IllegalArgumentException if a key is already registered
*/
default void registerAll(final Locale locale, final ResourceBundle bundle) {
this.registerAll(
locale, bundle.keySet(), key -> Translation.format(new MessageFormat(bundle.getString(key), locale)));
this.registerAll(locale, bundle.keySet(), key -> Translation.format(bundle.getString(key), locale));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package com.xpdustry.distributor.api.translation;

import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public interface Translation {
Expand All @@ -28,8 +28,8 @@ static Translation text(final String text) {
return new TextTranslation(text);
}

static Translation format(final MessageFormat format) {
return new MessageFormatTranslation(format);
static Translation format(final String pattern, final Locale locale) {
return new MessageFormatTranslation(pattern, locale);
}

String formatArray(final List<Object> args);
Expand Down

0 comments on commit 01fcdf5

Please sign in to comment.