io.quarkus.qute.i18n.MessageBundles Maven / Gradle / Ivy
package io.quarkus.qute.i18n;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CompletionStage;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Instance;
import org.jboss.logging.Logger;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.qute.Engine;
import io.quarkus.qute.EngineBuilder;
import io.quarkus.qute.EvalContext;
import io.quarkus.qute.NamespaceResolver;
import io.quarkus.qute.Qute;
import io.quarkus.qute.Resolver;
import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.qute.Variant;
import io.quarkus.qute.runtime.MessageBundleRecorder.BundleContext;
public final class MessageBundles {
public static final String ATTRIBUTE_LOCALE = "locale";
public static final String DEFAULT_LOCALE = "<>";
private static final Logger LOGGER = Logger.getLogger(MessageBundles.class);
private MessageBundles() {
}
public static T get(Class bundleInterface) {
return get(bundleInterface, null);
}
public static T get(Class bundleInterface, Localized localized) {
if (!bundleInterface.isInterface()) {
throw new IllegalArgumentException("Not a message bundle interface: " + bundleInterface.getName());
}
if (!bundleInterface.isAnnotationPresent(MessageBundle.class)
&& !bundleInterface.isAnnotationPresent(Localized.class)) {
throw new IllegalArgumentException(
"Message bundle interface must be annotated either with @MessageBundle or with @Localized: "
+ bundleInterface.getName());
}
InstanceHandle handle = localized != null ? Arc.container().instance(bundleInterface, localized)
: Arc.container().instance(bundleInterface);
if (handle.isAvailable()) {
return handle.get();
}
throw new IllegalStateException(Qute.fmt(
"Unable to obtain a message bundle for interface [{iface.name}]{#if loc} and locale [{loc.value}]{/if}")
.data("iface", bundleInterface)
.data("loc", localized)
.render());
}
static void setupNamespaceResolvers(@Observes EngineBuilder builder, BundleContext context) {
// Avoid injecting "Instance
© 2015 - 2025 Weber Informatics LLC | Privacy Policy