Skip to content

Commit

Permalink
Also enable translation of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed May 9, 2022
1 parent 030cb0e commit de49d41
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.4.0-alpha.32'
version = '1.4.0-alpha.33'

sourceCompatibility = "17"
targetCompatibility = "17"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ protected Optional<PlaceholderData> resolveSimplePlaceholder(Object property, St
return Optional.of(new ScalarPlaceholderData<>(number, numberFormat::format));
} else if (property instanceof String propertyString && isFieldAnnotatedWith(bean.getClass(), placeholderName, Translatable.class)) {
return Optional.of(new ScalarPlaceholderData<>(options.translate(propertyString, locale).orElse(propertyString)));
} else if (property instanceof Enum<?> enumProperty && isFieldAnnotatedWith(bean.getClass(), placeholderName, Translatable.class)) {
var translation = options.translate(enumProperty.toString(), locale);
if (translation.isPresent()) {
return Optional.of(new ScalarPlaceholderData<>(translation.get()));
} else {
return Optional.of(new ScalarPlaceholderData<>(enumProperty));
}
} else if (property instanceof Enum || property instanceof String || ReflectionUtils.isWrapperType(property.getClass())) {
return Optional.of(new ScalarPlaceholderData<>(property));
} else if (property instanceof Temporal temporal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void doCustomTranslations() {
}

@Test
void translatesAnnotatedTerm() {
void translatesAnnotatedString() {
// Arrange
var options = new GenerationOptionsBuilder()
.withTranslation((term, locale) -> {
Expand All @@ -77,6 +77,26 @@ void translatesAnnotatedTerm() {
assertThat(commandingStyle.get().getRawValue(), equalTo("Einfühlend"));
}

@Test
void translatesAnnotatedEnum() {
// Arrange
var options = new GenerationOptionsBuilder()
.withTranslation((term, locale) -> {
if (term.equals("Red") && locale.equals(Locale.GERMAN)) {
return Optional.of("Rot");
}
return Optional.empty();
})
.build();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD, new CustomPlaceholderRegistryImpl(), options, null);

// Act
Optional<PlaceholderData> commandingStyle = resolver.resolve("uniform", Locale.GERMAN);

// Assert
assertThat(commandingStyle.get().getRawValue(), equalTo("Rot"));
}

public static class CustomTranslatedPlaceholder extends CustomWordPlaceholderData {

public CustomTranslatedPlaceholder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Captain {

private final String name;
private final int rank;
@Translatable
private final Uniform uniform;
private final FirstOfficer officer;
private final List<Service> services;
Expand Down

0 comments on commit de49d41

Please sign in to comment.