JavaFX 8 Localization of dialog's internal ele

2020-03-17 05:00发布

问题:

I am currently developing a JavaFX application with Slovak language localization and inside the application I am using an Alert dialog for showing exceptions with expandable content pane as shown below on images:

I would like to have this dialog completely translated which is going well with Header, Title or Content but I cannot find a way how to translate the Show/Hide details label of the expandable area.

So my question can be a little bit generalized: How to change/translate text of JavaFX internal elements?

Thanks in advance for any help.

PS: For creation of this Alert dialog for exceptions I am using code as found on code.makery.ch

回答1:

For your particular use case, you can add another listener to expandedProperty which will override the texts shown of "details button":

Platform.runLater(() ->
{
    Hyperlink detailsButton = ( Hyperlink ) alert.getDialogPane().lookup( ".details-button" );

    alert.getDialogPane().expandedProperty().addListener(
            ( ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue ) ->
    {
        detailsButton.setText( newValue ? "My less text" : "My more text" );
    });

    // trigger listeners
    alert.getDialogPane().setExpanded( true );
    alert.getDialogPane().setExpanded( false );
});

For more common hack see Localizing JavaFx Controls. From there you need to put the following keys to your custom properties file:

// translate these
Dialog.detail.button.more = Show Details
Dialog.detail.button.less = Hide Details