【问题标题】:dialog internationalization in javafxjavafx中的对话框国际化
【发布时间】:2023-04-10 05:43:02
【问题描述】:

我在我的代码中发现了一个问题,因为它根据操作系统的语言翻译了一些单词(在本例中为按钮)。我已经寻找解决方案,但没有找到适合我的情况。据我所见,捆绑包用于翻译字符串。

这是我的明确问题:

我的问题是,它写的不是取消,而是法语单词“Annuler”。

这是对话框的代码:

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("");
        }
    });

有人知道解决办法吗?

【问题讨论】:

    标签: java javafx internationalization translation


    【解决方案1】:

    尝试在启动时提供以下 JVM 参数:

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

    【讨论】:

      【解决方案2】:

      这也可以在运行时通过在Application 类的main 方法中使用Locale.setDefault(locale) 来实现。

      例如:

      public class App extends Application {
      
          public static void main(String[] args) {
              Locale.setDefault(Locale.ENGLISH);
      
              try {
                  launch(args);
              } catch (Throwable e) {
                  // Handle error
              }
          }
      
      }
      

      在调用Application.launch() 后再次调用Locale.setDefault(locale),对对话框按钮文本没有影响。

      【讨论】:

        【解决方案3】:

        您可以手动添加按钮:

        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();
            }
        }
        

        【讨论】:

          【解决方案4】:

          对于问题中的问题和问题 controlsfx 向导中的元音变音

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

          我正在使用以下方法: 更改语言环境后,我在向导窗格上调用 refreshI18n(),为此我使用派生的 WizardPane。 refreshI18n() 将调用 fixButtons() 并根据设置的语言环境重新设置按钮文本。

          主要问题是找到控件并重置文本,例如对于按钮

          /**
          * 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;
          }
          

          【讨论】:

            猜你喜欢
            • 2012-04-25
            • 1970-01-01
            • 2023-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-11
            • 1970-01-01
            • 2015-10-13
            相关资源
            最近更新 更多