Search code examples
javajavafxjava-11jlinkjpackage

How to fix NumberFormat format even after packaging the application


I'm using NumberFormat to format my decimal numbers to an Italien format (10000 -> 10.000), works as expected, but when I package my application using Jlink badass plugin, it displays all numbers in a US format (10,000) (event tho I choose the Italien format in my code)

to simplify the problem I've made a simple hello world application that illustrates the problem :

Main Class

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Controller

public class HelloController {
    @FXML
    private Label welcomeText;

    @FXML
    protected void onHelloButtonClick() {
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.ITALIAN);
        DecimalFormat formatter = (DecimalFormat) nf;
        formatter.applyPattern("#,###");
        welcomeText.setText(formatter.format(56465465));
    }
}

Fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
      fx:controller="com.example.demo4.HelloController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
    </padding>

    <Label fx:id="welcomeText"/>
    <Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox>

Gradle.Build

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.10'
    id 'org.beryx.jlink' version '2.24.4'
}

group 'com.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    junitVersion = '5.7.1'
}

sourceCompatibility = '17'
targetCompatibility = '17'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'com.example.demo4'
    mainClass = 'com.example.demo4.Runner'
}

javafx {
    version = '17-ea+11'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {

    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}

test {
    useJUnitPlatform()
}


jlink {
    jpackage{
        imageOptions = ["--icon", "C:/demo4/src/main/resources/com/example/demo4/Icon.ico"]
    }
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'Karrty'
    }
}

Results

while compiling and executing the code, label shows : 56.465.465

while running the .exe file (created by Jlink badass plugin), label shows : 56,465,465


Solution

  • jlink has an --include-locales option, which you should include to appropriately localize your jlink image installation:

    See the manual page for jlink:

    Options

    --include-locales=langtag[,langtag]* 
    

    Description

    Includes the list of locales where langtag is a BCP 47 language tag. This option supports locale matching as defined in RFC 4647. Ensure that you add the module jdk.localedata when using this option.

    Example

    --add-modules jdk.localedata --include-locales=en,ja,*-IN
    

    As noted by Youssef Idraiss in comments, if your application has a module-info.java file, instead of adding the jdk.localedata module as a command line option, you can require the module in your module-info.java file.

    For use within the badass gradle plugin, you can pass the appropriate options to the plugin, e.g.

    jlink {
        jpackage{
            imageOptions = ["--icon", "C:/demo4/src/main/resources/com/example/demo4/Icon.ico"]
        }
        options = ['--include-locales=en,ja,*-IN', '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
        launcher {
            name = 'Karrty'
        }
    }