【问题标题】:JavaFX Internationalization with vars in Translated StringJavaFX 国际化与翻译字符串中的变量
【发布时间】:2017-03-24 20:28:20
【问题描述】:

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

我正在使用 fxml 文件来格式化我的 Windows 顺便说一句

【问题讨论】:

    标签: java javafx internationalization


    【解决方案1】:

    MessageFormatmessageArguments 一起使用。

    代码

    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}.
    

    【讨论】:

    • 不,在这种情况下没有必要。
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多