Do resource bundles in Java support runtime string substitution?

前端 未结 8 664
夕颜
夕颜 2020-12-25 10:32

Can you do the following with a Java ResourceBundle?

In the properties file...

example.dynamicresource=You currently have {0} accounts.
相关标签:
8条回答
  • 2020-12-25 11:19

    I don't think you can make this work for Non-English properties file.

    My message.properties file has the following line:

    info.fomat.log.message.start=Starting to parse log message in {0} format.

    And my message_fr_FR.properties file has the following line:

    info.fomat.log.message.start=A partir d'analyser le message connecter {0} format.

    This code works only for the English one

    String.format((String) messages .getString(GlobalConstants.MESSAGE_FORMAT_START), GlobalConstants.STR_JSON));

    It does NOT replace the placeholder with the value when my language / locale is French :-(

    Even MessageFormat.fomat() is no good

    0 讨论(0)
  • 2020-12-25 11:20

    Not without using the MessageFormat class, such as:

    String pattern = bundle.getString("example.dynamicresource");
    String message = MessageFormat.format(pattern, accountCount);
    
    0 讨论(0)
提交回复
热议问题