2

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.

  • That’s weird. It would seem that your installation is missing locale data or is trying to pick them up from a wrong source. In particular I wonder that you specify the full form of the day of week ( `EEEE`), but on the installation the abbreviated form is printed ( `Wed`, not even `Wednesday`). – Ole V.V. May 18 '22 at 09:05
  • 1
    To investigate where Java is trying to get its locale data from you may print out `System.getProperty("java.locale.providers")` and tell us the result from both your own Eclipse and from the deployed program (you may get null or an empty string). – Ole V.V. May 18 '22 at 09:09
  • @user16320675 - Thanks for pointing it out. I corrected it. – A. Alexander May 18 '22 at 11:33
  • 1
    @OleV.V. I updated the Question with the results. – A. Alexander May 18 '22 at 11:33
  • Thanks. `null` should imply that the default should be used, which in Java 17 is `CLDR,COMPAT`. If the CLDR (Unicode Common Locale Data Repository) data would be missing from or incomplete in your installation, I find that weird and a bit hard to believe. Does the same happen for other locales? – Ole V.V. May 18 '22 at 11:47

1 Answers1

3

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.

Holger
  • 285,553
  • 42
  • 434
  • 765