代码之家  ›  专栏  ›  技术社区  ›  parsecer

FXML文件中的FXML翻译功能

  •  0
  • parsecer  · 技术社区  · 3 年前

    我在fxml中有这个对象(以及其他对象):

      <Label text="Birthday">
            <font>
              <Font size="16.0" />
            </font>
      </Label>
    

    我也有这个Excel文档:

    Birthday     Date d'anniversaire     День Рождения
    

    它包含“生日”一词的两种翻译。

    当在下拉列表中的应用程序中选择了另一种语言时,我需要能够更改标签的文本。我该怎么做?可以在里面做吗 .fxml 文件?

    这是我从文件加载stage的方式:

    FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
    
    scene = new Scene(fxmlLoader.load(), 500, 400);
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Wellerman    3 年前

    正如上面的评论中提到的,这只是我过去翻译的一个例子。这不会在FXML中进行翻译。 FXML代码段(frmCalendar.FXML)

    <Label fx:id="lblTitle" text="Title"/>
    

    控制器类代码段(CalendarController.java)

    if (!Locale.getDefault().getLanguage().equals("en")) {
        lblTitle.setText(TranslationManager.translate("en", Locale.getDefault().getLanguage(), lblTitle.getText());
    }
    

    翻译管理器java代码段

    public class TranslationManager {
        public static String translate(String langFrom, String langTo, String text){
            //Here is where you would read your excel file and return the text in the desired language.
        }
    }
    

    这里有一个很棒的 question about reading excel files 如果你不熟悉这个过程。