-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #673 from Feuermagier/problem-type-desc
Conditional message overrides
- Loading branch information
Showing
13 changed files
with
256 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
autograder-api/src/main/java/de/firemage/autograder/api/AbstractTranslations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package de.firemage.autograder.api; | ||
|
||
import fluent.bundle.FluentBundle; | ||
|
||
public interface AbstractTranslations { | ||
FluentBundle getMainTranslations(); | ||
FluentBundle getConditionalTranslations(AbstractProblemType problemType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
autograder-core/src/main/java/de/firemage/autograder/core/LocalizedMessageForProblem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.firemage.autograder.core; | ||
|
||
import de.firemage.autograder.api.AbstractTranslations; | ||
import de.firemage.autograder.api.Translatable; | ||
import fluent.bundle.FluentBundle; | ||
|
||
import java.util.Optional; | ||
|
||
public record LocalizedMessageForProblem(Translatable translatable, ProblemType problemType) implements Translatable { | ||
public LocalizedMessageForProblem { | ||
if (translatable instanceof LocalizedMessageForProblem) { | ||
throw new IllegalArgumentException("LocalizedMessageForProblem cannot be nested"); | ||
} | ||
} | ||
|
||
@Override | ||
public String format(AbstractTranslations translations) { | ||
var conditionalBundle = translations.getConditionalTranslations(problemType); | ||
if (conditionalBundle != null) { | ||
var result = translatable.tryFormat(conditionalBundle); | ||
if (result.isPresent()) { | ||
return result.get(); | ||
} | ||
} | ||
|
||
return translatable.format(translations.getMainTranslations()); | ||
} | ||
|
||
@Override | ||
public Optional<String> tryFormat(FluentBundle bundle) { | ||
return translatable.tryFormat(bundle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
autograder-core/src/main/java/de/firemage/autograder/core/Translations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package de.firemage.autograder.core; | ||
|
||
import de.firemage.autograder.api.AbstractProblemType; | ||
import de.firemage.autograder.api.AbstractTranslations; | ||
import fluent.bundle.FluentBundle; | ||
import fluent.bundle.FluentResource; | ||
import fluent.functions.icu.ICUFunctionFactory; | ||
import fluent.syntax.parser.FTLParser; | ||
import fluent.syntax.parser.FTLStream; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public class Translations implements AbstractTranslations { | ||
private final FluentBundle mainTranslations; | ||
private final Map<AbstractProblemType, FluentBundle> conditionalTranslations; | ||
|
||
public Translations(Locale locale, List<FluentResource> mainOverrides, Map<AbstractProblemType, List<String>> conditionalOverrides) { | ||
String filename = switch (locale.getLanguage()) { | ||
case "de" -> "/strings.de.ftl"; | ||
case "en" -> "/strings.en.ftl"; | ||
default -> throw new IllegalArgumentException("No translation available for the locale " + locale); | ||
}; | ||
|
||
try { | ||
// == Normal messages | ||
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 : mainOverrides) { | ||
fluentBuilder.addResourceOverriding(bundle); | ||
} | ||
|
||
// default messages | ||
try (var stream = this.getClass().getResourceAsStream(filename)) { | ||
if (stream == null) { | ||
throw new IllegalStateException("Could not find the autograder messages"); | ||
} | ||
fluentBuilder.addResourceOverriding(FTLParser.parse(FTLStream.of(new String(stream.readAllBytes(), StandardCharsets.UTF_8)))); | ||
} | ||
|
||
this.mainTranslations = fluentBuilder.build(); | ||
|
||
// == Conditional messages | ||
this.conditionalTranslations = new HashMap<>(); | ||
for (var entry : conditionalOverrides.entrySet()) { | ||
var conditionalBuilder = FluentBundle.builder(locale, ICUFunctionFactory.INSTANCE); | ||
var content = String.join("\n", entry.getValue()); | ||
conditionalBuilder.addResource(FTLParser.parse(FTLStream.of(content))); | ||
this.conditionalTranslations.put(entry.getKey(), conditionalBuilder.build()); | ||
} | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public FluentBundle getMainTranslations() { | ||
return this.mainTranslations; | ||
} | ||
|
||
@Override | ||
public FluentBundle getConditionalTranslations(AbstractProblemType problemType) { | ||
return this.conditionalTranslations.get(problemType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.