Search code examples
javatranslationlocaldatemodularization

Translation of LocalDateTime with DateTimeFormatter in modularized program (JDK17)


I try to translate a LocalDateTime to the current used language like shown in this answer: https://stackoverflow.com/a/51843932

Running the program in Eclipse works as intended, but when it gets deployed with maven and jlink it falls back to english.

I assume I am missing a module to be required? Its a JDK17 modularized application.

I tried:

Locale loc = new Locale("de", "DE");
Locale.setDefault(loc);
lblDateTime.setText(LocalDateTime.now()
    .format(DateTimeFormatter.ofPattern("EEEE, dd. MMMM yyyy - HH:mm:ss");
lblDateTime.setText(LocalDateTime.now()
    .format(DateTimeFormatter.ofPattern("EEEE, dd. MMMM yyyy - HH:mm:ss", Locale.GERMAN);
lblDateTime.setText(LocalDateTime.now()
    .format(DateTimeFormatter.ofPattern("EEEE, dd. MMMM yyyy - HH:mm:ss").localizedBy(Locale.GERMAN);

Result in Eclipse in all 3 cases: Eclipse result

Result after deployment: deploy result

The module info:

module [name] {
    [...]

    requires transitive javafx.controls;
    requires javafx.fxml;
    requires transitive javafx.graphics;
    requires org.apache.logging.log4j.core;
    requires javafx.base;
    requires org.controlsfx.controls;
    requires java.base;
    requires java.desktop;
}

Update 1:
The result of System.getProperty("java.locale.providers") in both cases is null.

Update 2:
Adding requires jdk.localedata; to the module-info.java solved the issue. See accepted answer.


Solution

  • When you build an optimized environment but want support for localization, you must include jdk.localedata in your environment.

    Provides the locale data for locales other than US locale.

    So either, specify --add-modules jdk.localedata to jlink when creating the runtime image or add requires jdk.localedata; to your module-info.java.