Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message Overrides #658

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package de.firemage.autograder.api;

import fluent.bundle.FluentBundle;
import fluent.bundle.FluentResource;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
Expand All @@ -20,6 +24,7 @@ class Builder {
private int threads;
private ClassLoader classLoader;
private int maxProblemsPerCheck = -1;
private List<FluentResource> messageOverrides = new ArrayList<>();

private Builder(Locale locale) {
this.locale = locale;
Expand Down Expand Up @@ -64,5 +69,14 @@ public ClassLoader getClassLoader() {
public Locale getLocale() {
return locale;
}

public Builder messagesOverride(FluentResource bundle) {
this.messageOverrides.add(bundle);
return this;
}

public List<FluentResource> getMessageOverrides() {
return this.messageOverrides;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ public Linter(
default -> throw new IllegalArgumentException("No translation available for the locale " + locale);
};
try {
FluentResource resource = FTLParser.parse(FTLStream.of(
new String(this.getClass().getResourceAsStream(filename).readAllBytes(), StandardCharsets.UTF_8)
var fluentBuilder = FluentBundle.builder(locale, ICUFunctionFactory.INSTANCE);

// The name FluentBuilder#addResourceOverriding is a lie; it does not override preexisting messages

// message overrides
for (var bundle : builder.getMessageOverrides()) {
fluentBuilder.addResourceOverriding(bundle);
}

// default messages
FluentResource defaultMessages = FTLParser.parse(FTLStream.of(
new String(this.getClass().getResourceAsStream(filename).readAllBytes(), StandardCharsets.UTF_8)
));
this.fluentBundle = FluentBundle.builder(locale, ICUFunctionFactory.INSTANCE).addResource(resource).build();
fluentBuilder.addResourceOverriding(defaultMessages);

this.fluentBundle = fluentBuilder.build();
} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.firemage.autograder.core;

import de.firemage.autograder.api.AbstractLinter;
import fluent.syntax.parser.FTLParser;
import fluent.syntax.parser.FTLStream;
import org.junit.jupiter.api.Test;

import java.util.Locale;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestMessageOverriding {
@Test
void testNoMessageOverride() {
var builder = AbstractLinter.builder(Locale.ENGLISH);
var linter = new Linter(builder);
String msg = linter.translateMessage(new LocalizedMessage("status-compiling"));
assertEquals("Compiling", msg);
}

@Test
void testSingleMessageOverride() {
String fluentFile = "status-compiling = Foo Bar";

var builder =
AbstractLinter.builder(Locale.ENGLISH).messagesOverride(FTLParser.parse(FTLStream.of(fluentFile)));
var linter = new Linter(builder);
String msg = linter.translateMessage(new LocalizedMessage("status-compiling"));
assertEquals("Foo Bar", msg);
}

@Test
void testDoubleMessageOverride() {
String fluentFile1 = "status-compiling = param {$x}";
String fluentFile2 = "status-compiling = Foo Bar";

var builder = AbstractLinter.builder(Locale.ENGLISH)
.messagesOverride(FTLParser.parse(FTLStream.of(fluentFile1)))
.messagesOverride(FTLParser.parse(FTLStream.of(fluentFile2)));
var linter = new Linter(builder);
String msg = linter.translateMessage(new LocalizedMessage("status-compiling", Map.of("x", "xyz")));
assertEquals("param xyz", msg);
}

@Test
void testNonOverriddenMessageWithOverrides() {
String fluentFile = "status-compiling = Foo Bar";

var builder =
AbstractLinter.builder(Locale.ENGLISH).messagesOverride(FTLParser.parse(FTLStream.of(fluentFile)));
var linter = new Linter(builder);
String msg = linter.translateMessage(new LocalizedMessage("status-model"));
assertEquals("Building the code model", msg);
}
}
Loading