-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
order const. param to ResourceBundleMessageSource
This pull-request adds a `order` constructor parameter to `ResourceBundleMessageSource`. it is used to override the `Ordered::getOrder`. Currently, users which defined a MessageSource with cannot customize the `order` ``` @factory static class MessageSourceFactory { @singleton MessageSource createMessageSource() { return new MessageSource() { private final MessageSource delegate = new ResourceBundleMessageSource("i18n.messages"); @OverRide public @nonnull Optional<String> getRawMessage(@nonnull String code, @nonnull MessageContext context) { return delegate.getRawMessage(code, context); } @OverRide public @nonnull String interpolate(@nonnull String template, @nonnull MessageContext context) { return delegate.interpolate(template, context); } @OverRide public int getOrder() { return HIGHEST_PRECEDENCE; } }; } } ```
- Loading branch information
Showing
8 changed files
with
97 additions
and
6 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
53 changes: 53 additions & 0 deletions
53
context/src/test/java/io/micronaut/runtime/context/CompositeMessageSourceTest.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,53 @@ | ||
package io.micronaut.runtime.context; | ||
|
||
import io.micronaut.context.MessageSource; | ||
import io.micronaut.context.StaticMessageSource; | ||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.context.i18n.ResourceBundleMessageSource; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Singleton; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Locale; | ||
import java.util.Optional; | ||
|
||
import static io.micronaut.core.order.Ordered.HIGHEST_PRECEDENCE; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@Property(name = "spec.name", value = "CompositeMessageSourceTest") | ||
@MicronautTest(startApplication = false) | ||
class CompositeMessageSourceTest { | ||
private static final Locale SPANISH = new Locale("es", "ES"); | ||
|
||
@Test | ||
void messageSourcesAreSorted(MessageSource messageSource) { | ||
String code = "jakarta.validation.constraints.Positive.message"; | ||
Optional<String> messageOptional = messageSource.getMessage(code, Locale.ENGLISH); | ||
assertTrue(messageOptional.isPresent()); | ||
assertEquals("Must be positive", messageOptional.get()); | ||
messageOptional = messageSource.getMessage(code, SPANISH); | ||
assertTrue(messageOptional.isPresent()); | ||
assertEquals("Debe ser positivo", messageOptional.get()); | ||
} | ||
|
||
@Requires(property = "spec.name", value = "CompositeMessageSourceTest") | ||
@Factory | ||
static class MessageSourceFactory { | ||
@Singleton | ||
MessageSource createMessageSource() { | ||
return new ResourceBundleMessageSource("i18n.messages", HIGHEST_PRECEDENCE); | ||
} | ||
} | ||
|
||
@Requires(property = "spec.name", value = "CompositeMessageSourceTest") | ||
@Singleton | ||
static class DefaultMessages extends StaticMessageSource { | ||
DefaultMessages() { | ||
addMessage("jakarta.validation.constraints.Positive.message", "must be greater than 0"); | ||
} | ||
} | ||
} |
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 @@ | ||
jakarta.validation.constraints.Positive.message=Must be positive |
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,2 @@ | ||
jakarta.validation.constraints.Positive.message=Debe ser positivo | ||
|
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
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