spoke
spoke

Reputation: 267

dialog internationalization in javafx

I fount a problem in my code because it translates some words (in this case a button) according to the language of the os. I've searched for solutions but I didn't find anything to fit my case. As far as I've seen bundles are used to translate strings.

Here is my problem explicitly: enter image description here

My problem is that instead of cancel it writes "Annuler", the french word.

Here is the code for the dialog:

printerSet.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            ChoiceDialog<String> dialog = new ChoiceDialog<>(
                    "Dummy Printer", choices);
            dialog.setTitle("Choice Dialog");
            dialog.setHeaderText(null);
            dialog.setContentText("Choose the printer you want to use:");

            Optional<String> result = dialog.showAndWait();
            if (result.isPresent()) {
                String opt = result.get();
                System.out.println("Your choice: " + opt);
                printerLabel.setText("Selected Printer: " + opt);
            }

            printButton.setDisable(true);
            name.setText("");
            code.setText("");
            description.setText("");
            availability.setText("");
        }
    });

Does anyone know a solution?

Upvotes: 1

Views: 1934

Answers (4)

Wolfgang Fahl
Wolfgang Fahl

Reputation: 15769

For the problem in the question and and issue with Umlauts in the controlsfx wizard

see https://bitbucket.org/controlsfx/controlsfx/issues/769/encoding-problem-all-german-umlauts-are

I am using the following method: after changing the locale I call refreshI18n() on my wizardpanes I use a derived WizardPane for that. refreshI18n() will call fixButtons() and there the button text is set newly according to the set locale.

The main issue is to find the controls and reset the text e.g. for Buttons

/**
* https://bitbucket.org/controlsfx/controlsfx/issues/769/encoding-problem-all-german-umlauts-are
* 
* @param wizardPane
*/
protected void fixButtons() {
  ButtonType buttonTypes[] = { ButtonType.NEXT, ButtonType.PREVIOUS,
    ButtonType.CANCEL, ButtonType.FINISH };
  for (ButtonType buttonType : buttonTypes) {
    Button button = findButton(buttonType);
    if (button != null) {
      button.setText(buttonType.getText());
    }
  }
}

/**
 * get the Button for the given buttonType
 * @return the button
 */
public Button findButton(ButtonType buttonType) {
  for (Node node : getChildren()) {
    if (node instanceof ButtonBar) {
      ButtonBar buttonBar = (ButtonBar) node;
      ObservableList<Node> buttons = buttonBar.getButtons();
      for (Node buttonNode : buttons) {
        Button button = (Button) buttonNode;
        @SuppressWarnings("unchecked")
        ObjectProperty<ButtonData> prop = (ObjectProperty<ButtonData>) button
          .getProperties().get("javafx.scene.control.ButtonBar.ButtonData");
        ButtonData buttonData = prop.getValue();
        if (buttonData.equals(buttonType.getButtonData())) {
          return button;
        }
      }
    }
  }
  return null;
}

Upvotes: 0

Lennart
Lennart

Reputation: 73

This can also be achieved at runtime by using Locale.setDefault(locale) in the main method of the Application class.

For example:

public class App extends Application {

    public static void main(String[] args) {
        Locale.setDefault(Locale.ENGLISH);

        try {
            launch(args);
        } catch (Throwable e) {
            // Handle error
        }
    }

}

Calling Locale.setDefault(locale) again after Application.launch() has been called, had no effect on the dialog button texts.

Upvotes: 1

Jonatan Stenbacka
Jonatan Stenbacka

Reputation: 1864

You could add the buttons manually:

MVCE:

import java.util.Optional;

import javafx.application.Application;

import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class MCVE extends Application {

    @Override
    public void start(Stage stage) {

         ChoiceDialog<String> dialog = new ChoiceDialog<>(
                 "Dummy Printer");
         dialog.setTitle("Choice Dialog");
         dialog.setHeaderText(null);
         dialog.setContentText("Choose the printer you want to use:");

         // Remove the default buttons and then add your custom ones.
         dialog.getDialogPane().getButtonTypes().clear();
         dialog.getDialogPane().getButtonTypes().add(
                new ButtonType("OK", ButtonData.OK_DONE));
         dialog.getDialogPane().getButtonTypes().add(
                new ButtonType("Cancel", ButtonData.CANCEL_CLOSE));

         Optional<String> result = dialog.showAndWait();
         if (result.isPresent()) {
             String opt = result.get();
             System.out.println("Your choice: " + opt);
         }
    }

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

Upvotes: 0

Puce
Puce

Reputation: 38132

Try to provide the following JVM arguments at start-up:

java -Duser.language=en -Duser.country=US ...

Upvotes: 1

Related Questions