JavaFX国际化与翻译字符串中的变量

时间:2017-03-24 20:28:20

标签: java javafx internationalization

我刚刚发现,JavaFX具有内置的国际化功能! 由于我开始重写我的应用程序的GUI,我将使用这个方便的功能。
我唯一能找到的就是如何在翻译的字符串中添加动态值...
例如:
chat.message.player_joined=Player %s joined!
我希望能够用相应的玩家名称替换%s 但由于字符串是通过属性文件加载的,而不是像node.setText(String.format("Player %s joined!", playerName))那样 我不知道如何做到这一点......

我使用fxml文件格式化我的windows btw

1 个答案:

答案 0 :(得分:1)

MessageFormatmessageArguments一起使用。

<强> 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));

资源捆绑

TEST_BUNDLE_TEXT=Test text with integer {0}.