我尝试将 LocalDateTime 翻译为当前使用的语言,如以下答案所示:https://stackoverflow.com/a/51843932
在 Eclipse 中运行程序按预期工作,但是当使用 maven 和 jlink 部署它时,它会回退为英语。
我假设我缺少所需的模块?它是一个JDK17模块化应用程序。
我尝试过:
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);
模块信息:
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;
}
更新1:
两种情况下 System.getProperty("java.locale.providers")
的结果都是 null
。
更新2:
将 requires jdk.localedata;
添加到 module-info.java
解决了该问题。查看已接受的答案。
最佳答案
当您构建优化的环境但希望支持本地化时,必须包含 jdk.localedata
在您的环境中。
Provides the locale data for locales other than US locale.
因此,在创建运行时镜像时将 --add-modules jdk.localedata
指定为 jlink
或添加 requires jdk.localedata;
到您的module-info.java
。
关于java - 模块化程序中 LocalDateTime 与 DateTimeFormatter 的翻译 (JDK17),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72285598/