java - 模块化程序中 LocalDateTime 与 DateTimeFormatter 的翻译 (JDK17)

标签 java translation localdate modularization

我尝试将 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);

所有 3 种情况在 Eclipse 中的结果: Eclipse result

部署后的结果: deploy result

模块信息:

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/

相关文章:

directx - 如何为游戏关卡编辑器实现平移和旋转小部件

math - 结合不同轴心点的四元数

java - 并发数组列表

java - PAX-CDI : CdiContainerFactory and BundleTracker

jquery - 翻译自定义 jQuery 验证消息

javascript - Spring MVC 4 + jackson + LocalDate

java - 获取当前财政年度的开始日期

java - Tomcat 6.x web.xml 默认和自定义 servlet 路由

java - 有没有办法减少这条线

java - 将信用卡日期 MM/yy 格式化为 MM/dd/yyyy