Qute and (localized) values of a *.properties file

85 views
Skip to first unread message

Torsten Reinhard

unread,
Jul 28, 2022, 5:25:01 AM7/28/22
to Quarkus Development mailing list
Hi,

in our backend project we have a Label.properties as well as Label_de_DE.properties (and more for other languages).
I used https://github.com/kklisura/props-to-constants-generator to generate  (type-safe) constants for each key of my *.properties file:

public final class LabelsConstants {
   public static final String VEHICLEDATA_MODEL = "vehicleData.model"; 
   public static final String REPORTTITLES_KEYDATA = "reportTitles.keyData";
   ...
}

How can I access the localized values now in my (type-safed) Qute templates?

Here´s my Template:

@CheckedTemplate(basePath = "")
final class HtmlTemplates {
...
public static native TemplateInstance keyData(
MyItem item
)

And my template looks like:
{@foo.bar.MyItem}
{#title}{item.get(LabelConstants. REPORTTITLES_KEYDATA) }{/title}

My idea is to pass a ResourceBundle and than use rb.get( LabelConstants.MY_CONSTANT) somehow in the template.

Can you point me in the right direction how to achieve this ?
What´s the recommended way to use checked Templates with values from a *.properties File ? I dont want to use String identifiers in my templates like "foo.bar" that will probably not resolve. Instead, the Constants class should be used.

Thanx, Toschder


Martin Kouba

unread,
Aug 1, 2022, 4:30:15 AM8/1/22
to tosc...@gmail.com, Quarkus Development mailing list
Hi,

added some comments inline...

On 28. 07. 22 11:25, Torsten Reinhard wrote:
> Hi,
>
> in our backend project we have a /Label.properties/ as well as
> /Label_de_DE.properties/ (and more for other languages).
> I used https://github.com/kklisura/props-to-constants-generator
> <https://github.com/kklisura/props-to-constants-generator> to generate
> (type-safe) constants for each key of my *.properties file:
>
> public final class LabelsConstants {
>    public static final String VEHICLEDATA_MODEL = "vehicleData.model";
>    public static final String REPORTTITLES_KEYDATA =
> "reportTitles.keyData";
>    ...
> }
>
> How can I access the localized values now in my (type-safed) Qute templates?
>
> Here´s my Template:
>
> @CheckedTemplate(basePath = "")
> final class HtmlTemplates {
> ...
> public static native TemplateInstance keyData(
> MyItem item
> )
>
> And my template looks like:
> {@foo.bar.MyItem}
> {#title}{item.get(LabelConstants. REPORTTITLES_KEYDATA) }{/title}
>
> My idea is to pass a ResourceBundle and than use rb.get(
> LabelConstants.MY_CONSTANT) somehow in the template.

This should work. But you'll need to make the constants available in the
templates. One of the possible solutions is to annotate the
LabelsConstants class with @TemplateData [1] and then use something like
{rb.get(LabelsConstants:MY_CONSTANT)}.

[1]
https://quarkus.io/guides/qute-reference#accessing-static-fields-and-methods

> Can you point me in the right direction how to achieve this ?
> What´s the recommended way to use checked Templates with values from a
> *.properties File ? I dont want to use String identifiers in my
> templates like "foo.bar" that will probably not resolve. Instead, the
> Constants class should be used.

ResourceBundle is ok. You can also try to use the Type-safe Message
Bundles [2] but here you'd need to create the type-safe message
interface first (instead of the constants).

[2]
https://quarkus.io/guides/qute-reference#type-safe-message-bundles

>
> Thanx, Toschder
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Quarkus Development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/quarkus-dev/b04fe161-a036-4d8d-8ef6-3b2bd19e99den%40googlegroups.com
> <https://groups.google.com/d/msgid/quarkus-dev/b04fe161-a036-4d8d-8ef6-3b2bd19e99den%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Martin Kouba
Software Engineer
Red Hat, Czech Republic

Reply all
Reply to author
Forward
0 new messages