import java.util.Locale;
import java.util.Map;
+import javax.annotation.Nonnull;
+
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.util.template.Filter;
import net.pterodactylus.util.template.TemplateContext;
*/
@Override
public String format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
- if (data instanceof L10nText) {
- L10nText l10nText = (L10nText) data;
- if (l10nText.getParameters().isEmpty()) {
- return webInterface.getL10n().getString(l10nText.getText());
- }
- return new MessageFormat(webInterface.getL10n().getString(l10nText.getText()), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(l10nText.getParameters().toArray());
+ List<Object> parameterValues = getParameters(data, parameters);
+ String text = getText(data);
+ if (parameterValues.isEmpty()) {
+ return webInterface.getL10n().getString(text);
}
- if (parameters.isEmpty()) {
- return webInterface.getL10n().getString(String.valueOf(data));
+ return new MessageFormat(webInterface.getL10n().getString(text), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(parameterValues.toArray());
+ }
+
+ @Nonnull
+ private String getText(Object data) {
+ return (data instanceof L10nText) ? ((L10nText) data).getText() : String.valueOf(data);
+ }
+
+ @Nonnull
+ private List<Object> getParameters(Object data, Map<String, Object> parameters) {
+ if (data instanceof L10nText) {
+ return ((L10nText) data).getParameters();
}
List<Object> parameterValues = new ArrayList<Object>();
int parameterIndex = 0;
parameterValues.add(value);
++parameterIndex;
}
- return new MessageFormat(webInterface.getL10n().getString(String.valueOf(data)), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(parameterValues.toArray());
+ return parameterValues;
}
}
@Test
fun `filter processes l10n text without parameters correctly`() {
translations["data"] = "translated data"
- assertThat(filter.format(templateContext, L10nText("data"), emptyMap()), equalTo("translated data"))
+ assertThat(filter.format(templateContext, L10nText("data"), emptyMap()), equalTo("translated data"))
}
@Test
assertThat(filter.format(templateContext, L10nText("data", listOf(4.5, "data")), emptyMap()), equalTo("translated 4.5 data"))
}
+ @Test
+ fun `filter does not replace values if there are no parameters`() {
+ translations["data"] = "{link}"
+ assertThat(filter.format(templateContext, "data", emptyMap()), equalTo("{link}"));
+ }
+
}