Skip to content

Commit

Permalink
[GR-33683] Update the process of setting the default locale.
Browse files Browse the repository at this point in the history
PullRequest: graal/10270
  • Loading branch information
d-kozak committed Nov 29, 2021
2 parents 3984a84 + b9373e3 commit 2c1c5d4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/reference-manual/native-image/Resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -154,7 +152,8 @@ public static class Options {
@Option(help = "Make all hosted charsets available at run time")//
public static final HostedOptionKey<Boolean> 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<String> 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)//
Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit 2c1c5d4

Please sign in to comment.