forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qute type-safe messages: add convenient way to localize enum constants
- resolves quarkusio#40089
- Loading branch information
Showing
11 changed files
with
462 additions
and
49 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
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
281 changes: 251 additions & 30 deletions
281
...ions/qute/deployment/src/main/java/io/quarkus/qute/deployment/MessageBundleProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
74 changes: 74 additions & 0 deletions
74
.../qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleEnumTest.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,74 @@ | ||
package io.quarkus.qute.deployment.i18n; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.Template; | ||
import io.quarkus.qute.i18n.Message; | ||
import io.quarkus.qute.i18n.MessageBundle; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class MessageBundleEnumTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(Messages.class, MyEnum.class) | ||
.addAsResource("messages/enu.properties") | ||
.addAsResource("messages/enu_cs.properties") | ||
.addAsResource(new StringAsset( | ||
"{enu:myEnum(MyEnum:ON)}::{enu:myEnum(MyEnum:OFF)}::{enu:myEnum(MyEnum:UNDEFINED)}::" | ||
+ "{enu:shortEnum(MyEnum:ON)}::{enu:shortEnum(MyEnum:OFF)}::{enu:shortEnum(MyEnum:UNDEFINED)}::" | ||
+ "{enu:foo(MyEnum:ON)}::{enu:foo(MyEnum:OFF)}::{enu:foo(MyEnum:UNDEFINED)}::" | ||
+ "{enu:locFileOverride(MyEnum:ON)}::{enu:locFileOverride(MyEnum:OFF)}::{enu:locFileOverride(MyEnum:UNDEFINED)}"), | ||
"templates/foo.html")); | ||
|
||
@Inject | ||
Template foo; | ||
|
||
@Test | ||
public void testMessages() { | ||
assertEquals("On::Off::Undefined::1::0::U::+::-::_::on::off::undefined", foo.render()); | ||
assertEquals("Zapnuto::Vypnuto::Nedefinováno::1::0::N::+::-::_::zap::vyp::nedef", | ||
foo.instance().setLocale("cs").render()); | ||
} | ||
|
||
@MessageBundle(value = "enu", locale = "en") | ||
public interface Messages { | ||
|
||
// Replaced with: | ||
// @Message("{#when myEnum}" | ||
// + "{#is ON}{enu:myEnum_ON}" | ||
// + "{#is OFF}{enu:myEnum_OFF}" | ||
// + "{#is UNDEFINED}{enu:myEnum_UNDEFINED}" | ||
// + "{/when}") | ||
@Message | ||
String myEnum(MyEnum myEnum); | ||
|
||
// Replaced with: | ||
// @Message("{#when myEnum}" | ||
// + "{#is ON}{enu:shortEnum_ON}" | ||
// + "{#is OFF}{enu:shortEnum_OFF}" | ||
// + "{#is UNDEFINED}{enu:shortEnum_UNDEFINED}" | ||
// + "{/when}") | ||
@Message | ||
String shortEnum(MyEnum myEnum); | ||
|
||
@Message("{#when myEnum}" | ||
+ "{#is ON}+" | ||
+ "{#is OFF}-" | ||
+ "{#else}_" | ||
+ "{/when}") | ||
String foo(MyEnum myEnum); | ||
|
||
@Message | ||
String locFileOverride(MyEnum myEnum); | ||
|
||
} | ||
|
||
} |
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
10 changes: 10 additions & 0 deletions
10
extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MyEnum.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,10 @@ | ||
package io.quarkus.qute.deployment.i18n; | ||
|
||
import io.quarkus.qute.TemplateEnum; | ||
|
||
@TemplateEnum | ||
public enum MyEnum { | ||
ON, | ||
OFF, | ||
UNDEFINED | ||
} |
13 changes: 13 additions & 0 deletions
13
extensions/qute/deployment/src/test/resources/messages/enu.properties
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,13 @@ | ||
myEnum_ON=On | ||
myEnum_OFF=Off | ||
myEnum_UNDEFINED=Undefined | ||
|
||
shortEnum_ON=1 | ||
shortEnum_OFF=0 | ||
shortEnum_UNDEFINED=U | ||
|
||
locFileOverride={#when myEnum}\ | ||
{#is ON}on\ | ||
{#is OFF}off\ | ||
{#else}undefined\ | ||
{/when} |
13 changes: 13 additions & 0 deletions
13
extensions/qute/deployment/src/test/resources/messages/enu_cs.properties
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,13 @@ | ||
myEnum_ON=Zapnuto | ||
myEnum_OFF=Vypnuto | ||
myEnum_UNDEFINED=Nedefinováno | ||
|
||
shortEnum_ON=1 | ||
shortEnum_OFF=0 | ||
shortEnum_UNDEFINED=N | ||
|
||
locFileOverride={#when myEnum}\ | ||
{#is ON}zap\ | ||
{#is OFF}vyp\ | ||
{#else}nedef\ | ||
{/when} |
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