2015-09-08 27 views
1

我在我的代碼中發現了一個問題,因爲它根據os的語言翻譯了一些單詞(在本例中爲一個按鈕)。我尋找解決方案,但沒有找到適合我的案例。據我所見,bundles用於翻譯字符串。javafx中的對話國際化

這裏是我的問題明確: enter image description here

我的問題是不是取消它寫道:「更改或取消」,法語單詞。

下面是對話的代碼:

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

有誰知道一個解決方案嗎?

回答

1

儘量提供以下JVM參數在啓動時:

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

你可以手動添加的按鈕:

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

這也可以在運行時實現在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),對對話按鈕文本沒有影響。

0

對於在controlsfx嚮導中的問題,以及問題的問題 變音符號

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

我使用下面的方法: 我wizardpanes改變我打電話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; 
}