Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert LocalesBuildTimeConfig to an interface #39748

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,22 @@ private Set<String> getRegisteredRoots(ConfigPhase configPhase) {
registeredRoots.add(root.getName());
}
}

if (BUILD_AND_RUN_TIME_FIXED.equals(configPhase)) {
for (ConfigClassWithPrefix mapping : buildTimeConfigResult.getBuildTimeRunTimeMappings()) {
registeredRoots.add(mapping.getPrefix());
}
}

if (RUN_TIME.equals(configPhase)) {
for (ConfigClassWithPrefix mapping : buildTimeConfigResult.getBuildTimeRunTimeMappings()) {
registeredRoots.add(mapping.getPrefix());
}
for (ConfigClassWithPrefix mapping : buildTimeConfigResult.getRunTimeMappings()) {
registeredRoots.add(mapping.getPrefix());
}
}

return registeredRoots;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public boolean getAsBoolean() {
(nativeConfig.userCountry().isPresent()
&& !Locale.getDefault().getCountry().equals(nativeConfig.userCountry().get()))
||
!Locale.getDefault().equals(localesBuildTimeConfig.defaultLocale)
!Locale.getDefault().equals(localesBuildTimeConfig.defaultLocale())
||
localesBuildTimeConfig.locales.stream().anyMatch(l -> !Locale.getDefault().equals(l));
localesBuildTimeConfig.locales().stream().anyMatch(l -> !Locale.getDefault().equals(l));
}
}

Expand All @@ -95,7 +95,7 @@ public boolean getAsBoolean() {
* effectively LocalesBuildTimeConfig.DEFAULT_LANGUAGE if none of the aforementioned is set.
*/
public static String nativeImageUserLanguage(NativeConfig nativeConfig, LocalesBuildTimeConfig localesBuildTimeConfig) {
String language = localesBuildTimeConfig.defaultLocale.getLanguage();
String language = localesBuildTimeConfig.defaultLocale().getLanguage();
if (nativeConfig.userLanguage().isPresent()) {
log.warn(DEPRECATED_USER_LANGUAGE_WARNING);
// The deprecated option takes precedence for users who are already using it.
Expand All @@ -114,7 +114,7 @@ public static String nativeImageUserLanguage(NativeConfig nativeConfig, LocalesB
* set.
*/
public static String nativeImageUserCountry(NativeConfig nativeConfig, LocalesBuildTimeConfig localesBuildTimeConfig) {
String country = localesBuildTimeConfig.defaultLocale.getCountry();
String country = localesBuildTimeConfig.defaultLocale().getCountry();
if (nativeConfig.userCountry().isPresent()) {
log.warn(DEPRECATED_USER_COUNTRY_WARNING);
// The deprecated option takes precedence for users who are already using it.
Expand All @@ -133,15 +133,15 @@ public static String nativeImageUserCountry(NativeConfig nativeConfig, LocalesBu
*/
public static String nativeImageIncludeLocales(NativeConfig nativeConfig, LocalesBuildTimeConfig localesBuildTimeConfig) {
// We start with what user sets as needed locales
final Set<Locale> additionalLocales = new HashSet<>(localesBuildTimeConfig.locales);
final Set<Locale> additionalLocales = new HashSet<>(localesBuildTimeConfig.locales());

if (additionalLocales.contains(Locale.ROOT)) {
return "all";
}

// We subtract what we already declare for native-image's user.language or user.country.
// Note the deprecated options still count.
additionalLocales.remove(localesBuildTimeConfig.defaultLocale);
additionalLocales.remove(localesBuildTimeConfig.defaultLocale());
if (nativeConfig.userCountry().isPresent() && nativeConfig.userLanguage().isPresent()) {
additionalLocales.remove(new Locale(nativeConfig.userLanguage().get(), nativeConfig.userCountry().get()));
} else if (nativeConfig.userLanguage().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import java.util.Locale;
import java.util.Set;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigDocDefault;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(name = ConfigItem.PARENT, phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class LocalesBuildTimeConfig {
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
@ConfigMapping(prefix = "quarkus")
public interface LocalesBuildTimeConfig {

// We set to en as the default language when all else fails since this is what the JDK does as well
public static final String DEFAULT_LANGUAGE = "${user.language:en}";
public static final String DEFAULT_COUNTRY = "${user.country:}";
String DEFAULT_LANGUAGE = "${user.language:en}";
String DEFAULT_COUNTRY = "${user.country:}";

/**
* The set of supported locales that can be consumed by the extensions.
Expand All @@ -27,9 +30,9 @@ public class LocalesBuildTimeConfig {
* A special string "all" is translated as ROOT Locale and then used in native-image
* to include all locales. Image size penalty applies.
*/
@ConfigItem(defaultValue = DEFAULT_LANGUAGE + "-"
+ DEFAULT_COUNTRY, defaultValueDocumentation = "Set containing the build system locale")
public Set<Locale> locales;
@WithDefault(DEFAULT_LANGUAGE + "-" + DEFAULT_COUNTRY)
@ConfigDocDefault("Set containing the build system locale")
Set<Locale> locales();

/**
* Default locale that can be consumed by the extensions.
Expand All @@ -41,6 +44,7 @@ public class LocalesBuildTimeConfig {
* Native-image build uses this property to derive {@code user.language} and {@code user.country} for the application's
* runtime.
*/
@ConfigItem(defaultValue = DEFAULT_LANGUAGE + "-" + DEFAULT_COUNTRY, defaultValueDocumentation = "Build system locale")
public Locale defaultLocale;
@WithDefault(DEFAULT_LANGUAGE + "-" + DEFAULT_COUNTRY)
@ConfigDocDefault("Build system locale")
Locale defaultLocale();
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public void created(BeanContainer container) {
configuration.builtinConstraints(detectedBuiltinConstraints)
.initializeBeanMetaData(classesToBeValidated)
// Locales, Locale ROOT means all locales in this setting.
.locales(localesBuildTimeConfig.locales.contains(Locale.ROOT) ? Set.of(Locale.getAvailableLocales())
: localesBuildTimeConfig.locales)
.defaultLocale(localesBuildTimeConfig.defaultLocale)
.locales(localesBuildTimeConfig.locales().contains(Locale.ROOT) ? Set.of(Locale.getAvailableLocales())
: localesBuildTimeConfig.locales())
.defaultLocale(localesBuildTimeConfig.defaultLocale())
.beanMetaDataClassNormalizer(new ArcProxyBeanMetaDataClassNormalizer());

if (hibernateValidatorBuildTimeConfig.expressionLanguage().constraintExpressionFeatureLevel().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ private String getDefaultLocale(AnnotationInstance bundleAnnotation, LocalesBuil
AnnotationValue localeValue = bundleAnnotation.value(BUNDLE_LOCALE);
String defaultLocale;
if (localeValue == null || localeValue.asString().equals(MessageBundle.DEFAULT_LOCALE)) {
defaultLocale = locales.defaultLocale.toLanguageTag();
defaultLocale = locales.defaultLocale().toLanguageTag();
} else {
defaultLocale = localeValue.asString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public EngineProducer(QuteContext context, QuteConfig config, QuteRuntimeConfig
this.templateRoots = context.getTemplateRoots();
this.tags = context.getTags();
this.templatePathExclude = config.templatePathExclude;
this.defaultLocale = locales.defaultLocale;
this.defaultLocale = locales.defaultLocale();
this.defaultCharset = config.defaultCharset;
this.container = Arc.container();

Expand Down
Loading