RoiEX
RoiEX

Reputation: 1235

JavaFX Internationalization with vars in Translated String

I just found out, that JavaFX has a built-in Internationalization feature! Since I'm starting to rewrite the GUI of my App I'm going to use this handy feature.
The only thing I can't seem to find is how I can add dynamic values into the translated String...
For example:
chat.message.player_joined=Player %s joined!
I would like to be able to replace %s with the corresponding player name.
But since the strings are loaded via the properties file and not something like node.setText(String.format("Player %s joined!", playerName)).
I have no Idea on how to do that...

I'm using fxml files to format my windows btw

Upvotes: 0

Views: 700

Answers (1)

MBec
MBec

Reputation: 2210

Use MessageFormat with messageArguments.

CODE

Label l = new Label();

final Locale currentLocale = new Locale("en", "US");
ResourceBundle bundle = ResourceBundle.getBundle("Bundle", currentLocale);
Object[] messageArguments = {new Integer(5)};

MessageFormat formatter = new MessageFormat("");
formatter.setLocale(currentLocale);

formatter.applyPattern(bundle.getString("TEST_BUNDLE_TEXT"));
l.setText(formatter.format(messageArguments));

RESOURCE BUNDLE

TEST_BUNDLE_TEXT=Test text with integer {0}.

Upvotes: 1

Related Questions