Skip to content

Internationalization (i18n)

Spring JavaFX Boot bridges Spring Boot's MessageSource to JavaFX's ResourceBundle, giving you a unified i18n system that works in both Java code and FXML templates.

Message Files

Place standard Spring Boot message files in src/main/resources/:

messages.properties        # Default locale (English)
messages_ka.properties     # Georgian
messages_de.properties     # German

Example content:

messages.properties
app.title=My Application
page.title.main=Dashboard
main.welcome=Welcome!
main.counter=Counter value: {0}
main.button.hello=Say Hello
messages_ka.properties
app.title=ჩემი აპლიკაცია
page.title.main=მთავარი
main.welcome=მოგესალმებით!
main.counter=მთვლელის მნიშვნელობა: {0}
main.button.hello=თქვი გამარჯობა

Using Messages in FXML

JavaFX supports %key syntax in FXML to reference resource bundle keys. Since Spring JavaFX Boot bridges MessageSource to a ResourceBundle, this works seamlessly:

<Button text="%main.button.hello"/>
<Label text="%main.welcome"/>

Using Messages in Java

Inject LocalizedMessageSource for convenient locale-aware message access:

@Autowired
private LocalizedMessageSource messages;

// Simple message
String welcome = messages.msg("main.welcome");

// Parameterized message — uses {0}, {1}, etc.
String counter = messages.msg("main.counter", 42);
// → "Counter value: 42"

Switching Locale

To switch the application language at runtime:

Locale newLocale = new Locale("ka");

// Update Java's default locale
Locale.setDefault(newLocale);

// Update the message source
messages.setLocale(newLocale);

// Reload the current view to apply translations
router.reload();

router.reload() forces a full reload of the current route and all parent layouts, rebuilding everything with the new translations.

How the Bridge Works

The MessageSourceResourceBundle class bridges Spring's MessageSource to JavaFX's ResourceBundle. This is registered automatically by the auto-configuration.

When the ViewResolver loads an FXML file, it passes a MessageSourceResourceBundle instance to the FXMLLoader. This allows JavaFX's %key syntax to resolve keys through Spring's MessageSource.

Locale tracking

LocalizedMessageSource maintains its own locale state independently of Locale.getDefault(). When switching locales, update both to keep them in sync.