Skip to content

Commit

Permalink
Adapt locales support for GraalVM >= 24.2
Browse files Browse the repository at this point in the history
Starting with GraalVM for JDK 24 (24.2) native image will no longer set
the locale default at build time. As a result, the default locale won't
be included by default in the native image unless explicitly specified.

See oracle/graal#9694
  • Loading branch information
zakkak committed Sep 24, 2024
1 parent ffd0c78 commit 6b89856
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public interface NativeConfig {

/**
* Defines the user language used for building the native executable.
* It also serves as the default Locale language for the native executable application runtime.
* With GraalVM versiond prior to GraalVM for JDK 24 it also serves as the default Locale language for the native executable
* application runtime.
* e.g. en or cs as defined by IETF BCP 47 language tags.
* <p>
*
Expand All @@ -100,7 +101,8 @@ public interface NativeConfig {

/**
* Defines the user country used for building the native executable.
* It also serves as the default Locale country for the native executable application runtime.
* With GraalVM versiond prior to GraalVM for JDK 24 it also serves as the default Locale country for the native executable
* application runtime.
* e.g. US or FR as defined by ISO 3166-1 alpha-2 codes.
* <p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ public static final class Version implements Comparable<Version> {
public static final Version VERSION_23_0_0 = new Version("GraalVM 23.0.0", "23.0.0", "17", Distribution.GRAALVM);
public static final Version VERSION_23_1_0 = new Version("GraalVM 23.1.0", "23.1.0", "21", Distribution.GRAALVM);
public static final Version VERSION_24_0_0 = new Version("GraalVM 24.0.0", "24.0.0", "22", Distribution.GRAALVM);
public static final Version VERSION_24_1_0 = new Version("GraalVM 24.1.0", "24.1.0", "23", Distribution.GRAALVM);
public static final Version VERSION_24_2_0 = new Version("GraalVM 24.2.0", "24.2.0", "24", Distribution.GRAALVM);

/**
* The minimum version of GraalVM supported by Quarkus.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ public static String nativeImageIncludeLocales(NativeConfig nativeConfig, Locale
return "all";
}

// We subtract what we already declare for native-image's user.language or user.country.
// Ensure the default locale is included to ensure compatibility with GraalVM for JDK 24, which doesn't set the locale at build-time.
additionalLocales.add(localesBuildTimeConfig.defaultLocale);
// Note the deprecated options still count.
additionalLocales.remove(localesBuildTimeConfig.defaultLocale);
if (nativeConfig.userCountry().isPresent() && nativeConfig.userLanguage().isPresent()) {
additionalLocales.remove(new Locale(nativeConfig.userLanguage().get(), nativeConfig.userCountry().get()));
additionalLocales.add(new Locale(nativeConfig.userLanguage().get(), nativeConfig.userCountry().get()));
} else if (nativeConfig.userLanguage().isPresent()) {
additionalLocales.remove(new Locale(nativeConfig.userLanguage().get()));
additionalLocales.add(new Locale(nativeConfig.userLanguage().get()));
}

return additionalLocales.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class LocalesBuildTimeConfig {
* For instance, the Hibernate Validator extension makes use of it.
* <p>
* Native-image build uses this property to derive {@code user.language} and {@code user.country} for the application's
* runtime.
* runtime. Starting with GraalVM for JDK 24 this option will not result in setting the default runtime locale, and
* any usages of it should be re-evaluated.
*/
@ConfigItem(defaultValue = DEFAULT_LANGUAGE + "-" + DEFAULT_COUNTRY, defaultValueDocumentation = "Build system locale")
public Locale defaultLocale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import io.quarkus.test.junit.DisableIfBuiltWithGraalVMNewerThan;
import io.quarkus.test.junit.DisableIfBuiltWithGraalVMOlderThan;
import io.quarkus.test.junit.GraalVMVersion;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.RestAssured;

Expand Down Expand Up @@ -81,7 +84,8 @@ public void testTimeZones(String zone, String language, String name) {
}

@Test
public void testDefaultLocale() {
@DisableIfBuiltWithGraalVMNewerThan(value = GraalVMVersion.GRAALVM_24_1_0)
public void testDefaultLocaleBefore24_2() {
RestAssured.given().when()
.get("/default/de-CH")
.then()
Expand All @@ -94,6 +98,21 @@ public void testDefaultLocale() {
.log().all();
}

@Test
@DisableIfBuiltWithGraalVMOlderThan(value = GraalVMVersion.GRAALVM_24_2_0)
public void testDefaultLocaleAfter24_1() {
RestAssured.given().when()
.get("/default/de-CH")
.then()
.statusCode(HttpStatus.SC_OK)
/*
* "Schweiz" is the correct name for Switzerland in German.
* German is the default language as per the application.properties.
*/
.body(is("Schweiz"))
.log().all();
}

@Test
public void testMissingLocaleSorryItaly() {
RestAssured.given().when()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ quarkus.locales=de,fr-FR,ja,uk-UA
# used in your application properties. This test uses it only to verify compatibility.
quarkus.native.user-language=cs
quarkus.default-locale=en-US
quarkus.test.arg-line=-Duser.language=de
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

public enum GraalVMVersion {
GRAALVM_23_1_0(GraalVM.Version.VERSION_23_1_0),
GRAALVM_24_0_0(GraalVM.Version.VERSION_24_0_0);
GRAALVM_24_0_0(GraalVM.Version.VERSION_24_0_0),
GRAALVM_24_1_0(GraalVM.Version.VERSION_24_1_0),
GRAALVM_24_2_0(GraalVM.Version.VERSION_24_2_0);

private final GraalVM.Version version;

Expand Down

0 comments on commit 6b89856

Please sign in to comment.