From b9373e3b825850eff8bfa0c219a7b64a6a93f0a6 Mon Sep 17 00:00:00 2001 From: David Kozak Date: Tue, 9 Nov 2021 12:32:05 +0100 Subject: [PATCH] update the process of setting the default locale --- docs/reference-manual/native-image/Resources.md | 4 ++-- .../jdk/localization/LocalizationFeature.java | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/reference-manual/native-image/Resources.md b/docs/reference-manual/native-image/Resources.md index e1aa1e5d8b72..6c2dfce82f56 100644 --- a/docs/reference-manual/native-image/Resources.md +++ b/docs/reference-manual/native-image/Resources.md @@ -63,9 +63,9 @@ See also the [guide on assisted configuration of Java resources and other dynami ## Locales It is also possible to specify which locales should be included in the image and what should be the default one. -For example, to switch the default locale to German and also include French and English, one can use the following hosted options. +For example, to switch the default locale to Swiss German and also include French and English, one can use the following hosted options. ```shell -native-image -H:DefaultLocale=de -H:IncludeLocales=fr,en +native-image -Duser.country=CH -Duser.language=de -H:IncludeLocales=fr,en ``` The locales are specified using [language tags](https://docs.oracle.com/javase/tutorial/i18n/locale/matching.html). All locales can be included via ``-H:+IncludeAllLocales``, but please note that it increases the size of the resulting diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/localization/LocalizationFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/localization/LocalizationFeature.java index 3523761059cf..80587c48cdda 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/localization/LocalizationFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/localization/LocalizationFeature.java @@ -133,9 +133,7 @@ public abstract class LocalizationFeature implements Feature { private final ForkJoinPool compressionPool = Options.LocalizationCompressInParallel.getValue() ? new ForkJoinPool(NativeImageOptions.NumberOfThreads.getValue()) : null; /** - * The Locale that the native image is built for. Currently, switching the Locale at run time is - * not supported because the resource bundles are only included for one Locale. We use the - * Locale that is set for the image generator. + * The Locale that the native image is built for. */ protected Locale defaultLocale = Locale.getDefault(); @@ -154,7 +152,8 @@ public static class Options { @Option(help = "Make all hosted charsets available at run time")// public static final HostedOptionKey AddAllCharsets = new HostedOptionKey<>(false); - @Option(help = "Default locale of the image, by the default it is the same as the default locale of the image builder.", type = OptionType.User)// + @Option(help = "Default locale of the image, by the default it is the same as the default locale of the image builder.", type = OptionType.User, // + deprecated = true, deprecationMessage = "Please switch to using system properties such as -Duser.country=CH -Duser.language=de")// public static final HostedOptionKey DefaultLocale = new HostedOptionKey<>(Locale.getDefault().toLanguageTag()); @Option(help = "Default charset of the image, by the default it is the same as the default charset of the image builder.", type = OptionType.User)// @@ -236,8 +235,10 @@ private static ResolvedJavaField findStaticField(ResolvedJavaType declaringClass public void afterRegistration(AfterRegistrationAccess access) { findClassByName = access::findClassByName; allLocales = processLocalesOption(); - defaultLocale = LocalizationSupport.parseLocaleFromTag(Options.DefaultLocale.getValue()); - UserError.guarantee(defaultLocale != null, "Invalid default locale %s", Options.DefaultLocale.getValue()); + if (Options.DefaultLocale.hasBeenSet()) { + defaultLocale = LocalizationSupport.parseLocaleFromTag(Options.DefaultLocale.getValue()); + UserError.guarantee(defaultLocale != null, "Invalid default locale %s", Options.DefaultLocale.getValue()); + } try { defaultCharset = Charset.forName(Options.DefaultCharset.getValue()); VMError.guarantee(defaultCharset.name().equals(Options.DefaultCharset.getValue()),