Skip to content

Commit

Permalink
Convert LocalesBuildTimeConfig to an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Mar 27, 2024
1 parent b087d1e commit b94e80a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
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,18 @@
import java.util.Locale;
import java.util.Set;

import io.quarkus.runtime.annotations.ConfigItem;
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 +29,8 @@ 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)
Set<Locale> locales();

/**
* Default locale that can be consumed by the extensions.
Expand All @@ -41,6 +42,6 @@ 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)
Locale defaultLocale();
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,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 @@ -1215,7 +1215,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

0 comments on commit b94e80a

Please sign in to comment.