Skip to content

Commit

Permalink
Qute: type-safe messages - add test for localized enum
Browse files Browse the repository at this point in the history
- related to quarkusio#40089
- also use the convenient TemplateInstance#setLocale() method
  • Loading branch information
mkouba committed Apr 16, 2024
1 parent 0b84af7 commit d56a625
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,13 @@ private Map<String, String> parseKeyToTemplateFromLocalizedFile(ClassInfo bundle
Map<String, String> keyToTemplate = new HashMap<>();
for (ListIterator<String> it = Files.readAllLines(localizedFile).listIterator(); it.hasNext();) {
String line = it.next();
if (line.startsWith("#") || line.isBlank()) {
// Comments and blank lines are skipped
if (line.isBlank()) {
// Blank lines are skipped
continue;
}
line = line.strip();
if (line.startsWith("#")) {
// Comments are skipped
continue;
}
int eqIdx = line.indexOf('=');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Locale;

import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.asset.StringAsset;
Expand All @@ -13,7 +11,6 @@
import io.quarkus.qute.Engine;
import io.quarkus.qute.i18n.Message;
import io.quarkus.qute.i18n.MessageBundle;
import io.quarkus.qute.i18n.MessageBundles;
import io.quarkus.test.QuarkusUnitTest;

public class MessageBundleDefaultedNameTest {
Expand Down Expand Up @@ -43,8 +40,7 @@ public class MessageBundleDefaultedNameTest {
public void testBundles() {
assertEquals("Hello world!",
Controller.Templates.index("world").render());
assertEquals("Ahoj svete!", Controller.Templates.index("svete")
.setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.forLanguageTag("cs")).render());
assertEquals("Ahoj svete!", Controller.Templates.index("svete").setLocale("cs").render());

assertEquals("Hello world!", engine.getTemplate("app").render());
assertEquals("Hello alpha!", engine.getTemplate("alpha").render());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Locale;

import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.asset.StringAsset;
Expand All @@ -13,7 +11,6 @@
import io.quarkus.qute.Template;
import io.quarkus.qute.i18n.Message;
import io.quarkus.qute.i18n.MessageBundle;
import io.quarkus.qute.i18n.MessageBundles;
import io.quarkus.test.QuarkusUnitTest;

public class MessageBundleLocaleTest {
Expand All @@ -31,8 +28,7 @@ public class MessageBundleLocaleTest {

@Test
public void testResolvers() {
assertEquals("Ahoj svete!",
foo.instance().setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.forLanguageTag("cs")).render());
assertEquals("Ahoj svete!", foo.instance().setLocale("cs").render());
}

@MessageBundle(locale = "cs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
import static io.quarkus.qute.i18n.MessageBundle.DEFAULT_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Locale;

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.TemplateEnum;
import io.quarkus.qute.i18n.Message;
import io.quarkus.qute.i18n.MessageBundle;
import io.quarkus.qute.i18n.MessageBundles;
import io.quarkus.test.QuarkusUnitTest;

public class MessageBundleLogicalLineTest {
Expand All @@ -25,18 +23,18 @@ public class MessageBundleLogicalLineTest {
.addClasses(Messages.class)
.addAsResource("messages/msg_cs.properties")
.addAsResource(new StringAsset(
"{msg:hello('Edgar')} {msg:helloNextLine('Edgar')} ::{msg:fruits}"),
"{msg:hello('Edgar')}::{msg:helloNextLine('Edgar')}::{msg:fruits}::{msg:myEnum(MyEnum:OFF)}"),
"templates/foo.html"));

@Inject
Template foo;

@Test
public void testResolvers() {
assertEquals("Hello Edgar! Hello \n Edgar! ::apple, banana, pear, watermelon, kiwi, mango",
assertEquals("Hello Edgar!::Hello \n Edgar!::apple, banana, pear, watermelon, kiwi, mango::Off",
foo.render());
assertEquals("Ahoj Edgar a dobrý den! Ahoj \n Edgar! ::apple, banana, pear, watermelon, kiwi, mango",
foo.instance().setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.forLanguageTag("cs")).render());
assertEquals("Ahoj Edgar a dobrý den!::Ahoj \n Edgar!::jablko, banan, hruska, meloun, kiwi, mango::Vypnuto",
foo.instance().setLocale("cs").render());
}

@MessageBundle(value = DEFAULT_NAME, locale = "en")
Expand All @@ -50,6 +48,21 @@ public interface Messages {

@Message("apple, banana, pear, watermelon, kiwi, mango")
String fruits();

@Message("{#when myEnum}"
+ "{#is ON}On"
+ "{#is OFF}Off"
+ "{#else}Undefined"
+ "{/when}")
String myEnum(MyEnum myEnum);

}

@TemplateEnum
public enum MyEnum {
ON,
OFF,
UNDEFINED
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testResolvers() {
assertEquals("Hello world! Ahoj Jachym! Hello you guys! Hello alpha! Hello! Hello foo from alpha!",
foo.instance().setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.forLanguageTag("cs")).render());
assertEquals("Hallo Welt! Hallo Jachym! Hello you guys! Hello alpha! Hello! Hello foo from alpha!",
foo.instance().setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.GERMAN).render());
foo.instance().setLocale(Locale.GERMAN).render());
assertEquals("Dot test!", engine.parse("{msg:['dot.test']}").render());
assertEquals("Hello world! Hello Malachi Constant!",
engine.getTemplate("dynamic").data("key", "hello_fullname").data("surname", "Constant").render());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ hello=Ahoj \
dobrý den!

helloNextLine=Ahoj \n {name}!
fruits = apple, banana, pear, \
watermelon, \
fruits = jablko, banan, hruska, \
meloun, \
kiwi, mango

# This is an example how to localize an enum value
myEnum={#when myEnum}\
{#is ON}Zapnuto\
{#is OFF}Vypnuto\
{#else}Nedefinovano\
{/when}

0 comments on commit d56a625

Please sign in to comment.