Skip to content

Commit b701076

Browse files
authored
Merge pull request quarkusio#31514 from mkouba/qute-default-bundle-name-strategy-02
Qute type-safe messages - default bundle name strategy #2
2 parents 9207c6b + ad802b2 commit b701076

File tree

7 files changed

+50
-53
lines changed

7 files changed

+50
-53
lines changed

docs/src/main/asciidoc/qute-reference.adoc

+8-6
Original file line numberDiff line numberDiff line change
@@ -2498,24 +2498,26 @@ The message bundles can be used at runtime:
24982498

24992499
The bundle name is defaulted unless it's specified with `@MessageBundle#value()`.
25002500
For a top-level class the `msg` value is used by default.
2501-
For a nested class the name starts with `msg` followed by an underscore, followed by the simple names of all enclosing classes in the hierarchy (top-level class goes first) seperated by underscores.
2501+
For a nested class the name consists of the simple names of all enclosing classes in the hierarchy (top-level class goes first), followed by the simple name of the message bundle interface.
2502+
Names are separated by underscores.
25022503

2503-
For example, the name of the following message bundle will be defaulted to `msg_Index`:
2504+
For example, the name of the following message bundle will be defaulted to `Controller_index`:
25042505

25052506
[source,java]
25062507
----
2507-
class Index {
2508+
class Controller {
25082509
25092510
@MessageBundle
2510-
interface Bundle {
2511+
interface index {
25112512
25122513
@Message("Hello {name}!")
2513-
String hello(String name);
2514+
String hello(String name); <1>
25142515
}
25152516
}
25162517
----
2518+
<1> This message could be used in a template via `{Controller_index:hello(name)}`.
25172519

2518-
NOTE: The bundle name is also used as a part of the name of a localized file, e.g. `msg_Index` in the `msg_Index_de.properties`.
2520+
NOTE: The bundle name is also used as a part of the name of a localized file, e.g. `Controller_index` in the `Controller_index_de.properties`.
25192521

25202522
==== Bundle Name and Message Keys
25212523

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,19 @@ List<MessageBundleBuildItem> processBundles(BeanArchiveIndexBuildItem beanArchiv
144144
} else {
145145
// The name starts with the DEFAULT_NAME followed by an underscore, followed by simple names of all
146146
// declaring classes in the hierarchy seperated by underscores
147-
List<String> enclosingNames = new ArrayList<>();
147+
List<String> names = new ArrayList<>();
148+
names.add(DotNames.simpleName(bundleClass));
148149
DotName enclosingName = bundleClass.enclosingClass();
149150
while (enclosingName != null) {
150151
ClassInfo enclosingClass = index.getClassByName(enclosingName);
151152
if (enclosingClass != null) {
152-
enclosingNames.add(DotNames.simpleName(enclosingClass));
153+
names.add(DotNames.simpleName(enclosingClass));
153154
enclosingName = enclosingClass.nestingType() == NestingType.TOP_LEVEL ? null
154155
: enclosingClass.enclosingClass();
155156
}
156157
}
157-
enclosingNames.add(MessageBundle.DEFAULT_NAME);
158-
// Class Bar declares nested class Foo and bundle Baz is declared as nested interface of Foo
159-
// [Foo, Bar, msg] -> [msg, Bar, Foo]
160-
Collections.reverse(enclosingNames);
161-
name = String.join("_", enclosingNames);
158+
Collections.reverse(names);
159+
name = String.join("_", names);
162160
}
163161
LOG.debugf("Message bundle %s: name defaulted to %s", bundleClass, name);
164162
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.quarkus.qute.deployment.i18n;
2+
3+
import io.quarkus.qute.CheckedTemplate;
4+
import io.quarkus.qute.TemplateInstance;
5+
import io.quarkus.qute.i18n.Message;
6+
import io.quarkus.qute.i18n.MessageBundle;
7+
8+
public class Controller {
9+
10+
@CheckedTemplate
11+
static class Templates {
12+
13+
static native TemplateInstance index(String name);
14+
15+
}
16+
17+
@MessageBundle
18+
public interface index {
19+
20+
@Message("Hello {name}!")
21+
String hello(String name);
22+
}
23+
24+
}

extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleDefaultedNameTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ public class MessageBundleDefaultedNameTest {
1616
@RegisterExtension
1717
static final QuarkusUnitTest config = new QuarkusUnitTest()
1818
.withApplicationRoot((jar) -> jar
19-
.addClasses(Views.class)
19+
.addClasses(Controller.class)
2020
.addAsResource(new StringAsset(
21-
"{msg_Views_Index:hello(name)}"),
22-
"templates/Index/index.html")
23-
.addAsResource(new StringAsset("hello=Ahoj {name}!"), "messages/msg_Views_Index_cs.properties"));
21+
"{Controller_index:hello(name)}"),
22+
"templates/Controller/index.html")
23+
.addAsResource(new StringAsset("hello=Ahoj {name}!"), "messages/Controller_index_cs.properties"));
2424

2525
@Test
2626
public void testBundle() {
2727
assertEquals("Hello world!",
28-
Views.Index.Templates.index("world").render());
29-
assertEquals("Ahoj svete!", Views.Index.Templates.index("svete")
28+
Controller.Templates.index("world").render());
29+
assertEquals("Ahoj svete!", Controller.Templates.index("svete")
3030
.setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.forLanguageTag("cs")).render());
3131
}
3232

extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleLocaleTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class MessageBundleLocaleTest {
2323
.withApplicationRoot((jar) -> jar
2424
.addClasses(Messages.class)
2525
.addAsResource(new StringAsset(
26-
"{msg_MessageBundleLocaleTest:helloWorld}"),
26+
"{MessageBundleLocaleTest_Messages:helloWorld}"),
2727
"templates/foo.html"));
2828

2929
@Inject

extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/Views.java

-27
This file was deleted.

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@
4343
* <p>
4444
* For a top-level class the {@value #DEFAULT_NAME} is used.
4545
* <p>
46-
* For a nested class the name starts with the {@value #DEFAULT_NAME} followed by an undercore, followed by the simple names
47-
* of all enclosing classes in the hierarchy (top-level class goes first) seperated by underscores.
46+
* For a nested class the name consists of the simple names of all enclosing classes in the hierarchy (top-level class goes
47+
* first), followed by the simple name of the message bundle interface. Names are separated by underscores.
4848
*
49-
* For example, the name of the following message bundle will be defaulted to {@code msg_Index} and it could
50-
* be used in a template via <code>{msg_Index:hello(name)}</code>:
49+
* For example, the name of the following message bundle will be defaulted to {@code Controller_index} and it could
50+
* be used in a template via <code>{Controller_index:hello(name)}</code>:
5151
*
5252
* <pre>
5353
* <code>
54-
* class Index {
54+
* class Controller {
5555
*
5656
* &#64;MessageBundle
57-
* interface Bundle {
57+
* interface index {
5858
*
5959
* &#64;Message("Hello {name}!")
6060
* String hello(String name);

0 commit comments

Comments
 (0)