Internationalization in JavaFX
JavaFX provides in FXML a very easy way to internationalize your user interfaces. When creating a view from an FXML file you can provide the FXMLLoader
with a resource bundle:
Locale locale = new Locale("en", "UK");
ResourceBundle bundle = ResourceBundle.load("strings", locale);
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("ui/main.fxml"),
bundle);
This provided bundle is automatically used to translate all texts in your FXML file that start with a %
. If you have a button definition in your FXML like this:
<Button text="%ui.button.text"/>
It will automatically receive the translation for the key ui.button.text
.
Controllers
When adding the Initializable
interface and the function that comes with it you will notice that this function has a resource bundle as its parameter. This is the bundle you provide the FXMLLoader
with and can be used by the controller to further translate texts or modify other locale-dependant information in the ui.