Skip to content

Commit

Permalink
Introduce TemplateInstance.setLocale
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Kouba <mkouba@redhat.com>
  • Loading branch information
gastaldi and mkouba committed Apr 15, 2024
1 parent 5871b82 commit 9f66268
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2829,7 +2829,7 @@ public class MyBean {
Template hello;
String render() {
return hello.instance().setAttribute("locale", Locale.forLanguageTag("cs")).render(); <1>
return hello.instance().setLocale("cs").render(); <1>
}
}
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public final class MessageBundles {

public static final String ATTRIBUTE_LOCALE = "locale";
public static final String ATTRIBUTE_LOCALE = TemplateInstance.LOCALE;
public static final String DEFAULT_LOCALE = "<<default>>";

private static final Logger LOGGER = Logger.getLogger(MessageBundles.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.qute;

import java.util.Locale;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -31,6 +32,11 @@ public interface TemplateInstance {
*/
String SELECTED_VARIANT = "selectedVariant";

/**
* Attribute key - locale.
*/
String LOCALE = "locale";

/**
* Set the the root data object. Invocation of this method removes any data set previously by
* {@link #data(String, Object)} and {@link #computedData(String, Function)}.
Expand Down Expand Up @@ -70,7 +76,6 @@ default TemplateInstance computedData(String key, Function<String, Object> funct
}

/**
*
* @param key
* @param value
* @return self
Expand All @@ -80,7 +85,6 @@ default TemplateInstance setAttribute(String key, Object value) {
}

/**
*
* @param key
* @return the attribute or null
*/
Expand Down Expand Up @@ -142,7 +146,6 @@ default CompletionStage<Void> consume(Consumer<String> consumer) {
}

/**
*
* @return the timeout
* @see TemplateInstance#TIMEOUT
*/
Expand All @@ -151,15 +154,13 @@ default long getTimeout() {
}

/**
*
* @return the original template
*/
default Template getTemplate() {
throw new UnsupportedOperationException();
}

/**
*
* @param id
* @return the fragment or {@code null}
* @see Template#getFragment(String)
Expand All @@ -178,6 +179,28 @@ default TemplateInstance onRendered(Runnable action) {
throw new UnsupportedOperationException();
}

/**
* Sets the {@code locale} attribute that can be used to localize parts of the template, i.e. to specify the locale for all
* message bundle expressions in the template.
*
* @param locale a language tag
* @return self
*/
default TemplateInstance setLocale(String locale) {
return setAttribute(LOCALE, Locale.forLanguageTag(locale));
}

/**
* Sets the {@code locale} attribute that can be used to localize parts of the template, i.e. to specify the locale for all
* message bundle expressions in the template.
*
* @param locale a {@link Locale} instance
* @return self
*/
default TemplateInstance setLocale(Locale locale) {
return setAttribute(LOCALE, locale);
}

/**
* This component can be used to initialize a template instance, i.e. the data and attributes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -64,4 +66,16 @@ public void testComputeData() {
assertTrue(fooUsed.get());
assertFalse(barUsed.get());
}

@Test
public void testLocale() throws Exception {
Engine engine = Engine.builder().addDefaults()
.addValueResolver(ValueResolver.builder()
.applyToName("locale")
.resolveSync(ctx -> ctx.getAttribute(TemplateInstance.LOCALE))
.build())
.build();
Template hello = engine.parse("Hello {locale}!");
assertEquals("Hello fr!", hello.instance().setLocale(Locale.FRENCH).render());
}
}

0 comments on commit 9f66268

Please sign in to comment.