Skip to content

Commit 30d733f

Browse files
authored
Merge pull request #44385 from mkouba/issue-44366
Qute: fix generation of qute-i18n-examples
2 parents 5be2257 + 959a2e1 commit 30d733f

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/MessageBundleMethodBuildItem.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ public final class MessageBundleMethodBuildItem extends MultiBuildItem {
1818
private final MethodInfo method;
1919
private final String template;
2020
private final boolean isDefaultBundle;
21+
private final boolean hasGeneratedTemplate;
2122

2223
MessageBundleMethodBuildItem(String bundleName, String key, String templateId, MethodInfo method, String template,
23-
boolean isDefaultBundle) {
24+
boolean isDefaultBundle, boolean hasGeneratedTemplate) {
2425
this.bundleName = bundleName;
2526
this.key = key;
2627
this.templateId = templateId;
2728
this.method = method;
2829
this.template = template;
2930
this.isDefaultBundle = isDefaultBundle;
31+
this.hasGeneratedTemplate = hasGeneratedTemplate;
3032
}
3133

3234
public String getBundleName() {
@@ -54,6 +56,11 @@ public MethodInfo getMethod() {
5456
return method;
5557
}
5658

59+
/**
60+
*
61+
* @return {@code true} if there is a corresponding method declared on the message bundle interface
62+
* @see #getMethod()
63+
*/
5764
public boolean hasMethod() {
5865
return method != null;
5966
}
@@ -79,6 +86,14 @@ public boolean isDefaultBundle() {
7986
return isDefaultBundle;
8087
}
8188

89+
/**
90+
*
91+
* @return {@code true} if the template was generated, e.g. a message bundle method for an enum
92+
*/
93+
public boolean hasGeneratedTemplate() {
94+
return hasGeneratedTemplate;
95+
}
96+
8297
/**
8398
*
8499
* @return the path

extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/MessageBundleProcessor.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,22 @@ void generateExamplePropertiesFiles(List<MessageBundleMethodBuildItem> messageBu
702702
List<MessageBundleMethodBuildItem> messages = entry.getValue();
703703
messages.sort(Comparator.comparing(MessageBundleMethodBuildItem::getKey));
704704
Path exampleProperties = generatedExamplesDir.resolve(entry.getKey() + ".properties");
705-
Files.write(exampleProperties,
706-
messages.stream().map(m -> m.getMethod().name() + "=" + m.getTemplate()).collect(Collectors.toList()));
705+
List<String> lines = new ArrayList<>();
706+
for (MessageBundleMethodBuildItem m : messages) {
707+
if (m.hasMethod()) {
708+
if (m.hasGeneratedTemplate()) {
709+
// Skip messages with generated templates
710+
continue;
711+
}
712+
// Keys are mapped to method names
713+
lines.add(m.getMethod().name() + "=" + m.getTemplate());
714+
} else {
715+
// No corresponding method declared - use the key instead
716+
// For example, there is no method for generated enum constant message keys
717+
lines.add(m.getKey() + "=" + m.getTemplate());
718+
}
719+
}
720+
Files.write(exampleProperties, lines);
707721
}
708722
}
709723

@@ -992,6 +1006,7 @@ private String generateImplementation(MessageBundleBuildItem bundle, ClassInfo d
9921006
}
9931007
keyMap.put(key, new SimpleMessageMethod(method));
9941008

1009+
boolean generatedTemplate = false;
9951010
String messageTemplate = messageTemplates.get(method.name());
9961011
if (messageTemplate == null) {
9971012
messageTemplate = getMessageAnnotationValue(messageAnnotation);
@@ -1043,6 +1058,7 @@ private String generateImplementation(MessageBundleBuildItem bundle, ClassInfo d
10431058
}
10441059
generatedMessageTemplate.append("{/when}");
10451060
messageTemplate = generatedMessageTemplate.toString();
1061+
generatedTemplate = true;
10461062
}
10471063
}
10481064
}
@@ -1068,7 +1084,7 @@ private String generateImplementation(MessageBundleBuildItem bundle, ClassInfo d
10681084
}
10691085

10701086
MessageBundleMethodBuildItem messageBundleMethod = new MessageBundleMethodBuildItem(bundleName, key, templateId,
1071-
method, messageTemplate, defaultBundleInterface == null);
1087+
method, messageTemplate, defaultBundleInterface == null, generatedTemplate);
10721088
messageTemplateMethods
10731089
.produce(messageBundleMethod);
10741090

@@ -1139,8 +1155,7 @@ private void generateEnumConstantMessageMethod(ClassCreator bundleCreator, Strin
11391155
}
11401156

11411157
MessageBundleMethodBuildItem messageBundleMethod = new MessageBundleMethodBuildItem(bundleName, enumConstantKey,
1142-
templateId, null, messageTemplate,
1143-
defaultBundleInterface == null);
1158+
templateId, null, messageTemplate, defaultBundleInterface == null, true);
11441159
messageTemplateMethods.produce(messageBundleMethod);
11451160

11461161
MethodCreator enumConstantMethod = bundleCreator.getMethodCreator(enumConstantKey,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.quarkus.qute.deployment.i18n;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.FileInputStream;
7+
import java.io.FileNotFoundException;
8+
import java.io.IOException;
9+
import java.nio.file.Path;
10+
import java.util.Properties;
11+
12+
import org.jboss.shrinkwrap.api.asset.StringAsset;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
16+
import io.quarkus.qute.i18n.Message;
17+
import io.quarkus.qute.i18n.MessageBundle;
18+
import io.quarkus.test.ProdBuildResults;
19+
import io.quarkus.test.ProdModeTestResults;
20+
import io.quarkus.test.QuarkusProdModeTest;
21+
22+
public class MessageBundleEnumExampleFileTest {
23+
24+
@RegisterExtension
25+
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
26+
.withApplicationRoot(root -> root
27+
.addClasses(Messages.class, MyEnum.class)
28+
.addAsResource(new StringAsset("""
29+
myEnum_ON=On
30+
myEnum_OFF=Off
31+
myEnum_UNDEFINED=Undefined
32+
"""),
33+
"messages/enu.properties"));
34+
35+
@ProdBuildResults
36+
ProdModeTestResults testResults;
37+
38+
@Test
39+
public void testExampleProperties() throws FileNotFoundException, IOException {
40+
Path path = testResults.getBuildDir().resolve("qute-i18n-examples").resolve("enu.properties");
41+
assertTrue(path.toFile().canRead());
42+
Properties props = new Properties();
43+
props.load(new FileInputStream(path.toFile()));
44+
assertEquals(3, props.size());
45+
assertTrue(props.containsKey("myEnum_ON"));
46+
assertTrue(props.containsKey("myEnum_OFF"));
47+
assertTrue(props.containsKey("myEnum_UNDEFINED"));
48+
}
49+
50+
@MessageBundle(value = "enu", locale = "en")
51+
public interface Messages {
52+
53+
// Replaced with:
54+
// @Message("{#when myEnum}"
55+
// + "{#is ON}{enu:myEnum_ON}"
56+
// + "{#is OFF}{enu:myEnum_OFF}"
57+
// + "{#is UNDEFINED}{enu:myEnum_UNDEFINED}"
58+
// + "{/when}")
59+
@Message
60+
String myEnum(MyEnum myEnum);
61+
62+
}
63+
64+
}

extensions/qute/runtime/src/main/java/io/quarkus/qute/i18n/Message.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
* There is a convenient way to localize enums.
2828
* <p>
2929
* If there is a message bundle method that accepts a single parameter of an enum type and has no message template defined then
30-
* it
31-
* receives a generated template:
30+
* it receives a generated template:
3231
*
3332
* <pre>
3433
* {#when enumParamName}

0 commit comments

Comments
 (0)