-1

I have this object in fxml (among others):

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

I also have this Excel document:

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

It contains two translations of the word Birthday.

I need to be able to change the text of the label when in the application in dropdown list another language is chosen. How would I do that? Is it possible to do inside the .fxml file?

This is how I load stage from file:

FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));

scene = new Scene(fxmlLoader.load(), 500, 400);
parsecer
  • 4,758
  • 13
  • 71
  • 140
  • You'll probably need to do this in the controller class rather than the FXML. – Wellerman May 04 '21 at 12:41
  • How? I already have the whole structure of the app, all of its components inside the FXML file. And many components need to be translated (a lot of rows in Excel) – parsecer May 04 '21 at 12:43
  • Which question? – parsecer May 04 '21 at 12:46
  • When I've translated text in java apps before, I've had the app structure in FXML but then programmatically translated everything in the .java controller class when needed. I can post an example if you'd like. – Wellerman May 04 '21 at 12:48
  • Yes I would appreciate it – parsecer May 04 '21 at 12:48
  • 3
    https://stackoverflow.com/questions/10143392/javafx-2-and-internationalization – matt May 04 '21 at 12:57
  • @parsecer if you use an @ it will notify somebody. There are a couple answers in there, it seems like you can create a resource bundle, then switch the resource bundle to change the language. – matt May 04 '21 at 12:58

1 Answers1

1

As mentioned in the comments above, this is just an example of how I have done translation in the past. This does not do translation within the FXML. Snippet of FXML (frmCalendar.fxml)

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

Snippet of Controller Class (CalendarController.java)

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

Snippet of TranslationManager.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.
    }
}

Here's a great question about reading excel files if you are not familiar with that process.

Wellerman
  • 846
  • 4
  • 14